/// <summary> /// Gets the relatedness. /// </summary> /// <param name="pos">The pos.</param> /// <returns> /// Return list of option for searching relatedness correspond to pos /// E.g hypo, hyper of noun /// tropo of verb. /// </returns> public static Opt[] GetRelatedness(PartsOfSpeech pos) { switch (pos) { case PartsOfSpeech.Noun: { var nounRelatedness = new[] { Opt.at(8), //hyper Opt.at(14), //holo Opt.at(19), //mero Opt.at(12) //hypo }; return nounRelatedness; } case PartsOfSpeech.Verb: { var verbRelatedness = new[] { Opt.at(31),//hyper Opt.at(36)//tropo // may be 38 }; return verbRelatedness; } case PartsOfSpeech.Adj: { var adjectiveRelatedness = new[] { Opt.at(0) }; return adjectiveRelatedness; } case PartsOfSpeech.Adv: { var advebRelatedness = new[] { Opt.at(48) }; return advebRelatedness; } } return null; }
private MyWordInfo[] LookupCandidates(Wnlib.Index index, MyWordInfo pos ) { if (pos.Sense < 0) pos.Sense=1; SynSet synset=new Wnlib.SynSet( index.offs[pos.Sense - 1 ], index.pos , index.wd, null , pos.Sense - 1); ArrayList lexemes=new ArrayList() ; ArrayList synIndex=new ArrayList() ; foreach (Lexeme obj in synset.words) { lexemes.Add(obj) ; synIndex.Add(index.offs[pos.Sense - 1 ]); } if (index.offs.Length > 1) { if (lexemes.Count <= 1) { for(int i=0; i < index.offs.Length; i++ ) { synset=new Wnlib.SynSet( index.offs[i], index.pos, index.wd, null, i ); foreach (Lexeme obj in synset.words) { synIndex.Add(index.offs[i]); lexemes.Add(obj) ; } } } else { synset=new Wnlib.SynSet( index.offs[0], index.pos, index.wd, null, 0 ); int count=0; //get top most frequency word senses foreach (Lexeme obj in synset.words) { lexemes.Add(obj) ; synIndex.Add(index.offs[0]); ++count; if (count > 4) break; } } } ArrayList sortedSet=new ArrayList() ; Hashtable trace=new Hashtable() ; int hasSem=0; for (int i = 0; i < lexemes.Count; i++) { Lexeme word=(Lexeme)lexemes[i]; word.word=word.word.ToLower() ; int senIndex=(int)synIndex[i]; if (senIndex != -1 && word.wnsns > 0) { word.semcor=new Wnlib.SemCor(word, senIndex); lexemes[i]=word; ++hasSem; } if (!trace.ContainsKey(word.word) ) { if ((word.semcor != null && word.semcor.semcor > 0 ) || (hasSem < 4)) { trace[word.word]=1; sortedSet.Add(word) ; } } //catch {} } Lexeme[] words=(Lexeme[])sortedSet.ToArray( typeof(Lexeme) ); ArrayList candidates=new ArrayList(); for( int i=0; i < words.Length; i++ ) { string word=words[i].word.Replace("_", " " ); if( word[0] <= 'Z' ) continue; MyWordInfo newpos=new MyWordInfo(word, pos.Pos) ; newpos.Sense=words[i].wnsns; if (words[i].semcor != null) newpos.Frequency=words[i].semcor.semcor; else newpos.Frequency=0; candidates.Add( newpos); } if (!trace.ContainsKey (index.wd)) candidates.Add(pos) ; if (candidates.Count > 1) { CompareLexeme comparer=new CompareLexeme(); candidates.Sort(comparer); } return (MyWordInfo[])candidates.ToArray( typeof(MyWordInfo) ); }
public MyWordInfo(string word, Wnlib.PartsOfSpeech pos) { this.Word=word; this.Pos=pos; this.Sense =0; }
/// <summary> /// Displays the results of the search. /// </summary> /// <param name="opt">The opt object holds the user-defined search options</param> private void ShowResults(Wnlib.Opt opt) { string tmpstr = ""; if (list.Count == 0) { showFeedback("Search for " + txtSearchWord.Text + " returned 0 results."); return; } Overview tmptbox = new Overview(); if ((!object.ReferenceEquals(pbobject.GetType(), tmptbox.GetType()))) { Overview tb = new Overview(); txtOutput.Text = ""; tb.useList(list, help, ref tmpstr); if ((help != null) & !string.IsNullOrEmpty(help)) { tmpstr = help + Constants.vbCrLf + Constants.vbCrLf + tmpstr; } tmpstr = Strings.Replace(tmpstr, Constants.vbLf, Constants.vbCrLf); tmpstr = Strings.Replace(tmpstr, Constants.vbCrLf, "", 1, 1); tmpstr = Strings.Replace(tmpstr, "_", " "); showFeedback(tmpstr); if (string.IsNullOrEmpty(tmpstr) | tmpstr == "<font color='green'><br />" + Constants.vbCr + " " + txtSearchWord.Text + " has no senses </font>") { showFeedback("Search for " + txtSearchWord.Text + " returned 0 results."); } txtOutput.Visible = true; pbobject = tb; } txtSearchWord.Focus(); if ((se != null)) { if (se.morphs.Count > 0) { // use morphs instead of se string wrd = null; list = new ArrayList(); foreach (string wrd_loopVariable in se.morphs.Keys) { wrd = wrd_loopVariable; // Build the treecontrol with the search results. // Node hierarchy is automatically constructed in the TreeControl. list.Add(se.morphs[wrd]); treeControl1.fillTree(list, false); } } else { // there are no morphs - all senses exist in se // Build the treecontrol with the search results. // Node hierarchy is automatically constructed in the TreeControl. list = new ArrayList(); list.Add(se); treeControl1.fillTree(list, false); } } }
/// <summary> /// Helper for displaying output and associated housekeeping. /// </summary> /// <param name="opt"></param> public void FixDisplay(Wnlib.Opt opt) { pbobject = ""; ShowResults(opt); txtSearchWord.Focus(); }
private void DoSearch(Wnlib.Opt opt) { if (opt.sch.ptp.mnemonic == "OVERVIEW") { Overview(); return; } txtOutput.Text = ""; Refresh(); list = new ArrayList(); se = new Wnlib.Search(txtSearchWord.Text, true, opt.pos, opt.sch, Int16.Parse(txtSenses.Text)); int a = se.buf.IndexOf("\\n"); if ((a >= 0)) { if ((a == 0)) { se.buf = se.buf.Substring(a + 1); a = se.buf.IndexOf("\\n"); } StatusBar1.Text = se.buf.Substring(0, a); se.buf = se.buf.Substring(a + 1); } list.Add(se); if ((Wnlib.WNOpt.opt("-h").flag)) { help = new Wnlib.WNHelp(opt.sch, opt.pos).help; } FixDisplay(opt); }
private static string[] lookupSynonyms(Wnlib.Index index) { // OVERVIEW: For each sense, grab the synset associated with our index. // Then, add the lexemes in the synset to a list. ArrayList synonyms = new ArrayList(10); // for each sense... for (int s = 0; s < index.offs.Length; s++) { // read in the word and its pointers Wnlib.SynSet synset = new Wnlib.SynSet(index.offs[s], index.pos, index.wd, null, s); // build a string out of the words for (int i = 0; i < synset.words.Length; i++) { string word = synset.words[i].word.Replace("_", " "); // if the word is capitalized, that means it's a proper noun. We don't want those. if (word[0] <= 'Z') continue; // add it to the list if it's a different word if (string.Compare(word, index.wd, true) != 0) synonyms.Add(word); } } return (string[])synonyms.ToArray(typeof(string)); }
/// <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 void FindMeroHolonyms(Wnlib.SynSet synset) { for (int i = 0; i < synset.ptrs.Length; i++) { if (synset.ptrs[i].ptp.ident == 12 || synset.ptrs[i].ptp.ident == 13) { Wnlib.SynSet daSynSet = new Wnlib.SynSet(synset.ptrs[i].off, synset.pos, synset); } } }
private void FindHypernyms(Wnlib.SynSet synset, int depth) { for (int i = 0; i < synset.ptrs.Length; i++) { if (synset.ptrs[i].ptp.ident == 2) // Hyper Pointer { Wnlib.SynSet parentSynSet = new Wnlib.SynSet(synset.ptrs[i].off, synset.pos, synset); UpdateHashMap(parentSynSet.hereiam, depth); depth--; if (depth != 0) { FindHypernyms(parentSynSet, depth); } } } }
/// <summary> /// Displays the results of the search. /// </summary> /// <param name="opt">The opt object holds the user-defined search options</param> private void ShowResults(Wnlib.Opt opt) { string tmpstr = ""; if (list.Count == 0) { showFeedback("Search for " + txtSearchWord.Text + " returned 0 results."); return; } Overview tmptbox = new Overview(); if ((!object.ReferenceEquals(pbobject.GetType(), tmptbox.GetType()))) { Overview tb = new Overview(); tb.useList(list, help, ref tmpstr); if ((help != null) & !string.IsNullOrEmpty(help)) { tmpstr = help + Constants.vbCrLf + Constants.vbCrLf + tmpstr; } tmpstr = Strings.Replace(tmpstr, Constants.vbLf, Constants.vbCrLf); tmpstr = Strings.Replace(tmpstr, Constants.vbCrLf, "", 1, 1); tmpstr = Strings.Replace(tmpstr, "_", " "); showFeedback(tmpstr); if (string.IsNullOrEmpty(tmpstr) | tmpstr == "<font color='green'><br />" + Constants.vbCr + " " + txtSearchWord.Text + " has no senses </font>") { showFeedback("Search for " + txtSearchWord.Text + " returned 0 results."); } pbobject = tb; } txtSearchWord.Focus(); if ((se != null)) { string outstr = ""; if (se.morphs.Count > 0) { // use morphs instead of se string wrd = null; foreach (string wrd_loopVariable in se.morphs.Keys) { wrd = wrd_loopVariable; // Build the output with the search results. // Node hierarchy is automatically constructed list = null; list[0] = se.morphs[wrd]; outstr = wnColour.buildContents(list); } } else { // there are no morphs - all senses exist in se // Build the output with the search results. // Node hierarchy is automatically constructed list[0] = se; outstr = wnColour.buildContents(list); } wnColour.Canvas.DocumentText = outstr; } }
/// <summary> /// Iterates and formats all of the children of the given top-level Search /// object for a given Part Of Speech. /// </summary> /// <param name="ssarray">The SynSetList for a given Part Of Speech</param> /// <returns></returns> private string FormatWN(Wnlib.SynSetList ssarray) { string retstr = ""; //Wnlib.SynSet ss = default(Wnlib.SynSet); //Wnlib.Lexeme lx = default(Wnlib.Lexeme); foreach ( Wnlib.SynSet ss in ssarray) { retstr += "<li>"; foreach (Wnlib.Lexeme lx in ss.words) { retstr += FormatLexeme(lx.word) + ", "; } // remove last comma retstr = Strings.Mid(retstr, 1, retstr.Length - 2); retstr += ": "; // show the definition retstr += FormatDefn(ss.defn); // recursive call to self for all children if ((ss.senses != null)) { retstr += "<ul>"; retstr += FormatWN(ss.senses); retstr += "</ul>"; } retstr += "</li>"; } return retstr; }
/// <summary> /// Takes a given Part Of Speech and iterates the Search object to build the /// hierarchical output. /// </summary> /// <param name="sch"></param> /// <param name="tmppos"></param> /// <returns></returns> public string contentIteration(Wnlib.Search sch) { //Wnlib.SynSet ss = default(Wnlib.SynSet); //Wnlib.Lexeme lx = default(Wnlib.Lexeme); string outstr = ""; outstr += "<ul>"; // iterate the returned senses foreach ( Wnlib.SynSet ss in sch.senses) { outstr += "<li>"; // format the WORDs foreach ( Wnlib.Lexeme lx in ss.words) { outstr += FormatLexeme(lx.word) + ", "; } // remove last comma outstr = Strings.Mid(outstr, 1, outstr.Length - 2); outstr += ": "; // show the definition outstr += FormatDefn(ss.defn); // children if ((ss.senses != null)) { outstr += FormatWN(ss.senses); } outstr += "</li>"; } outstr += "</ul>"; return outstr; }
/// <summary> /// Standardises the addition of a new TreeNode to the TreeView. Lexemes are iterated and comma-separated /// for display purposes. An icon is attached to the node according to the type of the sense. /// </summary> /// <param name="ss"></param> /// <returns></returns> private TreeNode newTreeNode(Wnlib.SynSet ss) { //Wnlib.Lexeme word = default(Wnlib.Lexeme); string words = null; TreeNode childnode = null; words = ""; // Build the words for display in the node. // Words are the lexemes in a search result. foreach ( Wnlib.Lexeme word in ss.words) { if (!string.IsNullOrEmpty(words)) { words += ", "; } words += Strings.Replace(word.word, "_", " "); // append the sense number when the sense is not 1 if (word.wnsns != 1) { words += "(" + word.wnsns + ")"; } } // define a new child node childnode = new TreeNode(); childnode.Text = words; // assign an icon according to the ident number of the ptr // (see static void classinit() in util.cs) if ((ss.thisptr != null)) { childnode.ImageIndex = ss.thisptr.ptp.ident; childnode.ToolTipText = ss.thisptr.ptp.label; } childnode.Tag = ss; return childnode; }
/// <summary> /// Fill all the child nodes. This is done to allow recursive child calls. /// </summary> /// <param name="ssarray">Search Sense Array</param> /// <param name="parentnode">Parent which the child is going to attach</param> private void fillTreeChild(Wnlib.SynSetList ssarray, TreeNode parentnode) { TreeNode childnode = null; //Wnlib.SynSet ss = default(Wnlib.SynSet); foreach ( Wnlib.SynSet ss in ssarray) { // define a new child node childnode = newTreeNode(ss); parentnode.Nodes.Add(childnode); // increase the depth of the tree via recursion if ((ss.senses != null)) { fillTreeChild(ss.senses, childnode); } } }
/// <summary> /// Fill the top level of the tree. /// </summary> /// <param name="sch"></param> /// <param name="opt">Currently a redundant parameter, but since it holds the search type, it can remain in case search type needs to be known.</param> /// <param name="tmppos"></param> private void fillTreeRoot(Wnlib.Search sch, ref TreeNode posnode) { // do the treeview //Wnlib.SynSet ss = default(Wnlib.SynSet); TreeNode parentnode = null; // iterate the returned senses foreach ( Wnlib.SynSet ss in sch.senses) { parentnode = newTreeNode(ss); // if a part of speech node has been created // then it becomes the top level if ((posnode != null)) { posnode.Nodes.Add(parentnode); } else { TreeView1.Nodes.Add(parentnode); } // do child senses if ((ss.senses != null)) { fillTreeChild(ss.senses, parentnode); } // fill in sense frames //Wnlib.SynSetFrame fr = default(Wnlib.SynSetFrame); TreeNode frnode = null; if (ss.frames.Count > 0) { foreach ( Wnlib.SynSetFrame fr in ss.frames) { frnode = new TreeNode(fr.fr.str); frnode.ImageIndex = 38; parentnode.Nodes.Add(frnode); } } } }