随笔 - 69, 文章 - 0, 评论 - 151, 阅读 - 60009
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

分词+搜索

Posted on   IT小炸弹  阅读(236)  评论(0编辑  收藏  举报
复制代码
分词
 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     }
复制代码
执行查询
 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 + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " +
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     }
复制代码
复制代码
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示