public List <SynSet> GetLexicalRelationships(SynSetRelation relations, bool recursive) { List <SynSet> synsets = new List <SynSet>(); GetLexicalRelationships(synsets, relations, recursive); return(synsets); }
/// <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(SynSetRelation relation) { if (!relationSynSets.ContainsKey(relation)) { return(0); } return(relationSynSets[relation].Count); }
private void GetLexicalRelationships(List <SynSet> synsets, SynSetRelation relation, bool recursive) { if (!lexicalRelations.ContainsKey(relation)) { return; } foreach (var synset in lexicalRelations[relation].Keys) { // only add synset if it isn't already present (wordnet contains cycles) if (!synsets.Contains(synset)) { synsets.Add(synset); } } }
public double Measure(SynSet synSet1, SynSet synSet2) { if (synSet1 == null) { throw new ArgumentNullException(nameof(synSet1)); } if (synSet2 == null) { throw new ArgumentNullException(nameof(synSet2)); } var tag = GenerateTag(synSet1, synSet2); return(cache.GetOrCreate( tag, entry => { SynSetRelation[] vlist = new SynSetRelation[1]; vlist[0] = SynSetRelation.Hypernym; var common = synSet1.GetClosestMutuallyReachableSynset(synSet2, vlist); double distance = 0; if (common != null) { distance = resnik.GetIC(synSet1) + resnik.GetIC(synSet2) - 2 * resnik.GetIC(common); } if (distance == 0) { return 0; } if (distance < MinDistance) { distance = MinDistance; } return 1 / distance; })); }
/// <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 List <SynSet> GetRelatedSynSets(SynSetRelation relation, bool recursive) { return(GetRelatedSynSets(new[] { relation }, recursive)); }
/// <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(SynSetRelation relation) { return(relationSynSets.ContainsKey(relation) ? relationSynSets[relation].Count : 0); }
/// <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 = (LexicographerFileName)lexicographerFileNumber; // get number of words in the synset and the start character of the word list int numWords = int.Parse(GetField(definition, 3, out int 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 numRelations = int.Parse(GetField(definition, relationCountField, out int relationFieldStart)); relationFieldStart = definition.IndexOf(' ', relationFieldStart) + 1; // grab each related synset relationSynSets = new Dictionary <SynSetRelation, List <SynSet> >(); lexicalRelations = new Dictionary <SynSetRelation, Dictionary <SynSet, Dictionary <int, List <int> > > >(); for (int relationNum = 0; relationNum < numRelations; ++relationNum) { string relationSymbol = null; int relatedSynSetOffset = -1; WordType relatedSynSetPOS = WordType.Unknown; 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); } // look up related synset directly else { relatedSynSet = idSynset[relatedSynSetPOS + ":" + relatedSynSetOffset]; } // get relation 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) { var list = relationSynSets.GetItemCreate(relation); list.Add(relatedSynSet); } // add lexical relation else { var itemRelation = lexicalRelations.GetItemCreate(relation); var itemRelatedSynSet = itemRelation.GetItemCreate(relatedSynSet); var itemSourceWordIndex = itemRelatedSynSet.GetItemCreate(sourceWordIndex); if (!itemSourceWordIndex.Contains(targetWordIndex)) { itemSourceWordIndex.Add(targetWordIndex); } } } Instantiated = true; }
public string ExpandWeightedQuery(string level, string query) { string expandedquery = ""; WordNetEngine wordnet = new WordNetEngine(); var directory = System.IO.Directory.GetCurrentDirectory(); Dictionary <string, string> thesaurus = new Dictionary <string, string>(); string path = directory + "\\wordnet\\"; wordnet.LoadFromDirectory(path); if (wordnet.IsLoaded) { char[] delimiter = { ' ', ';' }; string[] querylist = query.Split(delimiter, StringSplitOptions.RemoveEmptyEntries); //separate the text inputed into several parts; foreach (string item in querylist) { var synSetList = wordnet.GetSynSets(item); //create a set of synonyms for the item if (synSetList.Count != 0) //if there is synonym, it continues to do the next things { Dictionary <string, int> uniqueword = new Dictionary <string, int>(); foreach (SynSet syns in synSetList) { //syns.Words is a list not a string foreach (string w in syns.Words) { if (uniqueword.ContainsKey(w)) { uniqueword[w] = uniqueword[w] + 1; } else { uniqueword.Add(w, 1); } } //if a user want to expand the query to a certian lexical level, such as hypernym... //if the level is not synonym,it means the uniqueword will be larger if (level != "Synonym") { SynSetRelation relation = (SynSetRelation)Enum.Parse(typeof(SynSetRelation), level); var relationsynset = syns.GetRelatedSynSets(relation, true); foreach (SynSet element in relationsynset) { foreach (string ite in element.Words) { if (uniqueword.ContainsKey(ite)) { uniqueword[ite] = uniqueword[ite] + 1; } else { uniqueword.Add(ite, 1); } } } } }//finish exploring all synonyms for a specific item, so can add them into the dictionary string lexical = ""; foreach (string w in uniqueword.Keys) { if (w != item) { lexical = lexical + " " + w; } } thesaurus.Add(item, lexical); } //this condition is there are synonyms } //end the loop for each item( item is actualy a query) foreach (string term in thesaurus.Keys) { expandedquery = expandedquery + " " + term + "^5" + thesaurus[term]; } }//this condition is that wordnet engine is loaded, if you change the database directory, it can't work. return(expandedquery); }
/// <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 List<SynSet> GetRelatedSynSets(SynSetRelation relation, bool recursive) { return GetRelatedSynSets(new[] { relation }, recursive); }
/// <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(SynSetRelation relation) { return relationSynSets.ContainsKey(relation) ? relationSynSets[relation].Count : 0; }