public ReadOnlyCollection <string> Format(IEnumerable <string> words) { var result = new List <string>(); var r = new Regex("st:(\\w+[#]?)"); var affFileData = Resources.ru_aff; var dicFileData = Encoding.UTF8.GetBytes(Resources.ru_dic); using (var hunspell = new Hunspell(dictionaryFileData: dicFileData, affixFileData: affFileData)) { foreach (var word in words) { var suggestions = hunspell.Analyze(word.ToLower()); if (suggestions.Count == 0) { result.Add(word.ToLower()); continue; } var w = r.Match(suggestions.First()).Groups[1].Value; result.Add(w); } } return(new ReadOnlyCollection <string>(result)); }
public static void GetInfo(string word) { Console.WriteLine("Spelled TestCorrects:" + HunspellTr.Spell(word)); var stems = HunspellTr.Stem(word); Console.WriteLine("Stems:"); foreach (var stem in stems) { Console.WriteLine(stem); } var suggestions = HunspellTr.Suggest(word); Console.WriteLine("Suggestions:"); foreach (var suggestion in suggestions) { Console.WriteLine(suggestion); } Console.WriteLine("Analyses:"); var analyses = HunspellTr.Analyze(word); foreach (var analysis in analyses) { Console.WriteLine(analysis); } }
private string LemmatizeWordWithHunspell(string word, Hunspell speller) { if (word.Length == 0) { return(""); } else if (word.Length == 1) { return(word.Trim()); } else { if (speller.Spell(word)) { string theOne = ""; List <string> outcome = speller.Analyze(word.Trim()); if (outcome.Count == 0) { return(word.Trim()); } else { foreach (string o in outcome) { string tmp = Regex.Match(o, @"st:(.+?)\b").Groups[1].ToString(); if (theOne == "") { theOne = tmp; } if (tmp.Trim().ToLower() == word.Trim().ToLower()) { theOne = tmp; } } return(theOne); } } else { return(word.Trim()); } } }
/// <summary> /// Get morphs for the word. /// </summary> /// <param name="word">The word to analyse.</param> /// <returns>The list of morphs.</returns> public List <string> Analyze(string word) { return(_hunspell.Analyze(word)); }
static void Main(string[] args) { using (Hunspell hunspell = new Hunspell("en_us.aff", "en_us.dic")) { var correct = hunspell.Spell("houses"); var suggest = hunspell.Suggest("haise"); foreach (var x in suggest) { Console.WriteLine(x); } } /* * var test = new SpellEngineTests(); * test.CreationAndDestructionTest(); * test.FunctionsTest(); * return; */ // var test = new HyphenTests(); // test.CreationAndDestructionTest(); // test.MemoryLeakTest(); // test.UnicodeFilenameTest(); // test.GermanUmlautTest(); // test.CyrillicLanguagesTest(); // test.NemethTests(); var test = new HunspellTests(); // test.AllDictionariesTest(); test.SpellComplexWordsTest(); test.AddWordTest(); // test.GermanUmlautTest(); // test.UnicodeFilenameTest(); // test.MemoryLeakTest(); /* * var test = new InteropTests(); * test.Init(); * test.ArrayInteropTests(); * test.StringInteropTests(); * * * Console.WriteLine(""); * Console.WriteLine("Press any key to continue..."); * Console.ReadKey(); * * return; */ Console.WriteLine("NHunspell functions and classes demo"); /* * Console.WriteLine("Thesaurus with Thes"); * Thes thes = new Thes(); * thes.LoadOpenOffice("th_en_us_new.dat"); */ Console.WriteLine(""); Console.WriteLine("Thesaurus with Thes"); MyThes thes = new MyThes("th_en_us_new.dat"); using (Hunspell hunspell = new Hunspell("en_us.aff", "en_us.dic")) { ThesResult result = thes.Lookup("cars", hunspell); foreach (ThesMeaning meaning in result.Meanings) { Console.WriteLine(" Meaning:" + meaning.Description); foreach (string synonym in meaning.Synonyms) { Console.WriteLine(" Synonym:" + synonym); } } } Console.WriteLine(""); Console.WriteLine("Spell Check with with Hunspell"); // Important: Due to the fact Hunspell will use unmanaged memory you have to serve the IDisposable pattern // In this block of code this is be done by a using block. But you can also call hunspell.Dispose() using (Hunspell hunspell = new Hunspell("en_us.aff", "en_us.dic")) { Console.WriteLine("Check if the word 'Recommendation' is spelled correct"); bool correct = hunspell.Spell("Recommendation"); Console.WriteLine("Recommendation is spelled " + (correct ? "correct" : "not correct")); Console.WriteLine(""); Console.WriteLine("Make suggestions for the word 'Recommendatio'"); List <string> suggestions = hunspell.Suggest("Recommendatio"); Console.WriteLine("There are " + suggestions.Count.ToString() + " suggestions"); foreach (string suggestion in suggestions) { Console.WriteLine("Suggestion is: " + suggestion); } Console.WriteLine(""); Console.WriteLine("Analyze the word 'decompressed'"); List <string> morphs = hunspell.Analyze("decompressed"); foreach (string morph in morphs) { Console.WriteLine("Morph is: " + morph); } Console.WriteLine(""); Console.WriteLine("Stem the word 'decompressed'"); List <string> stems = hunspell.Stem("decompressed"); foreach (string stem in stems) { Console.WriteLine("Stem is: " + stem); } /* * for (; ; ) * { * Console.WriteLine(""); * Console.WriteLine("Word1:"); * string word = Console.ReadLine(); * Console.WriteLine("Word2:"); * string word2 = Console.ReadLine(); * * List<string> generated = hunspell.Generate(word, word2); // Generate("Girl","Boys"); * foreach (string stem in generated) * { * Console.WriteLine("Generated is: " + stem); * } * } */ } Console.WriteLine(""); Console.WriteLine("Hyphenation with Hyph"); // Important: Due to the fact Hyphen will use unmanaged memory you have to serve the IDisposable pattern // In this block of code this is be done by a using block. But you can also call hyphen.Dispose() using (Hyphen hyphen = new Hyphen("hyph_en_us.dic")) { Console.WriteLine("Get the hyphenation of the word 'Recommendation'"); HyphenResult hyphenated = hyphen.Hyphenate("Recommendation"); Console.WriteLine("'Recommendation' is hyphenated as: " + hyphenated.HyphenatedWord); hyphenated = hyphen.Hyphenate("eighteen"); hyphenated = hyphen.Hyphenate("eighteen"); } Console.WriteLine(""); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }