Exemple #1
0
// Check words against known models
func getFromModel(words []string, languages []string) (result string) {
	sample := strings.Join(words, " ")

	if len([]rune(sample)) < minLength {
		return unknownLanguage
	}

	var (
		scores  map[string]int = make(map[string]int, len(languages))
		model   []string       = models.GetOrderedModel(sample)
		minimal int            = maxDistance
	)

	for _, lang := range languages {
		scores[lang] = getDistance(model, models.GetModels()[strings.ToLower(lang)])
	}

	for lang, score := range scores {
		if score < minimal {
			minimal = score
			result = lang
		}
	}

	return result
}
func Test_GetOrderedModel(t *testing.T) {
	var om []string

	om = models.GetOrderedModel("abc")
	if len(om) != 1 || !keyExists("abc", om) {
		t.Errorf("Model must be [abc]")
	}

	om = models.GetOrderedModel("abca")
	if len(om) != 2 || !keyExists("abc", om) || !keyExists("bca", om) {
		t.Errorf("Model must be [abc bca]")
	}

	om = models.GetOrderedModel("abcabdcab")
	if len(om) != 6 ||
		!keyExists("cab", om) ||
		!keyExists("abc", om) ||
		!keyExists("abd", om) ||
		!keyExists("bca", om) ||
		!keyExists("bdc", om) ||
		!keyExists("dca", om) {
		t.Errorf("Model must be [cab abc abd bca bdc dca]")
	}
}