private int InitialSentence(out MyWordInfo[] current) { current = new MyWordInfo[_myPos.Length]; int overall = 0; for (int i = 0; i < current.Length; i++) { if (_alterWord[i] != null && _alterWord[i].Length > 0) { for (int j = 0; j < _alterWord[i].Length; j++) { _selected[i] = j; _contextWords[i] = _alterWord[i][j]; if (ReadWordSenseInfo(i)) { int score = ComputeScore(i); overall += score; break; } } } } return(overall); }
public MyWordInfo[] FindSynonyms(ref MyWordInfo pos, bool includeMorphs) { pos.Word = pos.Word.ToLower(); Index index = Index.lookup(pos.Word, PartOfSpeech.of(pos.Pos)); if (index == null) { if (!includeMorphs) { return(null); } var morphs = new MorphStr(pos.Word, PartOfSpeech.of(pos.Pos)); string morph = ""; while ((morph = morphs.next()) != null) { index = Index.lookup(morph, PartOfSpeech.of(pos.Pos)); pos.Word = morph; if (index != null) { break; } } } return(index == null ? null : LookupCandidates(index, pos)); }
private void btnLoad_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { helper = new MindMapOntology(openFileDialog1.FileName); //MindMapConcept c1 = helper.Concepts["READ"]; //MindMapConcept c2 = helper.Concepts["GOLF"]; //DistanceInfo info = MindMapConcept.Distance(c1, c2); //MessageBox.Show(info.Distance.ToString()); foreach (string conceptName in helper.Concepts.Keys) { this.lboxChildren.Items.Add(conceptName); } MyWordInfo word = new MyWordInfo("accomplishment", PartsOfSpeech.Noun); word.Sense = 1; MindMapConcept concept = MindMapMapper.GetConcept(word, helper); if (concept == null) { MessageBox.Show("Test"); } else { MessageBox.Show(concept.Name); } } }
private void Generate() { _alterWord = new MyWordInfo[_myPos.Length][]; _numWord = _myPos.Length; dx = new int[_numWord][]; _selected = new int[_numWord]; for (int i = 0; i < _myPos.Length; i++) { _selected[i] = -1; MyWordInfo pos = _myPos[i]; if (pos.Pos != PartsOfSpeech.Unknown && pos.Sense != -1) { _alterWord[i] = FindSynonyms(ref pos, true); if (_alterWord[i] != null) { foreach (MyWordInfo poss in _alterWord[i]) { poss.Pos = pos.Pos; } dx[i] = new int[_alterWord[i].Length]; _dictInfo[i] = new string[_alterWord[i].Length][][]; } } } _scores = new int[_myPos.Length][][][]; for (int i = 0; i < _myPos.Length; i++) { if (_alterWord[i] != null) { _scores[i] = new int[_alterWord[i].Length][][]; for (int a_i = 0; a_i < _alterWord[i].Length; a_i++) { _scores[i][a_i] = new int[_myPos.Length][]; for (int j = 0; j < _myPos.Length; j++) { if (_alterWord[j] != null) { _scores[i][a_i][j] = new int[_alterWord[j].Length]; } } } } } _numItems = 0; list.Clear(); bestScore = 0; bestSentence = string.Empty; IterativeGenerate(100, 0.9F); //TryAll (0); list.Insert(0, bestSentence); }
List <MyWordInfo> GetMaplex(IOwlLiteral conceptLiteral) { try { OwlNode nodeClass = (OwlNode)conceptLiteral.ParentEdges[0].ParentNode; string Maplex = nodeClass.ChildEdges[eMaplex, 0].ChildNode.ID.Split('^')[0]; string[] allMaplexes = Maplex.Split('\n'); List <MyWordInfo> result = new List <MyWordInfo>(); for (int i = 0; i < allMaplexes.Length; i++) { MyWordInfo word = new MyWordInfo(); string[] parts = allMaplexes[i].Split('-'); word.Word = parts[0]; //TODO: handle the adjective and adverb switch (parts[1][0]) { case 'n': case 'N': word.Pos = PartsOfSpeech.Noun; break; case 'v': case 'V': word.Pos = PartsOfSpeech.Verb; break; case 'a': case 'A': if (parts[1][2] == 'j' || parts[1][2] == 'J') { word.Pos = PartsOfSpeech.Adj; } else { word.Pos = PartsOfSpeech.Adv; } break; default: word.Pos = PartsOfSpeech.Unknown; break; } int index = 1; for (; parts[1][index] < '0' || parts[1][index] > '9'; index++) { ; } string senseNumber = parts[1].Substring(index); word.Sense = int.Parse(senseNumber); result.Add(word); } return(result); } catch (Exception) { return(new List <MyWordInfo>()); } }
private void Add_Sentence(int score) { ++_numItems; MyWordInfo[] pos = new MyWordInfo[_myPos.Length]; string newsen = ""; string[] words = new string[_numWord]; for (int i = 0; i < _numWord; i++) { string word; word = _selected[i] == -1 ? _myPos[i].Word : _alterWord[i][_selected[i]].Word; words[i] = word; pos[i] = new MyWordInfo(word, _myPos[i].Pos); } newsen = string.Format(_originalSentence, words); if (score > bestScore) { bestSentence = newsen + " " + score; bestScore = score; } if (k_best_sentence.Count < 500) { newsen = newsen + " " + score; if (!k_best_sentence.Contains(newsen)) { k_best_sentence.Add(newsen); k_best_score.Add(score); } } else { int min = 10000000; int rem = -1; for (int j = 0; j < k_best_sentence.Count; j++) { if ((int)k_best_score[j] < min) { min = (int)k_best_score[j]; rem = j; } } newsen = newsen + " " + score; if (!k_best_sentence.Contains(newsen) && rem != -1) { k_best_sentence.RemoveAt(rem); k_best_score.RemoveAt(rem); k_best_sentence.Add(newsen); k_best_score.Add(score); } } }
public static MindMapConcept GetConcept(MyWordInfo word,MindMapOntology ontology) { foreach (KeyValuePair<string,MindMapConcept> pair in ontology.Concepts) { for (int i = 0; i < pair.Value.Maplex.Count; i++) { MyWordInfo maplex = pair.Value.Maplex[i]; if (maplex.Pos==word.Pos&&maplex.Word.ToUpper()==word.Word.ToUpper()&&maplex.Sense==word.Sense) return pair.Value; } } return null; }
void FilterMeaningRepresentationForDrawing() { for (int i = 0; i < _TMR.Nounframes.Count; i++) { NounFrame nf = _TMR.Nounframes[i]; if (nf.Ownerof.ContainsKey(CaseRole.Possession)) { List <NounFrame> Pnfs = nf.Ownerof[CaseRole.Possession]; for (int j = 0; j < Pnfs.Count; j++) { NounFrame nfj = Pnfs[j]; if (_TMR.HasNoRelationsExceptFrom(Pnfs[j], nf)) { MyWordInfo wi = new MyWordInfo(); wi.Word = nfj.Text; wi.Sense = nfj.ParseNode.SenseNo; //TODO : assign part of speach here //wi.Pos = bool found = false; foreach (MyWordInfo winfo in nf.AdjectivesInfo) { if (wi.Word == winfo.Word) { found = true; break; } } if (!found) { nf.AdjectivesInfo.Add(wi); } int index = _TMR.Nounframes.IndexOf(nfj); Pnfs.RemoveAt(j); j--; if (index != -1) { _TMR.Nounframes.RemoveAt(index); if (index < i) { i--; } } } } } } }
private void PrepareInputArrOfDisambiguation() { int counter = 0; MyWordInfo[] mwi = new MyWordInfo[WordInfoArr2.Count]; foreach (string s in WordInfoArr2) { string[] strarr = s.Split(':'); if (strarr[0] == "VING") { if (mwi[counter - 1].Pos == Wnlib.PartsOfSpeech.Verb) { mwi[counter] = new MyWordInfo(strarr[1], Wnlib.PartsOfSpeech.Verb); counter++; } else { mwi[counter] = new MyWordInfo(strarr[1], Wnlib.PartsOfSpeech.Noun); counter++; } } else if (strarr[0] == "N" || (strarr[0].Contains("NPP") || strarr[0].Contains("PPJ"))) { mwi[counter] = new MyWordInfo(strarr[1], Wnlib.PartsOfSpeech.Noun); counter++; } else if (strarr[0] == "V" || strarr[0] == "VPSP" || strarr[0] == "BE1") { mwi[counter] = new MyWordInfo(strarr[1], Wnlib.PartsOfSpeech.Verb); counter++; } else if (strarr[0].Contains("CPADJ") || strarr[0].Contains("ADJ")) { mwi[counter] = new MyWordInfo(strarr[1], Wnlib.PartsOfSpeech.Adj); counter++; } else if (strarr[0].Contains("PADV") || strarr[0].Contains("ADV")) { mwi[counter] = new MyWordInfo(strarr[1], Wnlib.PartsOfSpeech.Adv); counter++; } //MessageBox.Show((string)s); } size = 0; for (int k = 0; k < WordInfoArr2.Count; k++) { if (mwi[k] != null) { size++; } } mwi2 = new MyWordInfo[size]; for (int k = 0; k < size; k++) { mwi2[k] = mwi[k]; } }
private static MyWordInfo[] LookupCandidates(Index index, MyWordInfo pos) { if (pos.Sense < 0) { pos.Sense = 1; } SynSet synset = new Wnlib.SynSet(index.SynsetOffsets[pos.Sense - 1], index.PartOfSpeech, 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.SynsetOffsets[pos.Sense - 1]); } if (index.SynsetOffsets.Length > 1) { if (lexemes.Count <= 1) { for (int i = 0; i < index.SynsetOffsets.Length; i++) { synset = new SynSet(index.SynsetOffsets[i], index.PartOfSpeech, index.Wd, null, i); foreach (Lexeme obj in synset.words) { synIndex.Add(index.SynsetOffsets[i]); lexemes.Add(obj); } } } else { synset = new SynSet(index.SynsetOffsets[0], index.PartOfSpeech, 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.SynsetOffsets[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 {} } var 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 void ConstructMapping() { string concept = ""; string word = ""; int ID = -1; string senseNo = ""; string Sense = ""; string Pos = ""; WordOlogy WO = new WordOlogy(); ArrayList wordologyArr = new ArrayList(); int conceptcounter = 0; LoadOntology(); FileStream allConceptsFile = new FileStream(_ontologyDirectoryPath + @"\AllConcepts.txt", FileMode.Open); StreamReader allConceptsFileReader = new StreamReader(allConceptsFile); string _wordologyDirectoryPath = @"..\..\..\wordology\"; BinaryFormatter bf = new BinaryFormatter(); FileStream fs = new FileStream( _wordologyDirectoryPath + "\\wordology.txt", FileMode.Create); int indxWatcherconceptCounter = 0; int NoMapLexConcepts = 0; int CannotGetSenseExeption = 0; int AllSensesMapped = 0; while ((concept = allConceptsFileReader.ReadLine()) != null) { indxWatcherconceptCounter++; string Conceptpath = _ontologyDirectoryPath + @"\" + concept[0] + @"\" + concept; Concept C = (Concept)Onto[concept]; Property maplexProperty = C.FullProperties["ENGLISH1"]; List <MyWordInfo> maplexsenses = new List <MyWordInfo>(); MyWordInfo mwi = new MyWordInfo(); int NoOfSensesSucceeded = 0; if (maplexProperty != null) { for (int i = 0; i < maplexProperty.Fillers.Count; i++) { string tmp = maplexProperty.Fillers[i].ScalarFiller; char[] charr = new char[] { '-', '_' }; string[] splt = tmp.Split(charr); //there r fillers with no type & a-bomb masalan if (splt.Length > 1) { mwi = new MyWordInfo(); for (int k = 0; k < splt.Length - 2; k++) { mwi.Word += splt[k] + " "; } mwi.Word += splt[splt.Length - 2]; if (splt[splt.Length - 1].Length == 2) { if (splt[splt.Length - 1][0] == 'v') { mwi.Pos = Wnlib.PartsOfSpeech.Verb; } else if (splt[splt.Length - 1][0] == 'n') { mwi.Pos = Wnlib.PartsOfSpeech.Noun; } else if (splt[splt.Length - 1][0] == 'a') { mwi.Pos = Wnlib.PartsOfSpeech.Adj; } else if (splt[splt.Length - 1][0] == 'r') { mwi.Pos = Wnlib.PartsOfSpeech.Adv; } else { mwi.Pos = Wnlib.PartsOfSpeech.Unknown; } } else { mwi.Pos = Wnlib.PartsOfSpeech.Unknown; mwi.Word += " " + splt[splt.Length - 1]; } if (i == 0 || (maplexsenses.Count > 0 && (mwi.Word != maplexsenses[maplexsenses.Count - 1].Word || mwi.Pos != maplexsenses[maplexsenses.Count - 1].Pos))) { maplexsenses.Add(mwi); } } //ne loop 3al ontology kolaha } if (maplexsenses.Count > 0) { MyWordInfo[] maplexArray = new MyWordInfo[maplexsenses.Count]; for (int j = 0; j < maplexsenses.Count; j++) { maplexArray[j] = maplexsenses[j]; } WordSenseDisambiguator wsd = new WordSenseDisambiguator(); MyWordInfo[] res = new MyWordInfo[maplexArray.Length]; res = wsd.Disambiguate(maplexArray); int i = 0; foreach (MyWordInfo wi in res) { string tmp = maplexProperty.Fillers[i].ScalarFiller; char[] charr = new char[] { '-', '_' }; string[] splt = tmp.Split(charr); if (splt.Length > 1 && splt[splt.Length - 1].Length == 2) { WO.SenseNo = splt[splt.Length - 1]; } else { // "sense doesn't have POS"; } Wnlib.PartOfSpeech p = Wnlib.PartOfSpeech.of((Wnlib.PartsOfSpeech)wi.Pos); try { Wnlib.Index index = Wnlib.Index.lookup(wi.Word.ToLower(), p); SynSet sense = new SynSet(index, res[i].Sense, null); WO.Sense = sense.defn; AllSensesMapped++; NoOfSensesSucceeded++; try { WO.Pos = p.name; } catch { WO.Pos = wi.Pos.ToString(); } ID++; WO.Word = wi.Word; WO.ID = ID; WO.Concept = concept; WO.Word = word; } catch { }; if (NoOfSensesSucceeded == 0) { CannotGetSenseExeption++; } i++; // bf.Serialize(fs, "\n" + WO); wordologyArr.Add(WO); } conceptcounter++; } } else { NoMapLexConcepts++; //new part Wnlib.Index index; Wnlib.PartOfSpeech p; Search se; try { index = Wnlib.Index.lookup(concept.ToLower(), PartOfSpeech.of(PartsOfSpeech.Noun)); if (index != null) { WO.Pos = "noun"; Opt[] relatedness = WordsMatching.Relatedness.GetRelatedness(PartsOfSpeech.Noun); foreach (Opt o in relatedness) { for (int senseNumber = 0; senseNumber < index.sense_cnt; senseNumber++) { se = new Search(concept, true, PartOfSpeech.of("noun"), o.sch, senseNumber); SynSet sense = new SynSet(index, senseNumber, se); WO.Concept = concept; WO.Word = concept; WO.Sense = sense.defn; WO.ID = ID; ID++; NoOfSensesSucceeded++; AllSensesMapped++; //bf.Serialize(fs, "\n" + WO); wordologyArr.Add(WO); } } } } catch { } try { index = Wnlib.Index.lookup(concept.ToLower(), PartOfSpeech.of(PartsOfSpeech.Verb)); if (index != null) { WO.Pos = "verb"; Opt[] relatedness = WordsMatching.Relatedness.GetRelatedness(PartsOfSpeech.Verb); foreach (Opt o in relatedness) { for (int senseNumber = 0; senseNumber < index.sense_cnt; senseNumber++) { se = new Search(concept, true, PartOfSpeech.of("verb"), o.sch, senseNumber); SynSet sense = new SynSet(index, senseNumber, se); WO.Sense = sense.defn; WO.Concept = concept; WO.Word = concept; WO.ID = ID; ID++; NoOfSensesSucceeded++; AllSensesMapped++; //bf.Serialize(fs, "\n" + WO); wordologyArr.Add(WO); } } } } catch { } try { index = Wnlib.Index.lookup(concept.ToLower(), PartOfSpeech.of(PartsOfSpeech.Adj)); if (index != null) { WO.Pos = "adj"; Opt[] relatedness = WordsMatching.Relatedness.GetRelatedness(PartsOfSpeech.Adj); foreach (Opt o in relatedness) { for (int senseNumber = 0; senseNumber < index.sense_cnt; senseNumber++) { se = new Search(concept, true, PartOfSpeech.of("adj"), o.sch, senseNumber); SynSet sense = new SynSet(index, senseNumber, se); WO.Sense = sense.defn; WO.Concept = concept; WO.Word = concept; WO.ID = ID; ID++; NoOfSensesSucceeded++; AllSensesMapped++; //bf.Serialize(fs, "\n" + WO); wordologyArr.Add(WO); } } } } catch { } try { index = Wnlib.Index.lookup(concept.ToLower(), PartOfSpeech.of(PartsOfSpeech.Adv)); if (index != null) { WO.Pos = "adv"; Opt[] relatedness = WordsMatching.Relatedness.GetRelatedness(PartsOfSpeech.Noun); foreach (Opt o in relatedness) { for (int senseNumber = 0; senseNumber < index.sense_cnt; senseNumber++) { se = new Search(concept, true, PartOfSpeech.of("adv"), o.sch, senseNumber); SynSet sense = new SynSet(index, senseNumber, se); WO.Sense = sense.defn; WO.Concept = concept; WO.Word = concept; WO.ID = ID; ID++; NoOfSensesSucceeded++; AllSensesMapped++; //bf.Serialize(fs, "\n" + WO); wordologyArr.Add(WO); } } } } catch { } if (NoOfSensesSucceeded != 0) { conceptcounter++; } } }//end while allConceptsFileReader.Close(); allConceptsFile.Close(); bf.Serialize(fs, wordologyArr); fs.Close(); MessageBox.Show("no map-lex concepts number = " + NoMapLexConcepts.ToString()); MessageBox.Show("can't getsense pos number = " + CannotGetSenseExeption.ToString()); MessageBox.Show(conceptcounter.ToString()); }
private void MapConceptsWithMapLex(string concept, Property maplexProperty) { MyWordInfo mwi; WordOlogy WO = new WordOlogy(); List <MyWordInfo> maplexsenses = new List <MyWordInfo>(); int NoOfSensesSucceeded = 0; for (int i = 0; i < maplexProperty.Fillers.Count; i++) { string tmp = maplexProperty.Fillers[i].ScalarFiller; char[] charr = new char[] { '-', '_' }; string[] splt = tmp.Split(charr); //there r fillers with no type & a-bomb masalan if (splt.Length > 1) { mwi = new MyWordInfo(); for (int k = 0; k < splt.Length - 2; k++) { mwi.Word += splt[k] + " "; } mwi.Word += splt[splt.Length - 2]; if (splt[splt.Length - 1].Length == 2) { if (splt[splt.Length - 1][0] == 'v') { mwi.Pos = Wnlib.PartsOfSpeech.Verb; } else if (splt[splt.Length - 1][0] == 'n') { mwi.Pos = Wnlib.PartsOfSpeech.Noun; } else if (splt[splt.Length - 1][0] == 'a') { mwi.Pos = Wnlib.PartsOfSpeech.Adj; } else if (splt[splt.Length - 1][0] == 'r') { mwi.Pos = Wnlib.PartsOfSpeech.Adv; } else { mwi.Pos = Wnlib.PartsOfSpeech.Unknown; } } else { mwi.Pos = Wnlib.PartsOfSpeech.Unknown; mwi.Word += " " + splt[splt.Length - 1]; } if (i == 0 || (maplexsenses.Count > 0 && (mwi.Word != maplexsenses[maplexsenses.Count - 1].Word || mwi.Pos != maplexsenses[maplexsenses.Count - 1].Pos))) { maplexsenses.Add(mwi); } } //ne loop 3al ontology kolaha } if (maplexsenses.Count > 0) { MyWordInfo[] maplexArray = new MyWordInfo[maplexsenses.Count]; for (int j = 0; j < maplexsenses.Count; j++) { maplexArray[j] = maplexsenses[j]; } WordSenseDisambiguator wsd = new WordSenseDisambiguator(); MyWordInfo[] res = new MyWordInfo[maplexArray.Length]; res = wsd.Disambiguate(maplexArray); int i = 0; foreach (MyWordInfo wi in res) { string tmp = maplexProperty.Fillers[i].ScalarFiller; char[] charr = new char[] { '-', '_' }; string[] splt = tmp.Split(charr); if (splt.Length > 1 && splt[splt.Length - 1].Length == 2) { WO.SenseNo = splt[splt.Length - 1]; } else { // "sense doesn't have POS"; } Wnlib.PartOfSpeech p = Wnlib.PartOfSpeech.of((Wnlib.PartsOfSpeech)wi.Pos); try { Wnlib.Index index = Wnlib.Index.lookup(wi.Word.ToLower(), p); SynSet sense = new SynSet(index, res[i].Sense, null); WO.Sense = sense.defn; // AllSensesMapped++; NoOfSensesSucceeded++; try { WO.Pos = p.name; } catch { WO.Pos = wi.Pos.ToString(); } ID++; WO.Word = wi.Word; WO.ID = ID; WO.Concept = concept; wordologyArr.Add(WO); } catch { }; if (NoOfSensesSucceeded == 0) { CannotGetSenseExeption++; } i++; //bf.Serialize(fs, "\n" + WO); } conceptcounter++; } }