/// <summary>Returns a list of Synonyms for a given word</summary> /// <param name="word">the word</param> /// <param name="pos">The Part of speech of a word</param> /// <param name="includeMorphs">include morphology? (fuzzy matching)</param> /// <returns>An array of strings containing the synonyms found</returns> /// <remarks> /// Note that my usage of 'Synonyms' here is not the same as hypernyms as defined by /// WordNet. Synonyms in this sense are merely words in the same SynSet as the given /// word. Hypernyms are found by tracing the pointers in a given synset. /// </remarks> public static string[] FindSynonyms(string word, Wnlib.PartsOfSpeech pos, bool includeMorphs) { // get an index to a synset collection word = word.ToLower(); Wnlib.Index index = Wnlib.Index.lookup(word, Wnlib.PartOfSpeech.of(pos)); // none found? if (index == null) { if (!includeMorphs) { return(null); } // check morphs var morphs = new Wnlib.MorphStr(word, Wnlib.PartOfSpeech.of(pos)); string morph = ""; while ((morph = morphs.next()) != null) { index = Wnlib.Index.lookup(morph, Wnlib.PartOfSpeech.of(pos)); if (index != null) { break; } } } // still none found? if (index == null) { return(null); } // at this point we will always have a valid index return(lookupSynonyms(index)); }
private static WordInfo lookupWordMorphs(string word) { // OVERVIEW: This functions only gets called when the word was not found with // an exact match. So, enumerate all the parts of speech, then enumerate // all of the word's morphs in each category. Perform a lookup on each // morph and save the morph/strength/part-of-speech data sets. Finally, // loop over all the data sets and then pick the strongest one. ArrayList wordinfos = new ArrayList(); // for each part of speech... for (int i = 0; i < enums.Length; i++) { // get a valid part of speech Wnlib.PartsOfSpeech pos = enums[i]; if (pos == Wnlib.PartsOfSpeech.Unknown) continue; // generate morph list Wnlib.MorphStr morphs = new Wnlib.MorphStr(word, Wnlib.PartOfSpeech.of(pos)); string morph = ""; while ((morph = morphs.next()) != null) { // get an index to a synset collection Wnlib.Index index = Wnlib.Index.lookup(morph, Wnlib.PartOfSpeech.of(pos)); // none found? if (index == null) continue; // save the wordinfo WordInfo wordinfo = getMorphInfo(wordinfos, morph); wordinfo.senseCounts[i] = index.sense_cnt; } } // search the wordinfo list for the best match WordInfo bestWordInfo = new WordInfo(); int maxStrength = 0; foreach (WordInfo wordinfo in wordinfos) { // for each part of speech... int maxSenseCount = 0; int strength = 0; for (int i = 0; i < enums.Length; i++) { // get a valid part of speech Wnlib.PartsOfSpeech pos = enums[i]; if (pos == Wnlib.PartsOfSpeech.Unknown) continue; // determine part of speech and strength strength += wordinfo.senseCounts[i]; if (wordinfo.senseCounts[i] > maxSenseCount) { maxSenseCount = wordinfo.senseCounts[i]; wordinfo.partOfSpeech = pos; } } // best match? if (strength > maxStrength) { maxStrength = strength; bestWordInfo = wordinfo; } } return bestWordInfo; }
/// <summary>Returns a list of Synonyms for a given word</summary> /// <param name="word">the word</param> /// <param name="pos">The Part of speech of a word</param> /// <param name="includeMorphs">include morphology? (fuzzy matching)</param> /// <returns>An array of strings containing the synonyms found</returns> /// <remarks> /// Note that my usage of 'Synonyms' here is not the same as hypernyms as defined by /// WordNet. Synonyms in this sense are merely words in the same SynSet as the given /// word. Hypernyms are found by tracing the pointers in a given synset. /// </remarks> public static string[] FindSynonyms(string word, Wnlib.PartsOfSpeech pos, bool includeMorphs) { // get an index to a synset collection word = word.ToLower(); Wnlib.Index index = Wnlib.Index.lookup(word, Wnlib.PartOfSpeech.of(pos)); // none found? if (index == null) { if (!includeMorphs) return null; // check morphs Wnlib.MorphStr morphs = new Wnlib.MorphStr(word, Wnlib.PartOfSpeech.of(pos)); string morph = ""; while ((morph = morphs.next()) != null) { index = Wnlib.Index.lookup(morph, Wnlib.PartOfSpeech.of(pos)); if (index != null) break; } } // still none found? if (index == null) return null; // at this point we will always have a valid index return lookupSynonyms(index); }
private static WordInfo LookupWordMorphs(string word) { // OVERVIEW: This functions only gets called when the word was not found with // an exact match. So, enumerate all the parts of speech, then enumerate // all of the word's morphs in each category. Perform a lookup on each // morph and save the morph/strength/part-of-speech data sets. Finally, // loop over all the data sets and then pick the strongest one. ArrayList wordinfos = new ArrayList(); // for each part of speech... for (int i = 0; i < Enums.Length; i++) { // get a valid part of speech Wnlib.PartsOfSpeech pos = Enums[i]; if (pos == Wnlib.PartsOfSpeech.Unknown) { continue; } // generate morph list Wnlib.MorphStr morphs = new Wnlib.MorphStr(word, Wnlib.PartOfSpeech.of(pos)); string morph = ""; while ((morph = morphs.next()) != null) { // get an index to a synset collection Wnlib.Index index = Wnlib.Index.lookup(morph, Wnlib.PartOfSpeech.of(pos)); // none found? if (index == null) { continue; } // save the wordinfo WordInfo wordinfo = GetMorphInfo(wordinfos, morph); wordinfo.SenseCounts[i] = index.SenseCnt; } } // search the wordinfo list for the best match WordInfo bestWordInfo = new WordInfo(); int maxStrength = 0; foreach (WordInfo wordinfo in wordinfos) { // for each part of speech... int maxSenseCount = 0; int strength = 0; for (int i = 0; i < Enums.Length; i++) { // get a valid part of speech Wnlib.PartsOfSpeech pos = Enums[i]; if (pos == Wnlib.PartsOfSpeech.Unknown) { continue; } // determine part of speech and strength strength += wordinfo.SenseCounts[i]; if (wordinfo.SenseCounts[i] > maxSenseCount) { maxSenseCount = wordinfo.SenseCounts[i]; wordinfo.partOfSpeech = pos; } } // best match? if (strength > maxStrength) { maxStrength = strength; bestWordInfo = wordinfo; } } return(bestWordInfo); }
private static WordInfo lookupWordMorphs(string word, bool tagged_only) { // OVERVIEW: This functions only gets called when the word was not found with // an exact match. So, enumerate all the parts of speech, then enumerate // all of the word's morphs in each category. Perform a lookup on each // morph and save the morph/strength/part-of-speech data sets. Finally, // loop over all the data sets and then pick the strongest one. ArrayList wordinfos = new ArrayList(); // for each part of speech... for (int i = 0; i < enums.Length; i++) { // get a valid part of speech Wnlib.PartsOfSpeech pos = enums[i]; if (pos == Wnlib.PartsOfSpeech.Unknown) { continue; } // generate morph list Wnlib.MorphStr morphs = new Wnlib.MorphStr(word, Wnlib.PartOfSpeech.of(pos)); string morph = ""; while ((morph = morphs.next()) != null) { // get an index to a synset collection Wnlib.Index index = Wnlib.Index.lookup(morph, Wnlib.PartOfSpeech.of(pos)); // none found? if (index == null) { continue; } // none tagged if (tagged_only && index.tagsense_cnt == 0) { continue; } // save the wordinfo WordInfo wordinfo = getMorphInfo(wordinfos, morph); if (tagged_only) { wordinfo.senseCounts[i] = index.tagsense_cnt; } else { wordinfo.senseCounts[i] = index.sense_cnt; } } } return(WordInfo.Compine(wordinfos)); /* * // search the wordinfo list for the best match * WordInfo bestWordInfo = new WordInfo(); * int maxStrength = 0; * foreach( WordInfo wordinfo in wordinfos ) * { * // for each part of speech... * int maxSenseCount = 0; * int strength = 0; * for( int i=0; i<enums.Length; i++ ) * { * // get a valid part of speech * Wnlib.PartsOfSpeech pos = enums[i]; * if( pos == Wnlib.PartsOfSpeech.Unknown ) * continue; * * // determine part of speech and strength * strength += wordinfo.senseCounts[i]; * if( wordinfo.senseCounts[i] > maxSenseCount ) * { * maxSenseCount = wordinfo.senseCounts[i]; * wordinfo.partOfSpeech = pos; * } * } * * // best match? * if( strength > maxStrength ) * { * maxStrength = strength; * bestWordInfo = wordinfo; * } * } * * return bestWordInfo; */ }