Esempio n. 1
0
 /// <summary>
 /// 意图分析
 /// </summary>
 private void meaningAnalysis(string str)
 {
     if (isAnswerDeal(str))
     {
         //回答上文的问题
     }
     else if (isSpecialDeal(str))
     {
         //特殊词汇
     }
     else if (isQuestionDeal(str))
     {
         //疑问句
         if (LogicAskDeal(str))
         {
             //从逻辑中找到答案
         }
         else if (getWebsiteAnswer(str))
         {
             //答案从百度获取
         }
         else
         {
             //百度没有这个答案
             tmpOutputSentence.Add("这个……我不知道");
             tmpOutputSentence.Add("你觉得呢?");
             askConcept = new ConceptUnit(tmpConcepts.First());
         }
     }
     else
     {
         //分析句子逻辑
         LogicTellDeal(str);
     }
 }
Esempio n. 2
0
        private ConceptUnit searchConcept(ConceptUnit con, bool matchObj = false)
        {
            for (int i = concepts.Count - 1; i >= 0; i--)
            {
                ConceptUnit c = concepts[i];
                //主语
                string sbj1 = "";
                foreach (var p in c.asub)
                {
                    sbj1 += p.word;
                }
                sbj1 += c.sub.word;
                string sbj2 = "";
                foreach (var p in con.asub)
                {
                    sbj2 += p.word;
                }
                sbj2 += con.sub.word;
                //计算编辑距离
                int jl1 = LevenshteinDistance(sbj1, sbj2);
                if ((double)jl1 / Math.Min(sbj1.Length, sbj2.Length) > 0.2)
                {
                    continue;
                }

                //谓语
                if (!c.pred.word.Contains(con.pred.word) && !con.pred.word.Contains(c.pred.word))
                {
                    continue;
                }

                if (matchObj)
                {
                    //宾语
                    string obj1 = "";
                    foreach (var p in c.aobj)
                    {
                        obj1 += p.word;
                    }
                    obj1 += c.obj.word;
                    string obj2 = "";
                    foreach (var p in con.aobj)
                    {
                        obj2 += p.word;
                    }
                    obj2 += con.obj.word;
                    //计算编辑距离
                    int jl2 = LevenshteinDistance(obj1, obj2);
                    if ((double)jl2 / Math.Min(obj1.Length, obj2.Length) > 0.2)
                    {
                        continue;
                    }
                }


                return(c);
            }
            return(null);
        }
Esempio n. 3
0
 public ConceptUnit(ConceptUnit u)
 {
     if (u == null)
     {
         return;
     }
     sub   = u.sub;
     pred  = u.pred;
     obj   = u.obj;
     asub  = new List <Word>(u.asub.ToArray());
     apred = new List <Word>(u.apred.ToArray());
     aobj  = new List <Word>(u.aobj.ToArray());
 }
Esempio n. 4
0
        private bool isAnswerDeal(string str)
        {
            bool isa = false;

            if (askConcept == null)
            {
                return(isa);
            }


            string[] yeswords = { "改", "好", "行", "嗯", "ok", "OK", "哦" };
            string[] nowords  = { "不", "别", "no", "NO", "甭", "算了" };

            foreach (var w in nowords)
            {
                if (str.Contains(w))
                {
                    isa = true;
                    tmpOutputSentence.Add("哦,好吧。");
                    askConcept = null;
                    break;
                }
            }
            foreach (var w in yeswords)
            {
                if (str.Contains(w))
                {
                    isa = true;
                    tmpOutputSentence.Add("嗯,我记住了," + askConcept.toString());
                    var oldc = searchConcept(askConcept, true);
                    if (oldc != null)
                    {
                        int id = searchConcept(askConcept, true).id;
                        removeConceptById(id);
                    }
                    concepts.Add(new ConceptUnit(askConcept));
                    askConcept = null;
                    break;
                }
            }

            return(isa);
        }
Esempio n. 5
0
 private void LogicTellDeal(string str)
 {
     foreach (var w in tmpConcepts)
     {
         ConceptUnit cu = searchConcept(w, true);
         if (cu != null)
         {
             //tmpOutputSentence.Add("我记得:" + cu.toString() + "?");
             int not1 = 0;
             int not2 = 0;
             foreach (var predw in cu.apred)
             {
                 if (predw.word == "不")
                 {
                     not1++;
                 }
             }
             foreach (var predw in w.apred)
             {
                 if (predw.word == "不")
                 {
                     not2++;
                 }
             }
             if (not1 == not2)
             {
                 tmpOutputSentence.Add("嗯,是这样的。");
             }
             else
             {
                 tmpOutputSentence.Add("不,我记得" + cu.toString());
                 tmpOutputSentence.Add("唔,这件事是我记错了吗……要改了它?");
                 askConcept = new ConceptUnit(w);
             }
         }
         else
         {
             tmpOutputSentence.Add("好,我记住了," + w.toString());
             concepts.Add(w);
         }
     }
 }
Esempio n. 6
0
        public ChatController(sendChatMessageDelegate toutputEvent = null)
        {
            info = new ChatInfo();
            run  = false;

            nowWaitTime       = 0;
            nowWaitAllTime    = 0;
            tmpInputSentence  = new List <string>();
            tmpOutputSentence = new List <string>();

            concepts    = new List <ConceptUnit>();
            askConcept  = null;
            tmpConcepts = new List <ConceptUnit>();
            if (toutputEvent != null)
            {
                outputEvent = toutputEvent;
            }
            inputEvent = new sendChatMessageDelegate(input);

            baiduWordList = IOController.readBaiduWords("BaiduWord.txt");
        }
Esempio n. 7
0
        private bool LogicAskDeal(string str)
        {
            bool lga = false;

            foreach (var w in tmpConcepts)
            {
                //tmpOutputSentence.Add("我记得:" + cu.toString() + "?");
                bool     isAskWhat = false;
                string[] whatWords = { "什么", "谁", "哪" };
                foreach (var word in whatWords)
                {
                    if (w.obj.word.Contains(word))
                    {
                        isAskWhat = true;
                        break;
                    }
                }
                if (isAskWhat)
                {
                    //是在询问一件事情
                    ConceptUnit cu = searchConcept(w);
                    if (cu != null)
                    {
                        lga = true;
                        tmpOutputSentence.Add("我记得" + cu.toString());
                    }
                    else
                    {
                        lga = false;
                    }
                }
                else
                {
                    //是在证实一件事情
                    ConceptUnit cu = searchConcept(w, true);
                    if (cu != null)
                    {
                        lga = true;
                        int not1 = 0;
                        int not2 = 0;
                        foreach (var predw in cu.apred)
                        {
                            if (predw.word == "不")
                            {
                                not1++;
                            }
                        }
                        foreach (var predw in w.apred)
                        {
                            if (predw.word == "不")
                            {
                                not2++;
                            }
                        }
                        if (not1 == not2)
                        {
                            tmpOutputSentence.Add("是的。");
                        }
                        else
                        {
                            tmpOutputSentence.Add("不,我记得" + cu.toString());
                        }
                    }
                    else
                    {
                        lga = false;
                    }
                }
            }

            return(lga);
        }
Esempio n. 8
0
        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);
        }