Last active
July 12, 2018 08:16
-
-
Save ArseniySavin/d5c63420a0202fd49a58c47d4e6f22f8 to your computer and use it in GitHub Desktop.
N-Gram
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
List<string> dict = new List<string> | |
{ | |
"Кодилак".ToUpper(), | |
"Кодла".ToUpper(), | |
"Колода".ToUpper(), | |
"Изуируд".ToUpper(), | |
"Рокод".ToUpper(), | |
"Диля".ToUpper(), | |
"Рокот".ToUpper(), | |
"Код".ToUpper(), | |
"Ваня".ToUpper(), | |
"Няня".ToUpper() | |
}; | |
var text = "Крокодила нян".ToUpper(); | |
var res = GetNGram(text, 3, dict); | |
Console.WriteLine(text); | |
Console.WriteLine("---"); | |
foreach (var item in res) | |
{ | |
Console.WriteLine(item); | |
} | |
Console.ReadKey(); | |
} | |
static List<string> GetNGram(string text, int nGramLen, List<string> dict) | |
{ | |
List<string> nGramOfVal = new List<string>(); | |
List<string> result = new List<string>(); | |
string[] words = text.Split(' '); | |
for (int i = 0; i < words.Length; i++) | |
{ | |
char[] word = words[i].ToArray(); | |
for (int k = 0; k < word.Length; k++) | |
{ | |
string res = string.Empty; | |
for (int j = k; j < (nGramLen + k); j++) | |
{ | |
if (j == word.Length) | |
break; | |
res += word[j]; | |
} | |
if (res.Length == nGramLen) | |
nGramOfVal.Add(res); | |
} | |
} | |
for (int i = 0; i < dict.Count; i++) | |
{ | |
for (int j = 0; j < nGramOfVal.Count; j++) | |
{ | |
if (dict[i].Contains(nGramOfVal[j])) | |
result.Add(dict[i]); | |
} | |
} | |
result = result.Distinct().ToList(); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment