public string resStr2 = ""; //存储标准答案替换字段 /// <summary> /// charList1 原题文章字符数组 /// charList2 标准答案字符数组 /// </summary> /// <param name="charList1"></param> /// <param name="charList2"></param> public ReplaceWord(char[] charList1, char[] charList2) { //char[] charList1 = readWord1.strList.ToCharArray(); List <string> TextList1 = new List <string>(); foreach (char i in charList1) { TextList1.Add(i.ToString()); } string[] strList1 = TextList1.ToArray(); List <string> TextList2 = new List <string>(); foreach (char i in charList2) { TextList2.Add(i.ToString()); } string[] strList2 = TextList2.ToArray(); LCS <string> strLCS = new LCS <string>(strList1, strList2); string str1 = ""; string str2 = ""; for (var i = 0; i < strLCS.Items.Length; i++) { if (strLCS.Items[i].ToString().IndexOf("-") == 0) { str1 += strLCS.Items[i].ToString().Substring(1); } else if (strLCS.Items[i].ToString().IndexOf("+") == 0) { str2 += strLCS.Items[i].ToString().Substring(1); } } char[] res1 = str1.ToCharArray(); char[] res2 = str2.ToCharArray(); List <string> resList1 = new List <string>(); List <string> resList2 = new List <string>(); for (var j = 0; j < res1.Length; j++) { if (resStr1.IndexOf(res1[j]) == -1) { resStr1 += res1[j]; } } for (var k = 0; k < res2.Length; k++) { if (resStr2.IndexOf(res2[k]) == -1) { resStr2 += res2[k]; } } }
/// <summary> /// 从index开始在strLCS中往后搜索替换字符串 /// </summary> /// <param name="index">开始搜索的位置</param> /// <param name="strLCS">LCS串</param> /// <param name="strBefore">返回:原字符串,如果未找到替换,strBefore为空</param> /// <param name="strAfter">返回:替换目标字符串</param> /// <returns>返回最后的Item的下一个Item的位置</returns> static int GetNextReplace(int index, LCS <char> strLCS, ref string strBefore, ref string strAfter) { //ITEM_MODE.X表示源文件中的原始文字,这里是原题,ITEM_MODE.Y表示目标文件中的替换后的新文字,这里是答案。 //本实验只有两种情况 //情况1,全文替换文字:先出现ITEM_MODE.Y后出现ITEM_MODE.X //情况2,全文删除文字:直接出现ITEM_MODE.X //出现ITEM_MODE.Y后没有出现ITEM_MODE.X表示增加文字,不是本实验研究范围。 strBefore = null; strAfter = null; int i; for (i = index; i < strLCS.Items.Length; i++) { Item <char> item = strLCS.Items[i]; if (item.Mode == ITEM_MODE.Y) { //Console.WriteLine(item); //测试用 //如果遇到下一组替换,本次替换结束 if (strBefore != null) { break; } strAfter += item.Value; } else if (item.Mode == ITEM_MODE.X) { //Console.WriteLine(item);//测试用 strBefore += item.Value; } else { if (strBefore != null) { break; } else if (strAfter != null) //如果只是增加,不认为是替换,继续往后找 { strAfter = null; } } } return(i); }
public Grade(string url1, string url2) { //替换前的字符串 string strBefore = null; //替换后的字符串 string strAfter = null; //替换出现的次数 int Count = 0; EWordDocument eWordDocument原题 = new EWordDocument(); eWordDocument原题.Open(url1); EWordDocument eWordDocument答案 = new EWordDocument(); eWordDocument答案.Open(url2); int idx; for (int i = 0; i < eWordDocument原题.LText.Count(); i++) { char[] arrayX = eWordDocument原题.LText[i].ToArray(); char[] arrayY = eWordDocument答案.LText[i].ToArray(); LCS <char> strLCS = new LCS <char>(arrayX, arrayY); idx = 0; string strBefore_ = null; string strAfter_ = null; while (idx < strLCS.Items.Length) { //如果未找到替换,strBefore为空 if (strBefore == null) { idx = GetNextReplace(idx, strLCS, ref strBefore, ref strAfter); if (strBefore != null) { Count++; } } else { idx = GetNextReplace(idx, strLCS, ref strBefore_, ref strAfter_); if (strBefore == strBefore_ && strAfter == strAfter_) { Count++; } } } } if (strBefore != null) { grade = Count; if (strAfter != null) { title = "替换题:请将文中所有的文字" + strBefore + "替换为" + strAfter + "。总分:" + Count + "分"; } else { title = "替换题:请删除文中所有的文字" + strBefore + "总分:" + Count + "分"; } } else { title = "没有替换题!"; } }