Daily Report 2012.10.31 刘宇翔
match函数功能基本功能已经实现了!
但是感觉缺陷还是有很多的。
目前这个匹配功能还有一些不合理的地方(与算法有关的)
目前还没有解决的一些问题:匹配级别的更合理设定、半全角英文识别、未知出错bug(暂未发现但我觉得应该会有)。
接下来的两天的工作分别是TEST和优化。
TEST将有姚铭协助我进行(自己TEST自己好像很难。。)。
写一下函数模块吧:
match(string word,string keyword)//主函数
wordmatch(string w,string keyword)//word单个词对整个keyword匹配
wkmatch(string w,string k)//word单个词对整个keyword单个词匹配
//3种模糊程度函数
m3();
m2();
m1();
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace match0 { class Program { static public int match(string word,string keyword) { int matchDegree = -1;//word、keyword匹配级别 string[] wordlist = word.Split(new char[] { ' ' },StringSplitOptions.RemoveEmptyEntries); int wlN = wordlist.Count();//word关键词数量 //非模糊匹配,返回-1或4**************************************************************** if (word.Length == 0 || keyword.Length == 0)//输入有空,返回-1 return matchDegree; if (word.ToLower() == keyword.ToLower())//word,keyword完全匹配,返回最高级3 { matchDegree = 4; return matchDegree; } //************************************************************************************** //模糊匹配,返回0或1或2或3**************************************************************暂未完全实现 if (wlN == 1)//word只含一个关键词 { matchDegree = wordmatch(wordlist[0], keyword); } else //word含多个关键词 { List<int> wkDegree = new List<int>(); for (int i = 0; i < wlN; i++) wkDegree.Add(0); for (int j = 0; j < wlN; j++) { wkDegree[j] = wordmatch(wordlist[j], keyword); } //去wkDegree[]众数法 /*int[] count = { 0, 0, 0, 0 }; for (int i = 0; i < wlN; i++) { switch (wkDegree[i]) { case 0: count[0]++; break; case 1: count[1]++; break; case 2: count[2]++; break; case 3: count[3]++; break; } } int Max = 0; matchDegree = 0; for (int i = 0; i < 4; i++) { if (count[i] > Max) { Max = count[i]; matchDegree = i; } }*/ //去wkDegree[]最大数法 int Max = 0; for (int i = 0; i < wlN; i++) { if (wkDegree[i] > Max) { Max = wkDegree[i]; } } matchDegree = Max; return matchDegree; } //*************************************************************************************** return matchDegree;//因错误等不明原因跳出,返回-1 } static public int wordmatch(string w, string keyword)//单个关键词对keyword的模糊匹配,w为单个关键词 { int wmatchDegree = 0;//w、keyword匹配级别 int Max = 0; List<int> wkDegree=new List<int>(); string[] keywordlist = keyword.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); int klN = keywordlist.Count();//keyword关键词数量 for (int i = 0; i < klN;i++ ) wkDegree.Add(0); for (int j = 0; j < klN; j++) { wkDegree[j] = wkmatch(w, keywordlist[j]); } Max = wkDegree[0]; for (int i = 1; i < klN; i++) { if (wkDegree[i] > Max) Max = wkDegree[i]; } wmatchDegree = Max; return wmatchDegree; } static public int wkmatch(string w, string k)//单个关键词对单个k的模糊匹配,k为keyword单个关键词 { int wkDegree = 0;//w、k匹配级别 int YorN = 0; int m = w.Length; int n = k.Length; w = w.ToLower(); k = k.ToLower(); //模糊度3 if (m < n) { YorN=m3(w, k); } else { YorN=m3(k, w); } if (YorN == 1) { wkDegree = 3; return wkDegree; } //模糊度2 else if (m < n) { YorN = m2(w, k); } else { YorN = m2(k, w); } if (YorN == 1) { wkDegree = 2; return wkDegree; } //模糊度1 else if (m < n) { YorN = m1(w, k); } else { YorN = m1(k, w); } if (YorN == 1) { wkDegree = 1; return wkDegree; } //模糊度0 else { wkDegree = 0; return wkDegree; } } static public int m3(string x, string y) { int ans=0; int m = x.Length; int n = y.Length; int i = 0; int j = 0; int k = 0; while (i < m && j < n) { if (x[i] == y[j]) { i++; j++; if (i == m) { ans = 1; break; } } else { i = 0; k++; j = k; } } return ans; }//模糊度3 static public int m2(string x, string y) { int ans = 0; int m = x.Length; int n = y.Length; int l = 0; int Ml = 0; for (int i = 0; i < (m/2+1); i++) { int i2 = i; int j = 0; int k = 0; while(j<n) { if (x[i2] == y[j]) { i2++; j++; l++; if (i2 >= m) { i2 = i; k++; j = k; if (l > Ml) Ml = l; } } else { i2 = i; k++; j = k; if( l > Ml ) Ml = l; } } } if (Ml > (m / 2)) ans = 1; else ans = 0; return ans; }//模糊度2 static public int m1(string x, string y) { int ans = 0; int m = x.Length; int n = y.Length; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (x[i] == y[j]) ans = 1; } } return ans; }//模糊度1 /*static int ChineseOrEnglish(string str)//中英文判断 暂未完全实现 { int len = 0; byte[] b; for (int i = 0; i < str.Length; i++) { b = Encoding.Default.GetBytes(str.Substring(i, 1)); if (b.Length > 1) len += 2; else len++; } return len; }*/ static void Main(string[] args)//供测试用主函数提供各函数返回值 { int a,b,c; string x = Console.ReadLine(); string y = Console.ReadLine(); a = match(x, y); //b = ChineseOrEnglish(x); //c = ChineseOrEnglish(y); Console.WriteLine(a); //Console.WriteLine(b); //Console.WriteLine(c); Console.WriteLine(x.Length); Console.WriteLine(y.Length); } } }