/// <summary> /// Get the meaning and synonyms. /// </summary> /// <param name="word">The word to get the meaning of.</param> /// <returns>The thesaurus result.</returns> public ThesaurusResult Thesaurus(string word) { if (_thesaurus != null) { // Get the meaning. ThesResult result = _thesaurus.Lookup(word, _hunspell); List <ThesaurusMeaning> meanings = new List <ThesaurusMeaning>(); // For each meaning found. foreach (ThesMeaning meaning in result.Meanings) { // Add the meaning. meanings.Add(new ThesaurusMeaning(meaning.Description, meaning.Synonyms)); } // Create the result. ThesaurusResult theResult = new ThesaurusResult(meanings, result.IsGenerated); // Return the result. return(theResult); } else { throw new NotImplementedException(); } }
public static string GetLightSpunText(string text) { var words = text.Split(' '); MyThes thes = new MyThes("th_en_US_new.dat"); Hunspell hunspell = new Hunspell("en_US.aff", "en_US.dic"); for (var i = 0; i < words.Length; i++) { StringBuilder sb = new StringBuilder(); ThesResult tr = thes.Lookup(words[i], hunspell); if (tr != null) { foreach (ThesMeaning meaning in tr.Meanings) { Random random = new Random(); words[i] = meaning.Synonyms[0]; } } } text = string.Join(" ", words); return(text); }
private static void Write(Document doc, ThesResult result) { if (result == null) { return; } var sec = doc.Sections.Last; var par = (Paragraph)null; var tun = (Textrun)null; //var hyp = (Hyperlink)null; par = sec.AddParagraph(); par.Style.Spacings.SpacingAfter = 15F; tun = par.AddTextrun(RtfUtility.unicodeEncode("Synonyms Result")); tun.Style.FontSize = 24F; tun.Style.TextColor = PrimaryColor; par = sec.AddParagraph(); tun = par.AddTextrun(RtfUtility.unicodeEncode("SYNONYMS")); tun.Style.FontStyle.Bold = true; int count = 1; foreach (var item in result.Meanings.SelectMany(i => i.Synonyms).Distinct()) { par = sec.AddParagraph(RtfUtility.unicodeEncode(string.Format("{0}. {1}", count, SynUtility.Text.UppercaseFirstLetter(item)))); count++; } sec.AddParagraph(); }
/// <summary> /// Utilizes the NHunspell library. /// Looks up the key in a Thesaurus and returns true if the Matrix contains /// either the key or any synonyms of the key /// </summary> /// <param name="key">They key to lookup</param> /// <param name="similarKey">The found similar key</param> /// <returns></returns> private bool ContainsSimilarKey(string key, out string similarKey) { similarKey = key; if (Matrix.ContainsKey(key)) { return(true); } ThesResult tr = Thesaurus.Lookup(key); if (tr == null) { return(false); } int count = 0; foreach (ThesMeaning meaning in tr.Meanings) { //count++; foreach (string synonym in meaning.Synonyms) { if (Matrix.ContainsKey(synonym)) { similarKey = synonym; return(true); } } //if (count > MAX_MEANINGS) // break; } return(false); }
public static string SpinText(string text) { var words = text.Split(' '); MyThes thes = new MyThes("th_en_US_new.dat"); Hunspell hunspell = new Hunspell("en_US.aff", "en_US.dic"); for (var i = 0; i < words.Length; i++) { StringBuilder sb = new StringBuilder(); ThesResult tr = thes.Lookup(words[i], hunspell); if (tr != null) { foreach (ThesMeaning meaning in tr.Meanings) { foreach (string synonym in meaning.Synonyms) { sb.Append(synonym + "|"); } } } if (sb.ToString().Length > 2) { words[i] = "{" + sb.ToString().Substring(0, sb.ToString().Length - 1) + "}"; } } text = string.Join(" ", words); return(text); }
public static string GetMostComplexSynonymScored(string word) { // TODO: Lemmatization ThesResult result = NHThesaurus.Lookup(word); if (result == null) { // No results in thesaurus. return(word); } Dictionary <string, List <ThesMeaning> > synonymsDict = result.GetSynonyms(); string mostComplexSynonym = ""; double mostComplexScore = 0; // TODO: Check if each 'synonym' can be the same POS tag as the original word before putting it into the synonym collection. #region Synonym collection setup List <string> synonyms = new List <string>(); foreach (string synonym in synonymsDict.Keys) { // Get each key (synonym) from the result dictionary and add it to the list of synonyms. synonyms.Add(synonym); } if (synonyms.Contains(word)) /* Original word is already in the list of synonyms. Do nothing. */ } {
public static List <String> GetSynonyms(string word, string language) { List <String> synonyms = new List <string>(); if (!languages.ContainsKey(language)) { throw new Exception("Language: " + language + " is not supported."); } ThesResult meanings = spellEngine[language].LookupSynonyms(word, true); foreach (ThesMeaning meaning in meanings.Meanings) { synonyms.AddRange(meaning.Synonyms); } return(synonyms.Distinct().ToList()); }
/// <summary> /// this method return a list of synonyms from the hunspell thesaraus dictionary. /// </summary> /// <param name="query"></param> /// <returns></returns> public List <string> HunspellSynonymsList(List <string> query) { List <string> listOfSynonyms = new List <string>(); MyThes thes = new MyThes(Directory.GetCurrentDirectory().Substring(0, Directory.GetCurrentDirectory().Length - 9) + "th_en_US_new.dat"); using (Hunspell hunspell = new Hunspell(Directory.GetCurrentDirectory().Substring(0, Directory.GetCurrentDirectory().Length - 9) + "en_us.aff", Directory.GetCurrentDirectory().Substring(0, Directory.GetCurrentDirectory().Length - 9) + "en_US.dic")) { for (int i = 0; i < query.Count - 1; i++) { string queryTerm = query[i] + " " + query[i + 1]; ThesResult tr = thes.Lookup(queryTerm, hunspell); if (tr == null) { continue; } foreach (ThesMeaning meaning in tr.Meanings) { listOfSynonyms.AddRange(meaning.Synonyms); } } if (listOfSynonyms.Count == 0) { foreach (string queryTerm in query) { ThesResult tr = thes.Lookup(queryTerm, hunspell); if (tr == null) { continue; } foreach (ThesMeaning meaning in tr.Meanings) { listOfSynonyms.AddRange(meaning.Synonyms); } listOfSynonyms = listOfSynonyms.Take(2).ToList(); } } foreach (var VARIABLE in listOfSynonyms) { VARIABLE.Trim(' '); } return(listOfSynonyms); } }
public List <string> getSynonyoms(string word) { List <string> toReturn = new List <string>(); //Synonyms for words ThesResult tr = thes.Lookup(word, hunspell); if (tr != null) { foreach (ThesMeaning meaning in tr.Meanings) { foreach (string synonym in meaning.Synonyms) { toReturn.Add(synonym); } } } return(toReturn); }
public string GetSynonyms(string query) { //var words = query.Split(new char[] {' '}); //for( int i = 0; i < words.Length; i++ ) //{ // var synSetList = wordNet.GetSynSets(words[i]); // words[i] += "^5"; // if (synSetList.Count == 0) // { // continue; // } // foreach (var synSet in synSetList) // { // var synWords = synSet.Words; // for( int j = 0; j < synWords.Count; j++) // { // if(synWords[j].Contains("_")) // { // synWords[j] = synWords[j].Replace("_", " "); // synWords[j] = "\"" + synWords[j] + "\""; // } // } // var synonyms = string.Join(" ", synWords); // words[i] += " " + synonyms; // } //} //query = string.Join(" ", words); //return query; string expandedQuery = String.Empty; MyThes thes = new MyThes("th_en_us_new.dat"); using (Hunspell hunspell = new Hunspell("en_AU.aff", "en_AU.dic")) { var words = query.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < words.Length; i++) { List <string> stems = new List <string>(); var word = words[i]; expandedQuery += " " + word + "^5"; var wordStems = hunspell.Stem(word); if (wordStems.Count > 0) { stems.Add(word); stems.AddRange(wordStems); } else { stems.Add(word); } foreach (var stem in stems) { ThesResult tr = thes.Lookup(stem, hunspell); if (!stem.Equals(word)) { expandedQuery += " " + stem; } if (tr != null && tr.Meanings.Count > 0) { foreach (ThesMeaning meaning in tr.Meanings) { expandedQuery += " " + string.Join(" ", meaning.Synonyms); } } } } } return(expandedQuery); }
internal static void CheckWordCorrectness(TextBox wordTextBox, String languageSign, TextBox contentTextBox) { String word = wordTextBox.Text.Trim(); if (String.IsNullOrEmpty(word)) { return; } if (!String.IsNullOrEmpty(contentTextBox.Text)) { contentTextBox.Text += "----------------------------" + Environment.NewLine; } contentTextBox.Text += "[" + DateTime.Now.ToLongTimeString() + "] "; using (Hunspell hunspell = new Hunspell(ConstantUtil.ApplicationExecutionPath() + "\\Languages\\" + languageSign + ".aff", ConstantUtil.ApplicationExecutionPath() + "\\Languages\\" + languageSign + ".dic")) { bool correct = hunspell.Spell(word); String correctness; if (correct) { wordTextBox.BackColor = Color.LightGreen; correctness = LanguageUtil.GetCurrentLanguageString("Correct", className); } else { wordTextBox.BackColor = Color.Tomato; correctness = LanguageUtil.GetCurrentLanguageString("Uncorrect", className); } contentTextBox.Text += String.Format(LanguageUtil.GetCurrentLanguageString("Word", className), word, correctness) + Environment.NewLine; //Uncorrect word, search suggestions if (!correct) { List <String> suggestions = hunspell.Suggest(word); contentTextBox.Text += String.Format(LanguageUtil.GetCurrentLanguageString("Tips", className, suggestions.Count), suggestions.Count) + Environment.NewLine; foreach (String suggestion in suggestions) { contentTextBox.Text += String.Format(LanguageUtil.GetCurrentLanguageString("Tip", className), suggestion) + Environment.NewLine; } return; } //Correct word, search meanings and synonyms MyThes thes = new MyThes(ConstantUtil.ApplicationExecutionPath() + "\\Languages\\" + languageSign + "_th.dat"); ThesResult tr = thes.Lookup(word, hunspell); if ((tr == null) || (tr.IsGenerated)) { return; } foreach (ThesMeaning meaning in tr.Meanings) { contentTextBox.Text += String.Format(LanguageUtil.GetCurrentLanguageString("Meaning", className), meaning.Description) + Environment.NewLine; foreach (String synonym in meaning.Synonyms) { contentTextBox.Text += String.Format(LanguageUtil.GetCurrentLanguageString("Synonym", className), synonym) + Environment.NewLine; } } } }
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(); }