分词
1 private void fc(string keyWord) //自定义分词方法
2 {
3 if (m_SimpleDictSeg == null)
4 {
5 try
6 {
7 m_SimpleDictSeg = new CSimpleDictSeg();
8 m_SimpleDictSeg.DictPath = Server.MapPath("Data/"); //指定分词词库位置
9 m_SimpleDictSeg.LoadDict(); //装载字库到内存
10 }
11 catch (Exception e1)
12 {
13 m_SimpleDictSeg = null;
14 Response.Write(String.Format("Load Dict Fail! ErrMsg:{0}", e1.Message));
15 return;
16 }
17 }
18 m_SimpleDictSeg.FilterStopWords = false; //过滤停用词
19 m_SimpleDictSeg.MatchName = true; //识别中文人名
20 Stopwatch watch = new Stopwatch();
21 watch.Start();
22 //过滤字符和标点符号
23 string key = keyWord; //赋值查询关键字
24
25 key = key.Replace("的", " ");
26 key = key.Replace("么", " ");
27 key = key.Replace("吗", " ");
28 key = key.Replace("与", " ");
29 key = key.Replace(',', ' '); //将标点符号替换为空格
30 key = key.Replace('。', ' ');
31 key = key.Replace('!', ' ');
32 key = key.Replace('-', ' ');
33 key = key.Replace('-', ' ');
34 key = key.Replace(':', ' ');
35 key = key.Replace(';', ' ');
36 key = key.Replace(':', ' ');
37 key = key.Replace(';', ' ');
38 key = key.Replace('*', ' ');
39 key = key.Replace('.', ' ');
40 key = key.Replace(',', ' ');
41 key = key.Replace('‘', ' ');
42 key = key.Replace('’', ' ');
43 key = key.Replace('“', ' ');
44 key = key.Replace('”', ' ');
45 key = key.Replace('?', ' ');
46 key = key.Replace('?', ' ');
47 key = key.Replace("'", " ");
48 key = key.Replace('"', ' ');
49 key = key.Replace('(', ' ');
50 key = key.Replace(')', ' ');
51 key = key.Replace(')', ' ');
52 key = key.Replace('(', ' ');
53 key = key.Replace('_', ' ');
54 key = key.Replace('、', ' ');
55 key = key.Replace(" ", ""); //最后再替换掉空格
56 ArrayList words = m_SimpleDictSeg.Segment(key); //输入分词
57 watch.Stop();
58 StringBuilder wordsString = new StringBuilder();
59 foreach (String str in words)
60 {
61 wordsString.AppendFormat("{0}/", str);
62 }
63 key = wordsString.ToString(); //去掉空格
64 key = key.Substring(0, key.Length - 1); //去掉最后一个 / 字符
65 strKey = key.Split(new[] { '/', ' ' });
66 }
2 {
3 if (m_SimpleDictSeg == null)
4 {
5 try
6 {
7 m_SimpleDictSeg = new CSimpleDictSeg();
8 m_SimpleDictSeg.DictPath = Server.MapPath("Data/"); //指定分词词库位置
9 m_SimpleDictSeg.LoadDict(); //装载字库到内存
10 }
11 catch (Exception e1)
12 {
13 m_SimpleDictSeg = null;
14 Response.Write(String.Format("Load Dict Fail! ErrMsg:{0}", e1.Message));
15 return;
16 }
17 }
18 m_SimpleDictSeg.FilterStopWords = false; //过滤停用词
19 m_SimpleDictSeg.MatchName = true; //识别中文人名
20 Stopwatch watch = new Stopwatch();
21 watch.Start();
22 //过滤字符和标点符号
23 string key = keyWord; //赋值查询关键字
24
25 key = key.Replace("的", " ");
26 key = key.Replace("么", " ");
27 key = key.Replace("吗", " ");
28 key = key.Replace("与", " ");
29 key = key.Replace(',', ' '); //将标点符号替换为空格
30 key = key.Replace('。', ' ');
31 key = key.Replace('!', ' ');
32 key = key.Replace('-', ' ');
33 key = key.Replace('-', ' ');
34 key = key.Replace(':', ' ');
35 key = key.Replace(';', ' ');
36 key = key.Replace(':', ' ');
37 key = key.Replace(';', ' ');
38 key = key.Replace('*', ' ');
39 key = key.Replace('.', ' ');
40 key = key.Replace(',', ' ');
41 key = key.Replace('‘', ' ');
42 key = key.Replace('’', ' ');
43 key = key.Replace('“', ' ');
44 key = key.Replace('”', ' ');
45 key = key.Replace('?', ' ');
46 key = key.Replace('?', ' ');
47 key = key.Replace("'", " ");
48 key = key.Replace('"', ' ');
49 key = key.Replace('(', ' ');
50 key = key.Replace(')', ' ');
51 key = key.Replace(')', ' ');
52 key = key.Replace('(', ' ');
53 key = key.Replace('_', ' ');
54 key = key.Replace('、', ' ');
55 key = key.Replace(" ", ""); //最后再替换掉空格
56 ArrayList words = m_SimpleDictSeg.Segment(key); //输入分词
57 watch.Stop();
58 StringBuilder wordsString = new StringBuilder();
59 foreach (String str in words)
60 {
61 wordsString.AppendFormat("{0}/", str);
62 }
63 key = wordsString.ToString(); //去掉空格
64 key = key.Substring(0, key.Length - 1); //去掉最后一个 / 字符
65 strKey = key.Split(new[] { '/', ' ' });
66 }
执行查询
1 private DataTable FindResult(string keyWord)
2 {
3 //调用自定义方法,初始化数据源对象
4 fc(keyWord); //关键字分词
5 DateTime dt = DateTime.Now; //开始记时
6 IndexSearcher search = new IndexSearcher(Server.MapPath("Index")); //把刚才建立的索引取出来
7 string[] sField = new string[strKey.Length]; //初始化长度
8 sField.SetValue("Title", 0);
9 for (int s = 1; s < strKey.Length; s++)
10 {
11 sField.SetValue("Title", s); //创建将要查询Title字段
12 }
13 lblFC.Text = ""; //清空分词结果
14 //显示分词结果
15 foreach (string k in strKey)
16 {
17 //显示分词结果在1个以上的词,并设置超级链接。
18 if (k.Length >= 1)
19 lblFC.Text = lblFC.Text + " " +
20 "<A target='black' href='product.aspx?keywords=" + System.Web.HttpUtility.UrlEncode(k, System.Text.Encoding.GetEncoding("utf-8")) + "'>" + k + "</A>";
21 }
22 //执行查询
23 BooleanQuery q = new BooleanQuery();//使用 BooleanQuery 对象封装组合搜索请求,与 或 非
24 Query qq = MultiFieldQueryParser.Parse(strKey, sField, new StandardAnalyzer());
25 q.Add(qq, BooleanClause.Occur.MUST);
26 Hits hit = search.Search(q); //获取查询结果
27 TimeSpan ts = DateTime.Now.Subtract(dt); //计算用时
28 for (int i = 0; i <= hit.Length() - 1; i++)
29 {
30 Document doc = hit.Doc(i);
31 DataRow row = Results.NewRow();
32 //添加到DataRow中
33 //row["Title"] = HightLight(Server.UrlDecode(Request.QueryString["keywords"]), doc.Get("Title"));
34 row["Title"] = HightLight(System.Web.HttpUtility.UrlEncode(keyWord, System.Text.Encoding.GetEncoding("utf-8")), doc.Get("Title"));
35 //显示高亮描红查询关键字
36 row["Corporation"] = doc.Get("Corporation");
37 row["ID"] = doc.Get("ID");
38 Results.Rows.Add(row);
39 }
40 search.Close();
41 //lblSum.Text = "<font color=red>找到相关查询结果约" + hit.Length() + "篇,</font> 用时" + ts.ToString() + " 秒 ";
42 return Results;
43 }
2 {
3 //调用自定义方法,初始化数据源对象
4 fc(keyWord); //关键字分词
5 DateTime dt = DateTime.Now; //开始记时
6 IndexSearcher search = new IndexSearcher(Server.MapPath("Index")); //把刚才建立的索引取出来
7 string[] sField = new string[strKey.Length]; //初始化长度
8 sField.SetValue("Title", 0);
9 for (int s = 1; s < strKey.Length; s++)
10 {
11 sField.SetValue("Title", s); //创建将要查询Title字段
12 }
13 lblFC.Text = ""; //清空分词结果
14 //显示分词结果
15 foreach (string k in strKey)
16 {
17 //显示分词结果在1个以上的词,并设置超级链接。
18 if (k.Length >= 1)
19 lblFC.Text = lblFC.Text + " " +
20 "<A target='black' href='product.aspx?keywords=" + System.Web.HttpUtility.UrlEncode(k, System.Text.Encoding.GetEncoding("utf-8")) + "'>" + k + "</A>";
21 }
22 //执行查询
23 BooleanQuery q = new BooleanQuery();//使用 BooleanQuery 对象封装组合搜索请求,与 或 非
24 Query qq = MultiFieldQueryParser.Parse(strKey, sField, new StandardAnalyzer());
25 q.Add(qq, BooleanClause.Occur.MUST);
26 Hits hit = search.Search(q); //获取查询结果
27 TimeSpan ts = DateTime.Now.Subtract(dt); //计算用时
28 for (int i = 0; i <= hit.Length() - 1; i++)
29 {
30 Document doc = hit.Doc(i);
31 DataRow row = Results.NewRow();
32 //添加到DataRow中
33 //row["Title"] = HightLight(Server.UrlDecode(Request.QueryString["keywords"]), doc.Get("Title"));
34 row["Title"] = HightLight(System.Web.HttpUtility.UrlEncode(keyWord, System.Text.Encoding.GetEncoding("utf-8")), doc.Get("Title"));
35 //显示高亮描红查询关键字
36 row["Corporation"] = doc.Get("Corporation");
37 row["ID"] = doc.Get("ID");
38 Results.Rows.Add(row);
39 }
40 search.Close();
41 //lblSum.Text = "<font color=red>找到相关查询结果约" + hit.Length() + "篇,</font> 用时" + ts.ToString() + " 秒 ";
42 return Results;
43 }
高亮显示查询关键字
1 /// <summary>
2 /// 高亮显示查询关键字
3 /// </summary>
4 /// <param name="title"> 关键字 </param>
5 /// <param name="result"> 数据库中查询出的结果 </param>
6 /// <returns> </returns>
7 public string HightLight(string title, string result)
8 {
9 if (strKey.Length > 0) //strKey是分词结果 字符串数组
10 {
11 for (int k = 0; k < strKey.Length; k++)
12 {
13 //高亮显示设置(设置红色字体并且加粗)
14 result = result.Replace(strKey[k], "<font color='red'><strong>" + strKey[k] + "</strong></font>");
15 }
16 }
17 else
18 {
19 result = result.Replace(title, "<font color='red'><strong>" + title + "</strong></font>");
20 }
21 return result;
22 }
2 /// 高亮显示查询关键字
3 /// </summary>
4 /// <param name="title"> 关键字 </param>
5 /// <param name="result"> 数据库中查询出的结果 </param>
6 /// <returns> </returns>
7 public string HightLight(string title, string result)
8 {
9 if (strKey.Length > 0) //strKey是分词结果 字符串数组
10 {
11 for (int k = 0; k < strKey.Length; k++)
12 {
13 //高亮显示设置(设置红色字体并且加粗)
14 result = result.Replace(strKey[k], "<font color='red'><strong>" + strKey[k] + "</strong></font>");
15 }
16 }
17 else
18 {
19 result = result.Replace(title, "<font color='red'><strong>" + title + "</strong></font>");
20 }
21 return result;
22 }