예제 #1
0
        //通常搜索,返回相似度最高的前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)));
        }
예제 #2
0
        //智能扩展搜索,从鼠标位置自动句子中有效截取范围,返回相似度最高的前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);
        }