private string Check(string sample, string[] langs) { if (sample.Length < MIN_LENGTH) { return(null); } Hashtable scores = new Hashtable(); TrigramModel model = CreateOrderedModel(sample); int lowestScore = Int32.MaxValue; string lowestScoreLanCode = null; for (int i = 0; i < langs.Length; i++) { string lkey = langs[i].ToLower(); if (langStat.Models[lkey] == null) { continue; //next please, no known model for this } TrigramModel known_model = (TrigramModel)langStat.Models[lkey]; int dist = Distance(model, known_model); scores[lkey] = dist; if (dist < lowestScore) { lowestScore = dist; lowestScoreLanCode = lkey; } } return(lowestScoreLanCode); }
private int Distance(TrigramModel model, TrigramModel known_model) { // Calculate the distance to the known model. int dist = 0; for (int i = 0; i < model.Trigrams.Length; i++) { if (known_model.HasTrigram(model.Trigrams[i].t)) { dist += Math.Abs(model.Trigrams[i].score - known_model.GetScore(model.Trigrams[i].t)); } else { dist += MAX_GRAMS; } } return(dist); }