Пример #1
0
        public static AnalyzeResult Analyze(string problem, string[] answerArr)
        {
            bool oppose = Regex.IsMatch(problem, "不是|不属于|不包括|不可以|不包含|不需要|错误|没有");//是否存在否定关键词

            //移除部分干扰搜索的关键字
            problem = RemoveUselessInfo(problem);

            #region 多线程获取信息
            Thread[] workThread = new Thread[1 + answerArr.Length * 2];//工作线程

            workThread[0] = new Thread(new ParameterizedThreadStart(WorkThread));
            WorkArgs args;

            args.Type  = TaskType.GetProblemData;
            args.Index = -1;
            args.Text  = problem;
            workThread[0].Start(args);

            answerCnt = new int[answerArr.Length];
            for (int i = 0; i < answerArr.Length; i++)
            {
                args.Type         = TaskType.AnswerCnt;
                args.Index        = i;
                args.Text         = answerArr[i];
                workThread[i + 1] = new Thread(new ParameterizedThreadStart(WorkThread));
                workThread[i + 1].Start(args);
            }

            problemAndAnswerCnt = new int[answerArr.Length];
            for (int i = 0; i < answerArr.Length; i++)
            {
                args.Type  = TaskType.ProblemAndAnswerCnt;
                args.Index = i;
                args.Text  = problem + " " + "\"" + answerArr[i] + "\"";
                workThread[i + 1 + answerArr.Length] = new Thread(new ParameterizedThreadStart(WorkThread));
                workThread[i + 1 + answerArr.Length].Start(args);
            }

            while (true)//等待所有线程执行完毕
            {
                bool allExited = true;
                for (int i = 0; i < workThread.Length; i++)
                {
                    if (workThread[i].IsAlive)
                    {
                        allExited = false;
                        break;
                    }
                }

                if (allExited)
                {
                    break;
                }
                else
                {
                    Thread.Sleep(50);
                }
            }
            #endregion

            //计算权值
            double[] pmiRank = CalculateRank.CalculatePMIRank(problemCnt, answerCnt, problemAndAnswerCnt);
            double[] cntRank = CalculateRank.CalculateCountRank(problem, answerArr, pmiRank, problemData);
            double[] sumRank = new double[answerArr.Length];

            int maxIndex = 0;
            int minIndex = 0;
            for (int i = 0; i < answerArr.Length; i++)
            {
                sumRank[i] = pmiRank[i] + cntRank[i];
                if (sumRank[i] > sumRank[maxIndex])
                {
                    maxIndex = i;
                }

                if (sumRank[i] < sumRank[minIndex])
                {
                    minIndex = i;
                }
            }

            double probabilitySum = 0;

            for (int i = 0; i < cntRank.Length; i++)
            {
                probabilitySum += sumRank[i];
            }
            //计算概率
            int[] probability = new int[answerArr.Length];
            for (int i = 0; i < answerArr.Length; i++)
            {
                probability[i] = (int)(sumRank[i] / probabilitySum * 100);
            }

            AnalyzeResult ar;
            ar.CntRank = cntRank;
            ar.PMIRank = pmiRank;
            ar.Index   = maxIndex;
            if (oppose)
            {
                ar.Index = minIndex;
            }
            ar.SumRank     = sumRank;
            ar.Probability = probability;
            ar.Oppose      = oppose;
            return(ar);
        }
