//通常搜索,返回相似度最高的前top个搜索结果 public CardDescription[] NormalSearch(string SentenceString, int Top) { //转为简体 //string simplesen = CharacterSet.BIG5ToGB(SentenceString); //这里应该是全角转半角 string simplesen = CharacterSet.SBCToDBC(SentenceString); //过滤stopwords simplesen = stopwrods.Replace(simplesen, " "); //过滤标点符号 StringBuilder sb = new StringBuilder(); for (int i = 0; i < simplesen.Length; i++) { if (Char.IsLetterOrDigit(simplesen[i])) { sb.Append(simplesen[i]); } else { sb.Append(' '); } } simplesen = sb.ToString(); if (simplesen.Length == 0) { return(new CardDescription[0]); } //清0 for (int i = 0; i < Result.Count; i++) { ((DictSearcherNode)Result[i]).CardIndex = i; ((DictSearcherNode)Result[i]).Score = 0; } DoSearch(simplesen); DictSearcherCompare dsc = new DictSearcherCompare(); Result.Sort(dsc); ArrayList al = new ArrayList(); if (Top == 0) { Top = cardLibrary.GetCount(); } for (int i = 0; i < Top; i++) { DictSearcherNode node = (DictSearcherNode)Result[i]; if (node.Score > 0) { CardDescription card = cardLibrary.GetCardByIndex(node.CardIndex); al.Add(card); } else { break; } } return((CardDescription[])al.ToArray(typeof(CardDescription))); }
public static string Mapper(string query) { string s = query.Trim(); s = CharacterSet.SBCToDBC(s); s = regex1.Replace(s, " AND "); s = regex2.Replace(s, " OR "); s = regex3.Replace(s, "name:"); s = regex4.Replace(s, "japName:"); s = regex5.Replace(s, "oldName:"); s = regex18.Replace(s, "shortName:"); s = regex6.Replace(s, "cardType:"); s = regex7.Replace(s, "tribe:"); s = regex8.Replace(s, "element:"); s = regex9.Replace(s, "effect:"); s = regex10.Replace(s, "adjust:"); s = regex11.Replace(s, "package:"); s = regex12.Replace(s, "infrequence:"); s = regex13.Replace(s, "atkValue:"); s = regex14.Replace(s, "defValue:"); s = regex15.Replace(s, "level:"); s = regex16.Replace(s, "limit:"); s = regex19.Replace(s, "ID:"); MatchCollection matches = regex17.Matches(s); for (int i = 0; i < matches.Count; i++) { Match match = matches[matches.Count - 1 - i]; string numlength = ""; switch (match.Groups["field"].Value) { case "atkValue": numlength = "4"; break; case "defValue": numlength = "4"; break; case "level": numlength = "2"; break; } string s2 = null; int n1 = 0; int n2 = 9999; if (match.Groups["num2"].Success) { try { n1 = int.Parse(match.Groups["num1"].Value); n2 = int.Parse(match.Groups["num2"].Value); } catch { } s2 = string.Format("{0}:[{1:D" + numlength + "} TO {2:D" + numlength + "}]", match.Groups["field"].Value, n1, n2); } else { try { n1 = int.Parse(match.Groups["num1"].Value); } catch { } s2 = string.Format("{0}:{1:D" + numlength + "}", match.Groups["field"].Value, n1); } if (match.Index > 0) { s = s.Substring(0, match.Index) + s2 + s.Substring(match.Index + match.Length, s.Length - match.Index - match.Length); } else { s = s2 + s.Substring(match.Index + match.Length, s.Length - match.Index - match.Length); } } //MessageBox.Show(s); return(s); }
//智能扩展搜索,从鼠标位置自动句子中有效截取范围,返回相似度最高的前top个搜索结果 public SearchResult TopSearch(string SentenceString, int lLoc, int top) { SearchResult sr = new SearchResult(); //转为简体 //string simplesen = CharacterSet.BIG5ToGB(SentenceString); //这里应该是全角转半角 string simplesen = CharacterSet.SBCToDBC(SentenceString); //过滤stopwords simplesen = stopwrods.Replace(simplesen, " "); //过滤标点符号 StringBuilder sb = new StringBuilder(); int skip = 0; for (int i = 0; i < simplesen.Length; i++) { if (Char.IsLetterOrDigit(simplesen[i])) { sb.Append(simplesen[i]); } else { sb.Append(' '); if (i <= lLoc) { skip++; } } } simplesen = sb.ToString(); if (simplesen.Length == 0) { return(sr); } //清0 for (int i = 0; i < Result.Count; i++) { DictSearcherNode node = (DictSearcherNode)Result[i]; node.CardIndex = i; node.Score = 0; } Hashtable SearchedKeyword = new Hashtable(); string lMainWord = ""; int lMainWordLenth = 0; string rMainWord = ""; int rMainWordLenth = 0; for (int i = lLoc; i >= 0; i--) { string s = simplesen.Substring(i, lLoc - i + 1); string trims = GetTokenizerText(s); if (trims.Length > 0 && !SearchedKeyword.ContainsKey(trims)) { SearchedKeyword.Add(trims, 0); if (DoSearch(s) > 0) { if (trims.Length > lMainWordLenth) { int blank = 0; while (s[blank] == ' ') { blank++; } lMainWord = SentenceString.Substring(i + blank, lLoc - i + 1 - blank); lMainWordLenth = trims.Length; } } else { break; } } } for (int i = lLoc + 1; i < simplesen.Length; i++) { string s = simplesen.Substring(lLoc, i - lLoc + 1); string trims = GetTokenizerText(s); if (trims.Length > 0 && !SearchedKeyword.ContainsKey(trims)) { SearchedKeyword.Add(trims, 0); if (DoSearch(s) > 0) { if (trims.Length > rMainWordLenth) { int blank = 0; while (s[blank] == ' ') { blank++; } rMainWord = SentenceString.Substring(lLoc + blank, i - lLoc + 1 - blank); rMainWordLenth = trims.Length; } } else { break; } } } DictSearcherCompare dsc = new DictSearcherCompare(); Result.Sort(dsc); sr.Cards = new CardDescription[top]; if (rMainWord.Length > 1) { if (GetTokenizerText(SentenceString.Substring(lLoc, 1)).Length > 0) { sr.KeyWord = lMainWord + rMainWord.Substring(1); } else { sr.KeyWord = lMainWord + rMainWord; } } else { sr.KeyWord = lMainWord; } for (int i = 0; i < top; i++) { sr.Cards[i] = cardLibrary.GetCardByIndex(((DictSearcherNode)Result[i]).CardIndex); } return(sr); }