搜索引擎查询环节,汉字关键字的判断

汉字,如果保持索引关键字的长度只有2和3两种,那么对于输入过长的怎么办?

5个字可以组成2+3和3+2两种,究竟是谁高,要根据命中率来排序,命中率高的,就作为组合搜索条件

比如:笨狼代码大管家,可以拆成:

 笨狼 代码 大管家
笨狼 代码大 管家
笨狼代 码大 管家
------------------
如果:“笨狼” “代码” “大管家”这几个字在索引中存在,或者词频高,那么就按照这个做组合搜索。

相反,象“码大”“代码大”这样可能压根不算是词的组合,排序就会靠后。

例如:当索引中存在:“笨狼” “代码” “大管家”“管家”时,命中率分别是

笨狼代 码大 管家1
笨狼 代码大 管家2
笨狼 代码 大管家3

倒排索引保持2+3的结构极其重要,因为:
1,可以保证命中率,因为单个字咱不搜,没意义,而所有大于1的词都可以拆成2n+3m的组合。

2,极大减少内存和文件消耗量

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static Dictionary<string, int> Dict = new Dictionary<string, int>();
static void Main(string[] args)
{
Dict.Add(
"笨狼", 4);
Dict.Add(
"代码", 44);
Dict.Add(
"大管家", 11);
Dict.Add(
"管家", 68);

KeyMaker KM
= new KeyMaker();
string input = "笨狼代码大管家";
List
<string> ListStr = KM.GetAllKey(input);
foreach (string S in ListStr)
{
Console.WriteLine(S);
}
Console.WriteLine(
"------------------");
Console.WriteLine(KM.GetMaxHitKey(input, Dict));


Console.Read();
}

class KeyMaker
{
public string GetMaxHitKey(string text, Dictionary<string, int> dict)
{
List
<string> strList = GetAllKey(text);
//查找最大命中,线性扫描即可,无须排序
if (strList.Count > 0)
{
int maxValue = -1;
int maxIndex = -1;
for (int i = 0; i < strList.Count;i++ )
{
string[] arrA = strList[i].Split(" ".ToCharArray());
int x = 0;
foreach (string a in arrA)
{
x
+= (dict.ContainsKey(a) ? 1: 0);
}
if (x > maxValue)
{
maxValue
= x;
maxIndex
= i;
}
//Console.WriteLine("/"+strList[i] + x);
}

return strList[maxIndex] + maxValue;
}
else
{
return "";
}
}


public List<string> GetAllKey(string text)
{
List
<string> strList = new List<string>();
if (text.Length > 1)
{
getKeys(text, text,
"", strList);
}
return strList;
}
private void getKeys(string text, string tempText, string resultText, List<string> strList)
{
switch (tempText.Length)
{
case 0:
break;
case 1:
break;
case 2:
strList.Add(resultText.Trim()
+ " " + text.Substring(text.Length - 2));
break;
case 3:
strList.Add(resultText.Trim()
+ " " + text.Substring(text.Length - 3));
break;
default:
getKeys(text, tempText.Remove(
0, 3), resultText + " " + tempText.Substring(0, 3), strList);
getKeys(text, tempText.Remove(
0, 2), resultText + " " + tempText.Substring(0, 2), strList);

break;
}
}

}

}
}


中英文混合的,英文保留原分词规则即可,不解释。


posted @ 2011-04-14 01:11  CSDN大笨狼  阅读(750)  评论(0编辑  收藏  举报