Пример #2
0
        /// <summary>
        /// 答案推荐算法
        /// </summary>
        /// <param name="problem">题目</param>
        /// <param name="answerArr">选项</param>
        public static AnalyzeResult Analyze(string problem, string[] answerArr)
        {
            //是否存在否定关键词
            bool oppose = Regex.IsMatch(problem, "不是|不属于|不包括|不可以|不包含|不需要|错误|没有|不会|不可能|找不到");

            answers = answerArr;
            //移除部分干扰搜索的关键字
            problem = RemoveUselessInfoAndPrivative(problem);

            #region 多线程获取信息
            Thread[] workThread = new Thread[1 + answerArr.Length * 2];//工作线程

            workThread[0] = new Thread(new ParameterizedThreadStart(WorkThread));
            WorkArgs args;

            args.Type  = TaskType.GetProblemData;
            args.Index = -1;
            args.Text  = problem;
            workThread[0].Start(args);

            answerCnt = new int[answerArr.Length];
            for (int i = 0; i < answerArr.Length; i++)
            {
                args.Type         = TaskType.AnswerCnt;
                args.Index        = i;
                args.Text         = answerArr[i];
                workThread[i + 1] = new Thread(new ParameterizedThreadStart(WorkThread));
                workThread[i + 1].Start(args);
            }

            problemAndAnswerCnt = new int[answerArr.Length];
            for (int i = 0; i < answerArr.Length; i++)
            {
                args.Type  = TaskType.ProblemAndAnswerCnt;
                args.Index = i;
                args.Text  = problem + " " + "\"" + answerArr[i] + "\"";
                workThread[i + 1 + answerArr.Length] = new Thread(new ParameterizedThreadStart(WorkThread));
                workThread[i + 1 + answerArr.Length].Start(args);
            }

            while (true)//等待所有线程执行完毕
            {
                bool allExited = true;
                for (int i = 0; i < workThread.Length; i++)
                {
                    if (workThread[i].IsAlive)
                    {
                        allExited = false;
                        break;
                    }
                }

                if (allExited)
                {
                    break;
                }
                else
                {
                    Thread.Sleep(50);
                }
            }
            #endregion

            #region 计算权值

            double[] pmiRank = CalculateRank.CalculatePMIRank(problemCnt, answerCnt, problemAndAnswerCnt); //PMI算法得到的权值
            double[] cntRank = CalculateRank.CalculateCountRank(problem, answerArr, pmiRank, problemData); ///计算算法得到的权值
            double[] sumRank = new double[answerArr.Length];

            double pmiAddCnt = 0;//权值和
            for (int i = 0; i < answerArr.Length; i++)
            {
                pmiAddCnt += pmiRank[i] + cntRank[i];
            }

            int trueAnswerIndex = SearchTureAnswer(problemData, answerArr);//检索绝对正确的答案
            Debug.WriteLine("权威答案:" + trueAnswerIndex);

            if (trueAnswerIndex != -1)//匹配到权威答案
            {
                sumRank[trueAnswerIndex] = pmiAddCnt;
            }
            else//检索百度知道
            {
                if (SearchInBaiduZhiDao(problemData, answerArr, out int baiDuZhiDaoAnswerIndex, out int zdRatio))
                {
                    sumRank[baiDuZhiDaoAnswerIndex] += (double)pmiAddCnt * zdRatio / 100;
                    Debug.WriteLine(sumRank[baiDuZhiDaoAnswerIndex]);
                    Debug.WriteLine("百度知道答案:" + baiDuZhiDaoAnswerIndex + " Ratio:" + zdRatio);
                }
            }
            #endregion
            int maxIndex = 0;
            int minIndex = 0;
            for (int i = 0; i < answerArr.Length; i++)
            {
                sumRank[i] += pmiRank[i] + cntRank[i];
                if (sumRank[i] > sumRank[maxIndex])
                {
                    maxIndex = i;
                }

                if (sumRank[i] < sumRank[minIndex])
                {
                    minIndex = i;
                }
            }

            #region 计算概率
            double probabilitySum = 0;//权值和

            for (int i = 0; i < cntRank.Length; i++)
            {
                probabilitySum += sumRank[i];
            }

            int[] probability = new int[answerArr.Length];
            for (int i = 0; i < answerArr.Length; i++)
            {
                probability[i] = (int)(sumRank[i] / probabilitySum * 100);
            }
            #endregion

            AnalyzeResult ar;//分析结果
            ar.CntRank = cntRank;
            ar.PMIRank = pmiRank;
            ar.Index   = maxIndex;
            if (oppose) //存在否定词则取最小
            {
                ar.Index = minIndex;
            }
            ar.SumRank     = sumRank;
            ar.Probability = probability;
            ar.Oppose      = oppose;
            return(ar);
        }