<commit_before>/*
 * Human-friendly token generator
 *     Copyright (c) 2017, <NAME> <<EMAIL>>
 *
 *   For license see LICENSE
 */

package toktok

import (
	"math/rand"
	"sync"
	"time"

	"github.com/xrash/smetrics"
)

type Token struct {
	Code string
}

type Bucket struct {
	length uint
	runes  []rune
	Tokens map[string]Token

	sync.RWMutex
}

func NewBucket(tokenLength uint) (Bucket, error) {
	return NewBucketWithRunes(tokenLength, "<KEY>")
}

func NewBucketWithRunes(tokenLength uint, runes string) (Bucket, error) {
	if tokenLength < 2 {
		return Bucket{}, ErrTokenLengthTooSmall
	}
	if len(runes) < 4 {
		return Bucket{}, ErrTooFewRunes
	}

	return Bucket{
		length: tokenLength,
		runes:  []rune(runes),
		Tokens: make(map[string]Token),
	}, nil
}

func (bucket *Bucket) NewToken(distance int) Token {
	if distance < 1 {
		return Token{}
	}

	bucket.Lock()
	defer bucket.Unlock()

	var c string
	for {
		c = GenerateToken(bucket.length, bucket.runes)

		dupe := false
		for _, token := range bucket.Tokens {
			if hd := smetrics.WagnerFischer(c, token.Code, 1, 1, 2); hd <= distance {
				dupe = true
				break
			}
		}
		if !dupe {
			break
		}
	}

	token := Token{
		Code: c,
	}
	bucket.Tokens[token.Code] = token

	return token
}

func (bucket *Bucket) Resolve(code string) (Token, int) {
	var t Token
	distance := 65536

	bucket.RLock()
	defer bucket.RUnlock()

	for _, token := range bucket.Tokens {
		if hd := smetrics.WagnerFischer(code, token.Code, 1, 1, 2); hd <= distance {
			if hd == distance {
				// duplicate distance, ignore the previous result as it's not unique enough
				t = Token{}
			} else {
				t = token
				distance = hd

				if distance == 0 {
					// perfect match
					break
				}
			}
		}
	}

	return t, distance
}

func GenerateToken(n uint, letterRunes []rune) string {
	l := len(letterRunes)
	b := make([]rune, n)<commit_msg>Try finding a perfect match in the tokens map first<commit_after>/*
 * Human-friendly token generator
 *     Copyright (c) 2017, <NAME> <<EMAIL>>
 *
 *   For license see LICENSE
 */

package toktok

import (
	"math/rand"
	"sync"
	"time"

	"github.com/xrash/smetrics"
)

type Token struct {
	Code string
}

type Bucket struct {
	length uint
	runes  []rune
	Tokens map[string]Token

	sync.RWMutex
}

func NewBucket(tokenLength uint) (Bucket, error) {
	return NewBucketWithRunes(tokenLength, "<KEY>")
}

func NewBucketWithRunes(tokenLength uint, runes string) (Bucket, error) {
	if tokenLength < 2 {
		return Bucket{}, ErrTokenLengthTooSmall
	}
	if len(runes) < 4 {
		return Bucket{}, ErrTooFewRunes
	}

	return Bucket{
		length: tokenLength,
		runes:  []rune(runes),
		Tokens: make(map[string]Token),
	}, nil
}

func (bucket *Bucket) NewToken(distance int) Token {
	if distance < 1 {
		return Token{}
	}

	bucket.Lock()
	defer bucket.Unlock()

	var c string
	for {
		c = GenerateToken(bucket.length, bucket.runes)

		dupe := false
		for _, token := range bucket.Tokens {
			if hd := smetrics.WagnerFischer(c, token.Code, 1, 1, 2); hd <= distance {
				dupe = true
				break
			}
		}
		if !dupe {
			break
		}
	}

	token := Token{
		Code: c,
	}
	bucket.Tokens[token.Code] = token

	return token, i
}

func (bucket *Bucket) Resolve(code string) (Token, int) {
	distance := 65536

	bucket.RLock()
	defer bucket.RUnlock()

	// try to find a perfect match first
	t, ok := bucket.Tokens[code]
	if ok {
		return t, 0
	}

	// find the closest match
	for _, token := range bucket.Tokens {
		if hd := smetrics.WagnerFischer(code, token.Code, 1, 1, 2); hd <= distance {
			if hd == distance {
				// duplicate distance, ignore the previous result as it's not unique enough
				t = Token{}
			} else {
				t = token
				distance = hd
			}
		}
	}

	return t, distance
}

func GenerateToken(n uint, letterRunes []rune) string {
	l := len(letterRunes)
	b := make([]rune, n)
{'fraction_non_alphanumeric': 0.12705882352941175, 'fraction_numerical': 0.009934640522875817, 'mean_word_length': 3.2183020948180814, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '161120', 'n_tokens_mistral': 1543, 'n_tokens_neox': 1391, 'n_words': 399}