private void getRelatedSynSets_Click(object sender, EventArgs e) { SynSet selectedSynSet = synSets.SelectedItem as SynSet; if (selectedSynSet == null || synSetRelations.SelectedIndex == -1) { return; } synSets.Items.Clear(); // get relations string relationStr = synSetRelations.SelectedItem.ToString(); relationStr = relationStr.Split(':')[0].Trim(); WordNetEngine.SynSetRelation relation = (WordNetEngine.SynSetRelation)Enum.Parse(typeof(WordNetEngine.SynSetRelation), relationStr); // add related synset foreach (SynSet relatedSynset in selectedSynSet.GetRelatedSynSets(relation, false)) { synSets.Items.Add(relatedSynset); } // selected synset if (synSets.Items.Count > 0) { synSets.SelectedIndex = 0; } }
/// <summary> /// Gets the number of synsets related to the current one by the given relation /// </summary> /// <param name="relation">Relation to check</param> /// <returns>Number of synset related to the current one by the given relation</returns> public int GetRelatedSynSetCount(WordNetEngine.SynSetRelation relation) { if (!_relationSynSets.ContainsKey(relation)) { return(0); } return(_relationSynSets[relation].Count); }
//////////////////////////////////////////////////////////////////////////////////////////////// /*--------------------------------------------------------------------------------------------*/ private void InsertFactors(ISession pSess, WordNetEngine.SynSetRelation pRel, DescriptorTypeId pDescTypeId, int?pDescTypeRefineWordId = null, bool pIsDef = false) { Console.WriteLine("Loading " + pRel + " Lexicals..."); IList <Lexical> lexList = pSess.QueryOver <Lexical>() .Where(x => x.RelationId == (byte)pRel) .List(); Console.WriteLine("Found " + lexList.Count + " " + pRel + " Lexicals" + TimerString()); using (ITransaction tx = pSess.BeginTransaction()) { Console.WriteLine("Building Factors..."); var oppMap = new HashSet <string>(); int oppSkips = 0; foreach (Lexical lex in lexList) { Artifact art = (vArtSet.WordIdMap.ContainsKey(lex.Word.Id) ? vArtSet.WordIdMap[lex.Word.Id] : vArtSet.SynsetIdMap[lex.Synset.Id]); Artifact targArt = (vArtSet.WordIdMap.ContainsKey(lex.TargetWord.Id) ? vArtSet.WordIdMap[lex.TargetWord.Id] :vArtSet.SynsetIdMap[lex.TargetSynset.Id]); if (oppMap.Contains(targArt.Id + "|" + art.Id)) { oppSkips++; continue; //avoid creating a B->A "duplicate" of an existing A->B Factor } oppMap.Add(art.Id + "|" + targArt.Id); var f = new Factor(); f.Lexical = lex; f.PrimaryArtifact = art; f.RelatedArtifact = targArt; f.IsDefining = pIsDef; f.DescriptorTypeId = (byte)pDescTypeId; f.AssertionId = (byte)FactorAssertionId.Fact; f.Note = "[" + art.Name + "] " + pDescTypeId + " [" + targArt.Name + "] {LEX." + pRel + "}"; if (pDescTypeRefineWordId != null) { f.DescriptorTypeRefine = vArtSet.WordIdMap[(int)pDescTypeRefineWordId]; } pSess.Save(f); } Console.WriteLine("Skipped " + oppSkips + " reversed Factors..." + TimerString()); Console.WriteLine("Comitting Factors..." + TimerString()); tx.Commit(); Console.WriteLine("Finished Factors" + TimerString()); Console.WriteLine(""); } }
/*--------------------------------------------------------------------------------------------*/ private void InsertAttributeFactors(ISession pSess) { const WordNetEngine.SynSetRelation att = WordNetEngine.SynSetRelation.Attribute; Console.WriteLine("Loading " + att + " Semantics..."); IList <Semantic> semList = pSess.QueryOver <Semantic>() .Where(x => x.RelationId == (byte)att) .Fetch(x => x.Synset).Eager .List(); Console.WriteLine("Found " + semList.Count + " " + att + " Semantics" + TimerString()); using (ITransaction tx = pSess.BeginTransaction()) { Console.WriteLine("Building Factors..."); foreach (Semantic sem in semList) { Artifact art = vArtSet.SynsetIdMap[sem.Synset.Id]; Artifact targArt = vArtSet.SynsetIdMap[sem.TargetSynset.Id]; if (sem.Synset.PartOfSpeechId != (byte)WordNetEngine.POS.Adjective && sem.TargetSynset.PartOfSpeechId != (byte)WordNetEngine.POS.Noun) { continue; } var f = new Factor(); f.Semantic = sem; f.PrimaryArtifact = art; f.RelatedArtifact = targArt; f.IsDefining = false; f.DescriptorTypeId = (byte)DescriptorTypeId.RefersTo; f.AssertionId = (byte)FactorAssertionId.Fact; f.Note = "[" + art.Name + "] " + DescriptorTypeId.RefersTo + " [" + targArt.Name + "] {" + att + "}"; pSess.Save(f); } Console.WriteLine("Comitting Factors..." + TimerString()); tx.Commit(); Console.WriteLine("Finished Factors" + TimerString()); Console.WriteLine(""); } }
/*--------------------------------------------------------------------------------------------*/ private void InsertFactors(ISession pSess, WordNetEngine.SynSetRelation pRel, DescriptorTypeId pDescTypeId, int?pDescTypeRefineWordId = null, bool pIsDef = false) { Console.WriteLine("Loading " + pRel + " Semantics..."); IList <Semantic> semList = pSess.QueryOver <Semantic>() .Where(x => x.RelationId == (byte)pRel) .List(); Console.WriteLine("Found " + semList.Count + " " + pRel + " Semantics" + TimerString()); using (ITransaction tx = pSess.BeginTransaction()) { Console.WriteLine("Building Factors..."); foreach (Semantic sem in semList) { Artifact art = vArtSet.SynsetIdMap[sem.Synset.Id]; Artifact targArt = vArtSet.SynsetIdMap[sem.TargetSynset.Id]; var f = new Factor(); f.Semantic = sem; f.PrimaryArtifact = art; f.RelatedArtifact = targArt; f.IsDefining = pIsDef; f.DescriptorTypeId = (byte)pDescTypeId; f.AssertionId = (byte)FactorAssertionId.Fact; f.Note = "[" + art.Name + "] " + pDescTypeId + " [" + targArt.Name + "] {" + pRel + "}"; if (pDescTypeRefineWordId != null) { f.DescriptorTypeRefine = vArtSet.WordIdMap[(int)pDescTypeRefineWordId]; } pSess.Save(f); } Console.WriteLine("Comitting Factors..." + TimerString()); tx.Commit(); Console.WriteLine("Finished Factors" + TimerString()); Console.WriteLine(""); } }
/// <summary> /// Gets synsets related to the current synset /// </summary> /// <param name="relation">Synset relation to follow</param> /// <param name="recursive">Whether or not to follow the relation recursively for all related synsets</param> /// <returns>Synsets related to the given one by the given relation</returns> public Set <SynSet> GetRelatedSynSets(WordNetEngine.SynSetRelation relation, bool recursive) { return(GetRelatedSynSets(new WordNetEngine.SynSetRelation[] { relation }, recursive)); }
/// <summary> /// Instantiates the current synset. If idSynset is non-null, related synsets references are set to those from /// idSynset; otherwise, related synsets are created as shells. /// </summary> /// <param name="definition">Definition line of synset from data file</param> /// <param name="idSynset">Lookup for related synsets. If null, all related synsets will be created as shells.</param> internal void Instantiate(string definition, Dictionary <string, SynSet> idSynset) { // don't re-instantiate if (_instantiated) { throw new Exception("Synset has already been instantiated"); } /* get lexicographer file name...the enumeration lines up precisely with the wordnet spec (see the lexnames file) except that * it starts with None, so we need to add 1 to the definition line's value to get the correct file name */ int lexicographerFileNumber = int.Parse(GetField(definition, 1)) + 1; if (lexicographerFileNumber <= 0) { throw new Exception("Invalid lexicographer file name number. Should be >= 1."); } _lexicographerFileName = (WordNetEngine.LexicographerFileName)lexicographerFileNumber; // get number of words in the synset and the start character of the word list int wordStart; int numWords = int.Parse(GetField(definition, 3, out wordStart), NumberStyles.HexNumber); wordStart = definition.IndexOf(' ', wordStart) + 1; // get words in synset _words = new List <string>(numWords); for (int i = 0; i < numWords; ++i) { int wordEnd = definition.IndexOf(' ', wordStart + 1) - 1; int wordLen = wordEnd - wordStart + 1; string word = definition.Substring(wordStart, wordLen); if (word.Contains(' ')) { throw new Exception("Unexpected space in word: " + word); } _words.Add(word); // skip lex_id field wordStart = definition.IndexOf(' ', wordEnd + 2) + 1; } // get gloss _gloss = definition.Substring(definition.IndexOf('|') + 1).Trim(); if (_gloss.Contains('|')) { throw new Exception("Unexpected pipe in gloss"); } // get number and start of relations int relationCountField = 3 + (_words.Count * 2) + 1; int relationFieldStart; int numRelations = int.Parse(GetField(definition, relationCountField, out relationFieldStart)); relationFieldStart = definition.IndexOf(' ', relationFieldStart) + 1; // grab each related synset _relationSynSets = new Dictionary <WordNetEngine.SynSetRelation, Set <SynSet> >(); _lexicalRelations = new Dictionary <WordNetEngine.SynSetRelation, Dictionary <SynSet, Dictionary <int, Set <int> > > >(); for (int relationNum = 0; relationNum < numRelations; ++relationNum) { string relationSymbol = null; int relatedSynSetOffset = -1; WordNetEngine.POS relatedSynSetPOS = WordNetEngine.POS.None; int sourceWordIndex = -1; int targetWordIndex = -1; // each relation has four columns for (int relationField = 0; relationField <= 3; ++relationField) { int fieldEnd = definition.IndexOf(' ', relationFieldStart + 1) - 1; int fieldLen = fieldEnd - relationFieldStart + 1; string fieldValue = definition.Substring(relationFieldStart, fieldLen); // relation symbol if (relationField == 0) { relationSymbol = fieldValue; } // related synset offset else if (relationField == 1) { relatedSynSetOffset = int.Parse(fieldValue); } // related synset POS else if (relationField == 2) { relatedSynSetPOS = GetPOS(fieldValue); } // source/target word for lexical relation else if (relationField == 3) { sourceWordIndex = int.Parse(fieldValue.Substring(0, 2), NumberStyles.HexNumber); targetWordIndex = int.Parse(fieldValue.Substring(2), NumberStyles.HexNumber); } else { throw new Exception(); } relationFieldStart = definition.IndexOf(' ', relationFieldStart + 1) + 1; } // get related synset...create shell if we don't have a lookup SynSet relatedSynSet; if (idSynset == null) { relatedSynSet = new SynSet(relatedSynSetPOS, relatedSynSetOffset, _wordNetEngine); } // look up related synset directly else { relatedSynSet = idSynset[relatedSynSetPOS + ":" + relatedSynSetOffset]; } // get relation WordNetEngine.SynSetRelation relation = WordNetEngine.GetSynSetRelation(_pos, relationSymbol); // add semantic relation if we have neither a source nor a target word index if (sourceWordIndex == 0 && targetWordIndex == 0) { _relationSynSets.EnsureContainsKey(relation, typeof(Set <SynSet>)); _relationSynSets[relation].Add(relatedSynSet); } // add lexical relation else { _lexicalRelations.EnsureContainsKey(relation, typeof(Dictionary <SynSet, Dictionary <int, Set <int> > >)); _lexicalRelations[relation].EnsureContainsKey(relatedSynSet, typeof(Dictionary <int, Set <int> >)); _lexicalRelations[relation][relatedSynSet].EnsureContainsKey(sourceWordIndex, typeof(Set <int>)); if (!_lexicalRelations[relation][relatedSynSet][sourceWordIndex].Contains(targetWordIndex)) { _lexicalRelations[relation][relatedSynSet][sourceWordIndex].Add(targetWordIndex); } } } // release the wordnet engine if we have one...don't need it anymore if (_wordNetEngine != null) { _wordNetEngine = null; } _instantiated = true; }
private void findLCS_Click(object sender, EventArgs e) { int found = 0; LinkBox.Items.Clear(); // retrive synsets Set <SynSet> synStartSet = null; try { synStartSet = _wordNetEngine.GetSynSets(StartWord.Text, (WordNetEngine.POS)pos.SelectedItem); } catch (Exception) { MessageBox.Show("Invalid Start SynSet ID"); return; } Set <SynSet> synDestSet = null; try { synDestSet = _wordNetEngine.GetSynSets(DestWord.Text, (WordNetEngine.POS)pos.SelectedItem); } catch (Exception) { MessageBox.Show("Invalid Dest SynSet ID"); return; } if (synStartSet.Count > 0) { WordNetEngine.SynSetRelation[] vlist = new WordNetEngine.SynSetRelation[1]; vlist[0] = WordNetEngine.SynSetRelation.Hypernym; //vlist[1] = WordNetEngine.SynSetRelation.InstanceHypernym; //vlist[2] = WordNetEngine.SynSetRelation.Hyponym; //vlist[3] = WordNetEngine.SynSetRelation.InstanceHyponym; foreach (SynSet synSrcSet in synStartSet) { foreach (SynSet synDstSet in synDestSet) { //synSets.Items.Add(synSet); List <SynSet> linkageList = null; linkageList = synSrcSet.GetShortestPathTo(synDstSet, vlist); SynSet s = synSrcSet.GetClosestMutuallyReachableSynset(synDstSet, vlist); if (s != null) { StringBuilder desc = new StringBuilder(); desc.Append("{"); bool prependComma = false; foreach (string word in s.Words) { desc.Append((prependComma ? ", " : "") + word); prependComma = true; } desc.Append("}"); LinkBox.Items.Add(desc.ToString()); LinkBox.Text = desc.ToString(); found++; //return; } } } if (found == 0) { LinkBox.Text = "false"; } } else { LinkBox.Text = "false"; // MessageBox.Show("No synsets found"); } }
/*--------------------------------------------------------------------------------------------*/ public void AddLexical(WordNetEngine.SynSetRelation pRel, SemanticNode pTargNode) { Lexicals[pRel].Add(pTargNode); }
/*--------------------------------------------------------------------------------------------*/ public void AddRelation(WordNetEngine.SynSetRelation pRel, SemanticNode pTargNode) { Relations[pRel].Add(pTargNode); }
public WordNetRelation(WordNetEngine.SynSetRelation relation, List <string> words) { Relation = relation; Words = words; }
public WordNetRelation(WordNetEngine.SynSetRelation relation) { Relation = relation; Words = null; }
/// <summary> /// Instantiates the current synset. If idSynset is non-null, related synsets references are set to those from /// idSynset; otherwise, related synsets are created as shells. /// </summary> /// <param name="definition">Definition line of synset from data file</param> /// <param name="idSynset">Lookup for related synsets. If null, all related synsets will be created as shells.</param> internal void Instantiate(string definition, Dictionary <string, SynSet> idSynset) { // don't re-instantiate if (!_instantiated) { // get number of words in the synset and the start character of the word list int wordStart; int numWords = int.Parse(GetField(definition, 3, out wordStart), NumberStyles.HexNumber); wordStart = definition.IndexOf(' ', wordStart) + 1; _words = new List <string>(numWords); // get words in synset for (int i = 0; i < numWords; ++i) { int wordEnd = definition.IndexOf(' ', wordStart + 1) - 1; int wordLen = wordEnd - wordStart + 1; string word = definition.Substring(wordStart, wordLen); _words.Add(word); // get lex_id lex_id = Convert.ToInt32(definition.Substring(definition.IndexOf(' ') + 1, 2)); // skip lex_id field wordStart = definition.IndexOf(' ', wordEnd + 2) + 1; } // get gloss _gloss = definition.Substring(definition.IndexOf('|') + 1).Trim(); // get number and start of relations int relationCountField = 3 + (_words.Count * 2) + 1; int relationFieldStart; int numRelations = int.Parse(GetField(definition, relationCountField, out relationFieldStart)); relationFieldStart = definition.IndexOf(' ', relationFieldStart) + 1; // grab each related synset _relationSynSets = new Dictionary <WordNetEngine.SynSetRelation, List <SynSet> >(); _lexicalRelations = new Dictionary <WordNetEngine.SynSetRelation, Dictionary <SynSet, Dictionary <int, List <int> > > >(); for (int relationNum = 0; relationNum < numRelations; ++relationNum) { string relationSymbol = null; int relatedSynSetOffset = -1; WordNetEngine.POS relatedSynSetPOS = WordNetEngine.POS.None; int sourceWordIndex = -1; int targetWordIndex = -1; // each relation has four columns for (int relationField = 0; relationField <= 3; ++relationField) { int fieldEnd = definition.IndexOf(' ', relationFieldStart + 1) - 1; int fieldLen = fieldEnd - relationFieldStart + 1; string fieldValue = definition.Substring(relationFieldStart, fieldLen); // relation symbol if (relationField == 0) { relationSymbol = fieldValue; } // related synset offset else if (relationField == 1) { relatedSynSetOffset = int.Parse(fieldValue); } // related synset POS else if (relationField == 2) { relatedSynSetPOS = GetPOS(fieldValue); } // source/target word for lexical relation else if (relationField == 3) { sourceWordIndex = int.Parse(fieldValue.Substring(0, 2), NumberStyles.HexNumber); targetWordIndex = int.Parse(fieldValue.Substring(2), NumberStyles.HexNumber); } else { throw new Exception(); } relationFieldStart = definition.IndexOf(' ', relationFieldStart + 1) + 1; } // get related synset...create shell if we don't have a lookup SynSet relatedSynSet; if (idSynset == null) { relatedSynSet = new SynSet(relatedSynSetPOS, relatedSynSetOffset, _wordNetEngine); } // look up related synset directly else { relatedSynSet = idSynset[relatedSynSetPOS + ":" + relatedSynSetOffset]; } // get relation WordNetEngine.SynSetRelation relation = WordNetEngine.GetSynSetRelation(_pos, relationSymbol); // add semantic relation if we have neither a source nor a target word index if (sourceWordIndex == 0 && targetWordIndex == 0) { _relationSynSets.EnsureContainsKey(relation, typeof(List <SynSet>)); _relationSynSets[relation].Add(relatedSynSet); } // add lexical relation else { _lexicalRelations.EnsureContainsKey(relation, typeof(Dictionary <SynSet, Dictionary <int, List <int> > >)); _lexicalRelations[relation].EnsureContainsKey(relatedSynSet, typeof(Dictionary <int, List <int> >)); _lexicalRelations[relation][relatedSynSet].EnsureContainsKey(sourceWordIndex, typeof(List <int>)); if (!_lexicalRelations[relation][relatedSynSet][sourceWordIndex].Contains(targetWordIndex)) { _lexicalRelations[relation][relatedSynSet][sourceWordIndex].Add(targetWordIndex); } } } _instantiated = true; } // release the wordnet engine if we have one...don't need it anymore if (_wordNetEngine != null) { _wordNetEngine = null; } }