public SearchResult TopSearch(string SentenceString, int lLoc, int top) { SearchResult sr = new SearchResult(); resultlist = new ArrayList(top); string simplesen = CharacterSet.BIG5ToGB(SentenceString); string mainsen = GetMainSentence(simplesen, lLoc).Trim(); //先匹配 CardMatch(mainsen); if (sr.KeyWord == null && resultlist.Count > 0) { sr.KeyWord = mainsen; } //接着搜索 string sss = PunRegex.Replace(simplesen, " "); CardSearch(sss); if (sr.KeyWord == null && resultlist.Count > 0) { sr.KeyWord = sss; } sss = PunRegex.Replace(mainsen, " "); CardSearch(sss); if (sr.KeyWord == null && resultlist.Count > 0) { sr.KeyWord = sss; } //如果结果数不足8,进行词扩展搜索 if (resultlist.Count < top) { WordList wl = GetWordList(simplesen, lLoc); StringBuilder sb = new StringBuilder(); int i = wl.CurrentIndex + 2; while (i >= wl.Words.Length) { i--; } for (; i >= wl.CurrentIndex; i--) { int j = wl.CurrentIndex - 2; while (j < 0) { j++; } for (; j <= wl.CurrentIndex; j++) { sb.Length = 0; for (int k = j; k <= i; k++) { sb.Append(wl.Words[k]); sb.Append(" "); } int precount = resultlist.Count; string ssss = sb.ToString(); CardSearch(ssss); if (sr.KeyWord == null && precount < resultlist.Count) { sr.KeyWord = ssss.Trim(); } } } } string stopWord = "的之の・"; char[] stopWords = { '的', '之', 'の', '・' }; Regex StopWordsRegex = new Regex(@"(^[\s、的之の・]*)|([\s、的之の・]*$)", RegexOptions.Compiled); //如果结果数不足8,进行字扩展搜索 if (resultlist.Count < top && stopWord.IndexOf(simplesen[lLoc]) < 0) { WordList wl = GetLetterList(simplesen, lLoc); StringBuilder sb = new StringBuilder(); int i = wl.CurrentIndex + 4; while (i >= wl.Words.Length) { i--; } for (; i >= wl.CurrentIndex; i--) { int j = wl.CurrentIndex - 2; while (j < 0) { j++; } for (; j <= wl.CurrentIndex; j++) { sb.Length = 0; for (int k = j; k <= i; k++) { sb.Append(wl.Words[k]); sb.Append(" "); } int precount = resultlist.Count; string ssss = sb.ToString(); CardSearch(ssss); if (sr.KeyWord == null && precount < resultlist.Count) { sr.KeyWord = StopWordsRegex.Replace(ssss, ""); } } } } sr.Cards = (CardDescription[])resultlist.ToArray(typeof(CardDescription)); return(sr); }