public List <FinalNode1> AdjacentNodeList(String LaTeX) { AdjacentNode adjacentNodes = new AdjacentNode(); List <FinalNode1> AdjacentNodeList = new List <FinalNode1>();//存放邻接节点有序对的集合 LimAndSum limandSums = new LimAndSum(); DuiJiaoXian duijiaoxian = new DuiJiaoXian(); NciGenHao nicigenHao = new NciGenHao(); DuiShu duishu = new DuiShu(); int biaozhi; String final; String cao; char[] caos; FuShu fushu = new FuShu(); biaozhi = 1; //每次循环前设置一个标志令标志等于1 final = ""; //每次令final为空字符串,用来接收“特殊lim和sum表达式”还是“一般表达式” cao = LaTeX.ToString().Trim(); //把数据库的数学表达式读出来,要开始判断了啊 caos = cao.ToCharArray(); for (int i = 0; i < caos.Length; i++) { if (Convert.ToString(caos[i]).Equals("\\") && Convert.ToString(caos[i + 1]).Equals("l") && Convert.ToString(caos[i + 2]).Equals("i") && Convert.ToString(caos[i + 3]).Equals("m")) { biaozhi = 0; break; } else if (Convert.ToString(caos[i]).Equals("\\") && Convert.ToString(caos[i + 1]).Equals("s") && Convert.ToString(caos[i + 2]).Equals("u") && Convert.ToString(caos[i + 3]).Equals("m")) { biaozhi = 0; break; } } if (biaozhi == 0)//说明有lim这种的 { final = limandSums.limAndSum(cao); final = fushu.fuShu(final); final = nicigenHao.NcigenHao(final); final = duishu.duiShu(final); } else { //下面这个是普通的和负号的 final = fushu.fuShu(cao); final = nicigenHao.NcigenHao(final); final = duishu.duiShu(final); } //================================对于遇到含有lim和sum等特殊表达式的需要一段代码来特殊处理,如果不是这类表达式,那就不用处理,不过还有一些负号这类的=========================================== AdjacentNodeList = adjacentNodes.AdjacentNodeList(final); return(AdjacentNodeList); }
public void sort(String queryLaTeX) { //=========用来判断节点个数============== AdjacentNode anode = new AdjacentNode(); //=========用来判断节点个数============== //Console.WriteLine("草拟吗了"); //======================================第二步:排序时间的测量===================================================================== System.Diagnostics.Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 开始监视代码运行时间 //第一步:获取查询表达式的所有子式集合 List <AAAAData> queryLaTeXList = new List <AAAAData>(); queryLaTeXList = Test4(queryLaTeX); //第二步:获取所有结果表达式集合(字符串) List <String> resultLaTeXList = new List <string>(); resultLaTeXList = getResultLaTeXList(queryLaTeX); //第三步:开始对查询表达式和每一个结果表达式进行打分,那就遍历每一个结果表达式吧 //======================!!!!!“查询表达式”的各个子式的分数依次是固定的========================== List <double> queryScore = new List <double>(); foreach (var queryZiShi in queryLaTeXList) { queryScore.Add(ZiShiBTlevel(queryZiShi, queryLaTeX)); } //======================!!!!!“查询表达式”的各个子式的分数依次是固定的========================== //======================放结果的最终需要排序的========================================== List <AAAAData> FinalList = new List <AAAAData>(); //======================放结果的最终需要排序的========================================== foreach (var resultLaTeX in resultLaTeXList) { //===================!!!!!对于每一个结果表达式,肯定也对应一个分数集合吧===================== List <double> resultScore = new List <double>(); foreach (var queryZiShiNode in queryLaTeXList) { if (ZiShiBTlevel(queryZiShiNode, resultLaTeX.ToLower()) == 1000)//说明结果表达式不存在查询子式 { //resultScore.Add(-(queryZiShiNode.BTLevel+1)); //这里没有的话我就弱化一下吧 resultScore.Add(-queryZiShiNode.BTLevel); } else { resultScore.Add(ZiShiBTlevel(queryZiShiNode, resultLaTeX.ToLower())); } } //===================!!!!!对于每一个结果表达式,肯定也对应一个分数集合吧===================== //==================================开始查询与结果计算评分===================================== //开始利用公式计算 double sum = 0; for (int k = 0; k < queryScore.Count; k++) { //sum = sum + Math.Abs((14 - queryScore[k]) - (14 - resultScore[k])) / (27 - Min(14 - queryScore[k], 14 - resultScore[k])); //sum = sum + Math.Abs((14 - queryScore[k]) - (14 - resultScore[k])) / (27); // 拉姆达=1 sum = sum + ((Math.Abs((14 - queryScore[k]) - (14 - resultScore[k]))) / (27)) * ((Math.Abs((14 - queryScore[k]) - (14 - resultScore[k]))) / (27)); // 拉姆达=2 //sum = sum + ((Math.Abs((14 - queryScore[k]) - (14 - resultScore[k]))) / (27)) * ((Math.Abs((14 - queryScore[k]) - (14 - resultScore[k]))) / (27)) * ((Math.Abs((14 - queryScore[k]) - (14 - resultScore[k]))) / (27)); // 拉姆达=3 //sum = sum + ((Math.Abs((14 - queryScore[k]) - (14 - resultScore[k]))) / (27)) * ((Math.Abs((14 - queryScore[k]) - (14 - resultScore[k]))) / (27)) * ((Math.Abs((14 - queryScore[k]) - (14 - resultScore[k]))) / (27)) * ((Math.Abs((14 - queryScore[k]) - (14 - resultScore[k]))) / (27)); // 拉姆达=4 //sum = sum + ((Math.Abs((14 - queryScore[k]) - (14 - resultScore[k]))) / (27)) * ((Math.Abs((14 - queryScore[k]) - (14 - resultScore[k]))) / (27)) * ((Math.Abs((14 - queryScore[k]) - (14 - resultScore[k]))) / (27)) * ((Math.Abs((14 - queryScore[k]) - (14 - resultScore[k]))) / (27)) * ((Math.Abs((14 - queryScore[k]) - (14 - resultScore[k]))) / (27)); // 拉姆达=5 } //=====================在这里起码补充一下查询树和结果树之间的差距的距离吧????????????=========================== //sum = sum + Math.Abs(MaxBTLevel(queryLaTeX) - MaxBTLevel(resultLaTeX.ToLower())) / (27 - Min(14 - MaxBTLevel(queryLaTeX), 14 - MaxBTLevel(resultLaTeX.ToLower()))); //sum = sum + Math.Abs(MaxBTLevel(queryLaTeX) - MaxBTLevel(resultLaTeX.ToLower())) / (27); //拉姆达=1 sum = sum + ((Math.Abs(MaxBTLevel(queryLaTeX) - MaxBTLevel(resultLaTeX.ToLower()))) / (27)) * ((Math.Abs(MaxBTLevel(queryLaTeX) - MaxBTLevel(resultLaTeX.ToLower()))) / (27)); //拉姆达=2 //sum = sum + ((Math.Abs(MaxBTLevel(queryLaTeX) - MaxBTLevel(resultLaTeX.ToLower()))) / (27)) * ((Math.Abs(MaxBTLevel(queryLaTeX) - MaxBTLevel(resultLaTeX.ToLower()))) / (27)) * ((Math.Abs(MaxBTLevel(queryLaTeX) - MaxBTLevel(resultLaTeX.ToLower()))) / (27)); //拉姆达=3 //sum = sum + ((Math.Abs(MaxBTLevel(queryLaTeX) - MaxBTLevel(resultLaTeX.ToLower()))) / (27)) * ((Math.Abs(MaxBTLevel(queryLaTeX) - MaxBTLevel(resultLaTeX.ToLower()))) / (27)) * ((Math.Abs(MaxBTLevel(queryLaTeX) - MaxBTLevel(resultLaTeX.ToLower()))) / (27)) * ((Math.Abs(MaxBTLevel(queryLaTeX) - MaxBTLevel(resultLaTeX.ToLower()))) / (27)); //拉姆达=4 //sum = sum + ((Math.Abs(MaxBTLevel(queryLaTeX) - MaxBTLevel(resultLaTeX.ToLower()))) / (27)) * ((Math.Abs(MaxBTLevel(queryLaTeX) - MaxBTLevel(resultLaTeX.ToLower()))) / (27)) * ((Math.Abs(MaxBTLevel(queryLaTeX) - MaxBTLevel(resultLaTeX.ToLower()))) / (27)) * ((Math.Abs(MaxBTLevel(queryLaTeX) - MaxBTLevel(resultLaTeX.ToLower()))) / (27)) * ((Math.Abs(MaxBTLevel(queryLaTeX) - MaxBTLevel(resultLaTeX.ToLower()))) / (27)); //拉姆达=5 //这里是节点个数不咋地 //sum = sum + Math.Abs(anode.AdjacentNodeList(queryLaTeX).Count - anode.AdjacentNodeList(resultLaTeX.ToLower()).Count) / (27); //=====================在这里起码补充一下查询树和结果树之间的差距的距离吧????????????=========================== //=====================这里我想连接一下查询表达式各个分数和结果表达式分数 String queryStr = ""; foreach (var it in queryScore) { queryStr = queryStr + it + "#"; } queryStr = queryStr + MaxBTLevel(queryLaTeX); String resultStr = ""; foreach (var it in resultScore) { resultStr = resultStr + it + "#"; } resultStr = resultStr + MaxBTLevel(resultLaTeX.ToLower()); //=====================这里我想连接一下查询表达式各个分数和结果表达式分数 //==================================开始查询与结果计算评分===================================== //==================把打好分的结果以节点形式放入一个集合当中=========================== AAAAData data = new AAAAData(); sum = sum / (queryScore.Count + 1);//??????????????????????这里加1也不对啊?????????????????????? //data.BTLevel = 1 - sum;//拉姆达=1 data.BTLevel = 1 - Math.Sqrt(sum);//拉姆达=2 //data.BTLevel = 1 - Math.Pow(sum,0.3333333333);//拉姆达=3 //data.BTLevel = 1 - Math.Pow(sum,0.25);//拉姆达=4 //data.BTLevel = 1 - Math.Pow(sum, 0.2);//拉姆达=5 data.str = resultLaTeX; data.queryStr = queryStr; data.resultStr = resultStr; FinalList.Add(data); //==================把打好分的结果以节点形式放入一个集合当中=========================== } //======================对最终结果进行排序====================== AAAAData tempdata; for (int i = 0; i < FinalList.Count - 1; i++) { for (int j = i + 1; j < FinalList.Count; j++) { if (FinalList[j].BTLevel > FinalList[i].BTLevel) { tempdata = FinalList[j]; FinalList[j] = FinalList[i]; FinalList[i] = tempdata; } } } //======================对最终结果进行排序====================== //======================打印输出================================ //foreach (var it in FinalList) //{ // Console.WriteLine("结果表达式:"+it.str + "\t" + "查询表达式打分:" + it.queryStr + "\t" + "结果表达式打分:" + it.resultStr + "\t" + "相似度得分为:" + it.BTLevel); //} //===========我想去个重================== List <AAAAData> quchong = new List <AAAAData>(); foreach (var it in FinalList) { if (quchong.Count == 0) { quchong.Add(it); } else { int biaozhi = 0; foreach (var itt in quchong) { if (it.str.Equals(itt.str))//如果出现重复了 { biaozhi = 1; break; } } if (biaozhi == 0)//说明没有出现重复了,你染没有重复,那就读取对应的结果表达式 { quchong.Add(it); } } } int counttt = 1; foreach (var it in quchong) { if (it.str.Equals("\\frac{a+b}{2}\\geq\\sqrt{a*b}")) { } Console.WriteLine("==========================================================第" + counttt + "个===================================================="); Console.WriteLine("结果表达式:" + it.str + "\t" + "查询表达式打分:" + it.queryStr + "\t" + "结果表达式打分:" + it.resultStr + "\t" + "相似度得分为:" + it.BTLevel); Console.WriteLine(""); Console.WriteLine(""); counttt++; } //===========我想去个重================== //======================打印输出================================ //======================================第二步:排序时间的测量===================================================================== stopwatch.Stop(); // 停止监视 TimeSpan timespan = stopwatch.Elapsed; // 获取当前实例测量得出的总时间 double hours = timespan.TotalHours; // 总小时 double minutes = timespan.TotalMinutes; // 总分钟 double seconds = timespan.TotalSeconds; // 总秒数 double milliseconds = timespan.TotalMilliseconds; // 总毫秒数 string timePast = "耗时" + seconds + "秒,即" + milliseconds + "毫秒!"; Console.WriteLine("========================================第二步:排序时间为:" + timePast); }
public void Test2(String queryLaTeX) { StreamReader sr = new StreamReader("C:\\Users\\dell\\Desktop\\暑假\\实验数据\\1.txt", Encoding.Default); String read = sr.ReadLine(); List <AAAAData> Final = new List <AAAAData>(); while (read != null) { //获取子式 ChildrenBTree children = new ChildrenBTree(); Dictionary <int, List <FinalNode1> > aaa = new Dictionary <int, List <FinalNode1> >(); aaa = children.childrenBTree(read); String strs = ""; //Console.WriteLine("草拟吗:"+read); foreach (var aaaa in aaa) { //每一个子式 String str = ""; foreach (var aaaaa in aaaa.Value) { str = str + aaaaa.zifu; } Dictionary <int, List <FinalNode1> > dics = new Dictionary <int, List <FinalNode1> >(); AAAA A = new AAAA(); dics = A.BTLevelScore(str, read); if (dics == null || dics.Count == 0) { //Console.WriteLine("草擦曹操"); read = sr.ReadLine(); continue; } //下面这个是统计查询表达式a+b,在结果表达式a+b+c+a+b,的两个a+b的最小树的层次啊 foreach (var it in dics) { //Console.WriteLine("键值:" + it.Key); int min = 10000; foreach (var itt in it.Value) { if (itt.BTreeLevel < min) { min = itt.BTreeLevel; } //Console.WriteLine(itt.zifu+"\t"+itt.BTreeLevel); } strs = strs + min + "#"; } } //============================= //统计一下负数的那个,树的高度越高,它的负值越大,负的越厉害。注意现在统计的是结果表达式的最大高度,也就是read int fushu = 0; int maxBTLevel = 0; List <FinalNode1> lists = new List <FinalNode1>(); AdjacentNode aa = new AdjacentNode(); lists = aa.AdjacentNodeList(read); foreach (var it in lists) { if (it.BTreeLevel > fushu) { fushu = it.BTreeLevel; } } fushu = (-1) * fushu; strs = strs + fushu + "#" + read; //Console.WriteLine(str); //已经得到一个查询表达式在结果表达式里面的层次级别了,开始计算分数了 String[] news = strs.Split('#'); List <int> list = new List <int>(); for (int i = 0; i < news.Length - 1; i++) { list.Add(Convert.ToInt32(news[i])); //Console.WriteLine("草:"+news[i]); } //开始排序 int tempdata; for (int i = 0; i < list.Count - 1; i++) { for (int j = i + 1; j < list.Count; j++) { if (list[j] < list[i]) { tempdata = list[j]; list[j] = list[i]; list[i] = tempdata; } } } //开始利用公式计算 double sum = 0; for (int i = 0; i < news.Length - 1; i++) { sum = sum + Math.Abs(19 - (20 - Convert.ToDouble(news[i]))) / (19); //Console.WriteLine("看看结果:"+sum); } sum = sum / (news.Length - 1); AAAAData d = new AAAAData(); d.BTLevel = 1 - sum; d.str = read; Final.Add(d); //============================= read = sr.ReadLine(); } //开始排序 AAAAData tempdatas; for (int i = 0; i < Final.Count - 1; i++) { for (int j = i + 1; j < Final.Count; j++) { if (Final[j].BTLevel > Final[i].BTLevel) { tempdatas = Final[j]; Final[j] = Final[i]; Final[i] = tempdatas; } } } foreach (var it in Final) { Console.WriteLine("公式为:" + it.str + "\t" + "相似度为:" + it.BTLevel); } }
public void WriteData() { //第一步:先把txt所有数学公式读入内存 StreamReader sr = new StreamReader("C:\\Users\\Administrator\\Desktop\\最终版组合测试添加数据用来修改的\\最终版组合测试添加数据用来修改的\\14最终版组合测试模糊匹配有点毛病啊,已解决14\\第二个实验测试数据\\1.txt", Encoding.Default); StreamWriter sw = new StreamWriter("C:\\Users\\Administrator\\Desktop\\最终版组合测试添加数据用来修改的\\最终版组合测试添加数据用来修改的\\14最终版组合测试模糊匹配有点毛病啊,已解决14\\第二个实验测试数据\\2.txt"); List <AAIndex> indexList = new List <AAIndex>(); DuiShu duishu = new DuiShu(); FuShu fuShu = new FuShu(); String read = sr.ReadLine(); read = duishu.duiShu(read); read = fuShu.fuShu(read); int a = 0; while (read != null) { //if (a == 1) //{ // break; //} //a++; List <FinalNode1> AdjacentNodeList = new List <FinalNode1>(); AdjacentNode ad = new AdjacentNode(); AdjacentNodeList = ad.AdjacentNodeList(read); String str = null; //这个是每一个字符串 //现在这个是中序遍历好的节点 List <String> ji = new List <string>(); foreach (var it in AdjacentNodeList) { //第一:如果左右节点都为操作数 if (isOperator(it.zifu) && it.left != null && it.right != null && !isOperator(it.left.zifu) && !isOperator(it.right.zifu)) { List <FinalNode1> nodeList = new List <FinalNode1>(); middleBianLi zhongxu = new middleBianLi(nodeList, it); String strr = null; foreach (var itt in nodeList) { strr = strr + itt.zifu; //Console.WriteLine("链接为:"+itt.zifu); } //foreach (var itt in nodeList) //{ // if (isOperator(itt.zifu)) // { // strr = strr + itt.BTreeLevel; // } //} ji.Add(strr); } //第二:如果左节点是操作数,右节点是操作符 if (isOperator(it.zifu) && it.left != null && it.right != null && !isOperator(it.left.zifu) && isOperator(it.right.zifu)) { ji.Add(it.left.zifu /*+ it.left.BTreeLevel*/); ji.Add(it.zifu /*+it.BTreeLevel*/); } //第三:如果左节点是操作符,右节点是操作数 if (isOperator(it.zifu) && it.left != null && it.right != null && isOperator(it.left.zifu) && !isOperator(it.right.zifu)) { ji.Add(it.zifu /*+ it.BTreeLevel*/); ji.Add(it.right.zifu /*+ it.right.BTreeLevel*/); } //第四:如果左右节点都是操作符 if (isOperator(it.zifu) && it.left != null && it.right != null && isOperator(it.left.zifu) && isOperator(it.right.zifu)) { ji.Add(it.zifu /*+ it.BTreeLevel*/); } //第五:类似sina if (isOperator(it.zifu) && it.left != null && it.right == null && !isOperator(it.left.zifu)) { ji.Add(it.left.zifu + it.zifu); } //第六:类似sin(a+b) if (isOperator(it.zifu) && it.left != null && it.right == null && isOperator(it.left.zifu)) { ji.Add(it.zifu /*+ it.BTreeLevel*/); } //if (isOperator(it.zifu) && it.left!=null && it.right != null && !isOperator(it.left.zifu) && !isOperator(it.right.zifu)) //{ // //如果该字符是运算符且左右节点都是运算数,说明它是个子树 // //把中序遍历好的每一个节点存入集合当中 // //Console.WriteLine("哈哈哈:"+it.zifu); // List<FinalNode1> nodeList = new List<FinalNode1>(); // middleBianLi zhongxu = new middleBianLi(nodeList, it); // String strr = null; // foreach (var itt in nodeList) // { // strr = strr + itt.zifu; // //Console.WriteLine("链接为:"+itt.zifu); // } // foreach (var itt in nodeList) // { // if (isOperator(itt.zifu)) // { // strr = strr + itt.BTreeLevel; // } // } // ji.Add(strr); //} //else if (it.left != null && it.right != null && isOperator(it.left.zifu) && !isOperator(it.right.zifu)) //{ // ji.Add(it.zifu+it.BTreeLevel); // ji.Add(it.right.zifu+it.right.BTreeLevel); //} //else if (it.left != null && it.right != null && !isOperator(it.left.zifu) && isOperator(it.right.zifu)) //{ // ji.Add(it.left.zifu+ it.left.BTreeLevel); // ji.Add(it.zifu+it.BTreeLevel); //} } foreach (var itt in ji) { str = str + itt + "#"; } str = str + read; sw.WriteLine(str); //Console.WriteLine("草泥马了:" + str); //Console.WriteLine("=============================="); read = sr.ReadLine(); if (read == null) { break; } read = duishu.duiShu(read); read = fuShu.fuShu(read); } //Console.WriteLine("哪出bug了:==============="); //开始写了 //foreach (var it in indexList) //{ // if (it.zishiStructure.Equals("")) // { // sw.WriteLine(it.zishi + "#" + " " + "#" + it.LaTeX + "#" + it.xuhao); // } // else // sw.WriteLine(it.zishi + "#" + it.zishiStructure + "#" + it.LaTeX + "#" + it.xuhao); //} sw.Flush(); }