public int CompareTo(object obj) { int result; try { LtpWord info = obj as LtpWord; if (this.id > info.id) { result = 1; } else { result = -1; } return(result); } catch (Exception ex) { throw new Exception(ex.Message); } }
/// <summary> /// 将依存句法树转化为概念单元 /// </summary> /// <param name="words"></param> private void getConcepts(List <LtpWord> words) { LtpWord beginw = getWordByParent(words, -1).First(); tmpConcepts.Clear(); getConcept(words, beginw); for (int i = 0; i < tmpConcepts.Count; i++) { if (tmpConcepts[i].sub.word.Contains("你")) { tmpConcepts[i].sub.word = tmpConcepts[i].sub.word.Replace("你", "我"); } else if (tmpConcepts[i].sub.word.Contains("我")) { tmpConcepts[i].sub.word = tmpConcepts[i].sub.word.Replace("我", "你"); } if (tmpConcepts[i].obj.word.Contains("你")) { tmpConcepts[i].obj.word = tmpConcepts[i].obj.word.Replace("你", "我"); } else if (tmpConcepts[i].obj.word.Contains("我")) { tmpConcepts[i].obj.word = tmpConcepts[i].obj.word.Replace("我", "你"); } } //foreach (var c in tmpConcepts) //{ // string tmpstr = c.id + ":"; // foreach (var a in c.asub) tmpstr += a.word + " "; // tmpstr += c.sub.word + " "; // foreach (var a in c.apred) tmpstr += a.word + " "; // tmpstr += c.pred.word + " "; // foreach (var a in c.aobj) tmpstr += a.word + " "; // tmpstr += c.obj.word; // tmpOutputSentence.Add(tmpstr); //} }
private List <ConceptUnit> getConcept(List <LtpWord> wordlist, LtpWord beginWord) { List <ConceptUnit> res = new List <ConceptUnit>(); List <LtpWord> preds = new List <LtpWord>(); preds.Add(beginWord); List <LtpWord> preds2 = getWordByParent(wordlist, beginWord.id, "COO"); foreach (var w in preds2) { preds.Add(w); } List <LtpWord> lastsbvw = new List <LtpWord>(); foreach (LtpWord predword in preds) { ConceptUnit u = new ConceptUnit(); u.id = this.getNextConceptIndex(); //设置谓语 u.pred = new Word(predword); List <Word> apred = new List <Word>(); //将谓语的修饰成分加入 List <LtpWord> apredw = getSubTreeWordsByParent(wordlist, predword.id); foreach (var w in apredw) { apred.Add(new Word(w)); } //查询是否有主语 List <LtpWord> sbvw = getWordByParent(wordlist, predword.id, "SBV"); if (sbvw.Count > 0) { lastsbvw = sbvw; } if (lastsbvw.Count > 0) { //设置主语为上一个并列句的主语 u.sub = new Word(lastsbvw.First()); //设置主语修饰语 List <LtpWord> asbvw = getSubTreeWordsByParent(wordlist, lastsbvw.First().id); List <Word> asub = new List <Word>(); foreach (var w in asbvw) { asub.Add(new Word(w)); } u.asub = asub; } //查询是否有宾语 List <LtpWord> objw = getWordByParent(wordlist, predword.id, "OB"); if (objw.Count > 0) { //首先将IOB即间接宾语设为谓语的修饰语 foreach (var w in objw) { if (w.pos == "IOB") { apred.Add(new Word(w)); } } u.apred = apred; //然后针对其他宾语递归进行操作,并且根据宾语中心词词性选择是将其作为子句(记录id)还是作为实宾语 foreach (var w in objw) { if (w.pos != "IOB") { if (w.pos.Contains("n") || w.pos.Contains("a") || (getWordByParent(wordlist, w.id, "SBJ").Count <= 0 && getWordByParent(wordlist, w.id, "OB").Count <= 0)) { //名词或形容词,作为实宾语 u.obj = new Word(w); //设置宾语修饰语 List <Word> aobj = new List <Word>(); List <LtpWord> aobw = getSubTreeWordsByParent(wordlist, w.id); foreach (var tw in aobw) { aobj.Add(new Word(tw)); } u.aobj = aobj; break; } else { //将其作为子句进行递归 List <ConceptUnit> objConcepts = getConcept(wordlist, w); foreach (ConceptUnit obju in objConcepts) { //res.Add(obju); //concepts.Push(obju); if (u.obj.type.Length <= 0) { u.obj = new Word(obju.id.ToString(), "id"); } else { //复制一份u以继承分析过的主谓 ConceptUnit u2 = new ConceptUnit(u); u2.id = this.getNextConceptIndex(); //concepts.Push(u2); u2.obj = new Word(obju.id.ToString(), "id"); res.Add(u2); tmpConcepts.Add(u2); } } } } } } else { u.apred = apred; } //暂时只考虑复句的最内层。。 if (u.obj.type != "id") { tmpConcepts.Add(u); res.Add(u); } } return(res); }
public Word(LtpWord w) { this.word = w.cont; this.type = w.pos; }