/// <summary> /// 设置关键字高亮 /// </summary> /// <param name="dicKeywords">关键字列表</param> /// <param name="model">返回的数据模型</param> /// <returns></returns> private LiteNewsModel SetHighlighter(Dictionary <string, string> dicKeywords, LiteNewsModel model) { //return model; SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>"); Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment()) { FragmentSize = 50 }; string strTitle = string.Empty; string strAbstract = string.Empty; dicKeywords.TryGetValue("title", out strTitle); dicKeywords.TryGetValue("abstract", out strAbstract); try { if (!string.IsNullOrEmpty(strTitle)) { var transStr = highlighter.GetBestFragment(strTitle, model.Title); model.Title = string.IsNullOrEmpty(transStr) ? model.Title : transStr; } if (!string.IsNullOrEmpty(strAbstract)) { var transStr = highlighter.GetBestFragment(strAbstract, model.Abstract); model.Abstract = string.IsNullOrEmpty(transStr) ? model.Abstract : transStr; } } catch { return(model); } return(model); }
/// <summary> /// 在title和content字段中查询数据 /// </summary> /// <param name="keyword"></param> /// <returns></returns> public List <SearchResult> Search(string keyword) { string[] fileds = { "title", "content" };//查询字段 QueryParser parser = null; parser = new MultiFieldQueryParser(version, fileds, analyzer);//多个字段查询 Query query = parser.Parse(keyword); int n = 1000; IndexSearcher searcher = new IndexSearcher(directory_luce, true);//true-表示只读 TopDocs docs = searcher.Search(query, (Filter)null, n); if (docs == null || docs.TotalHits == 0) { return(null); } else { List <SearchResult> list = new List <SearchResult>(); int counter = 1; foreach (ScoreDoc sd in docs.ScoreDocs)//遍历搜索到的结果 { try { Document doc = searcher.Doc(sd.Doc); int id = int.Parse(doc.Get("id")); string title = doc.Get("title"); string content = doc.Get("content"); string blogTag = doc.Get("blogTag"); string url = doc.Get("url"); int flag = int.Parse(doc.Get("flag")); int clickQuantity = int.Parse(doc.Get("clickQuantity")); string createdate = doc.Get("createdate"); PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>"); PanGu.HighLight.Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new PanGu.Segment()); highlighter.FragmentSize = 50; content = highlighter.GetBestFragment(keyword, content); string titlehighlight = highlighter.GetBestFragment(keyword, title); if (titlehighlight != "") { title = titlehighlight; } list.Add(new SearchResult(title, content, url, blogTag, id, clickQuantity, flag)); } catch (Exception ex) { Console.WriteLine(ex.Message); } counter++; } return(list); } //st.Stop(); //Response.Write("查询时间:" + st.ElapsedMilliseconds + " 毫秒<br/>"); }
/// <summary> /// 将关键字进行高亮显示 /// </summary> /// <param name="keywords">属性和关键字的字典</param> /// <param name="t">高亮显示的对象</param> /// <returns></returns> public AbstractFileBase InitHightLight(Dictionary <string, string> keywords, AbstractFileBase t) { if (t == null) { throw new Exception("需要高亮显示的对象不存在"); } SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>"); Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment()); highlighter.FragmentSize = MAXFRAGMENTSIZE; foreach (var item in hightLightFields) { switch (item) { //case HightLightField.FileName: // string fileName = string.Empty; // keywords.TryGetValue("fileName", out fileName); // if (!string.IsNullOrWhiteSpace(fileName)) // { // t.FileName = highlighter.GetBestFragment(fileName, t.FileName); // } // break; //case HightLightField.FilePath: // string filePath = string.Empty; // keywords.TryGetValue("filePath", out filePath); // if (!string.IsNullOrWhiteSpace(filePath)) // { // t.FilePath = highlighter.GetBestFragment(filePath, t.FilePath); // } // break; //case HightLightField.CreateTime: // string createTime = string.Empty; // keywords.TryGetValue("createTime", out createTime); // if (!string.IsNullOrWhiteSpace(createTime)) // { // t.CreateTime = highlighter.GetBestFragment(createTime, t.CreateTime); // } // break; case HightLightField.Content: string content = string.Empty; keywords.TryGetValue("content", out content); if (!string.IsNullOrWhiteSpace(content)) { //有问题 t.Content = highlighter.GetBestFragment(content, t.Content); } break; //case HightLightField.UpLoadPersonId: // string upLoadPersonId = string.Empty; // keywords.TryGetValue("upLoadPersonId", out upLoadPersonId); // if (!string.IsNullOrWhiteSpace(upLoadPersonId)) // { // //有问题 // t.UpLoadPersonId = highlighter.GetBestFragment(upLoadPersonId, t.UpLoadPersonId); // } // break; } } return(t); }
/// <summary> /// 把content按照keywords进行高亮 /// </summary> /// <param name="keywords"></param> /// <param name="content"></param> /// <returns></returns> private static string Highlight(string keywords, string content) { SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<strong>", "</strong>"); Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment()); highlighter.FragmentSize = 200; return(highlighter.GetBestFragment(keywords, content)); }
public static string HightLight(string keyword, string content) { //创建HTMLFormatter,参数为高亮单词的前后缀 var simpleHtmlFormatter = new SimpleHTMLFormatter("<font style=\"font-style:normal;color:#cc0000;\"><b>", "</b></font>"); //创建 Highlighter ,输入HTMLFormatter 和 盘古分词对象Semgent var highlighter = new Highlighter(simpleHtmlFormatter, new Segment()); //设置每个摘要段的字符数 highlighter.FragmentSize = 1000; //获取最匹配的摘要段 return highlighter.GetBestFragment(keyword, content); }
private Record SetHighlighter(string Keywords, Record model) { SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>"); Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment()); highlighter.FragmentSize = 50; string strTitle; string strContent; strTitle = highlighter.GetBestFragment(Keywords, model.Title); if (strTitle != "") { model.Title = strTitle; } strContent = highlighter.GetBestFragment(Keywords, model.Content); if (strContent != "") { model.Content = strContent; } else if (model.Content != "") { Random r = new Random(); int start = r.Next(0, model.Content.Length - 1); int len = model.Content.Length - 1 - start; if (len > 20) { model.Content = model.Content.Substring(start, 20); } else { model.Content = model.Content.Substring(start); } } return(model); }
/// <summary> /// 设置关键字高亮 /// </summary> /// <param name="dicKeywords">关键字列表</param> /// <param name="model">返回的数据模型</param> /// <returns></returns> private Article SetHighlighter(Dictionary <string, string> dicKeywords, Article model) { SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"green\">", "</font>"); Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment()); highlighter.FragmentSize = 50; string strTitle = string.Empty; string strContent = string.Empty; dicKeywords.TryGetValue("title", out strTitle); dicKeywords.TryGetValue("content", out strContent); if (!string.IsNullOrEmpty(strTitle)) { model.Title = highlighter.GetBestFragment(strTitle, model.Title); } if (!string.IsNullOrEmpty(strContent)) { model.Content = highlighter.GetBestFragment(strContent, model.Content); } return(model); }
/// <summary> /// 设置高亮 /// </summary> /// <param name="keywords">关键字</param> /// <param name="matchField">字段信息</param> /// <returns></returns> public void SetHighLight(string keywords, LnMatchField matchField) { var format = new SimpleHTMLFormatter(matchField.KeywordPrefix, matchField.KeywordSuffix); var highLighter = new Highlighter(format, new Segment()); highLighter.FragmentSize = matchField.FragmentSize < 1 ? this.Value.Length : matchField.FragmentSize; var newValue = highLighter.GetBestFragment(keywords, this.Value); if (string.IsNullOrEmpty(newValue) == false) { this.Value = newValue; } }
//需要添加PanGu.HighLight.dll的引用 /// <summary> /// 搜索结果高亮显示 /// </summary> /// <param name="keyword"> 关键字 </param> /// <param name="content"> 搜索结果 </param> /// <returns> 高亮后结果 </returns> public static string HightLight(string keyword, string content) { //创建HTMLFormatter,参数为高亮单词的前后缀 PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font style=\"font-style:normal;color:#ff0000;\"><b>", "</b></font>"); //创建 Highlighter ,输入HTMLFormatter 和 盘古分词对象Semgent PanGu.HighLight.Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment()); //设置每个摘要段的字符数 highlighter.FragmentSize = 1000; //获取最匹配的摘要段 return(highlighter.GetBestFragment(keyword, content)); }
//高亮显示 public static string CreateHightLight(string keywords, string Content) { PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>"); //创建Highlighter ,输入HTMLFormatter 和盘古分词对象Semgent PanGu.HighLight.Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment()); //设置每个摘要段的字符数 highlighter.FragmentSize = 150; //获取最匹配的摘要段 var result = highlighter.GetBestFragment(keywords, Content); return(string.IsNullOrEmpty(result) ? Content : result); }
private Record SetHighlighter(string Keywords, Record model) { SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>"); Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment()); highlighter.FragmentSize = 50; // string strTitle = string.Empty; //string strContent = string.Empty; //dicKeywords.TryGetValue("title", out strTitle); //dicKeywords.TryGetValue("content", out strContent); // model.Title = highlighter.GetBestFragment(Keywords, model.Title); model.Content = highlighter.GetBestFragment(Keywords, model.Content); return(model); }
/// <summary> /// 设置关键字高亮 /// </summary> /// <param name="dicKeywords">关键字列表</param> /// <param name="model">返回的数据模型</param> /// <returns></returns> private CustomerEntity SetHighlighter(Dictionary <string, string> dicKeywords, CustomerEntity model) { SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>"); Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment()); highlighter.FragmentSize = 250; string strTitle = string.Empty; string strContent = string.Empty; dicKeywords.TryGetValue("FullName", out strTitle); if (!string.IsNullOrEmpty(strTitle)) { string title = model.FullName; model.FullName = highlighter.GetBestFragment(strTitle, model.FullName); if (string.IsNullOrEmpty(model.FullName)) { model.FullName = title; } } return(model); }
/// <summary> /// 设置关键字高亮 /// </summary> /// <param name="dicKeywords">关键字列表</param> /// <param name="model">返回的数据模型</param> /// <returns></returns> private 供应商读出Lucene SetHighlighter(Dictionary<string, string> dicKeywords, 供应商读出Lucene model) { SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>"); Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment()); highlighter.FragmentSize = 50; string strTitle = string.Empty; dicKeywords.TryGetValue("title", out strTitle); if (!string.IsNullOrEmpty(strTitle)) { string title = highlighter.GetBestFragment(strTitle, model.供应商.企业基本信息.企业名称); if (!string.IsNullOrEmpty(title)) model.供应商.企业基本信息.企业名称 = title; } return model; }
/// <summary> /// 在title和content字段中查询数据 /// </summary> /// <param name="keyword"></param> /// <returns></returns> public List<SearchResult> Search(string keyword) { string[] fileds = { "title", "content" };//查询字段 QueryParser parser = null; parser = new MultiFieldQueryParser(version, fileds, analyzer);//多个字段查询 Query query = parser.Parse(keyword); int n = 1000; IndexSearcher searcher = new IndexSearcher(directory_luce, true);//true-表示只读 TopDocs docs = searcher.Search(query, (Filter)null, n); if (docs == null || docs.TotalHits == 0) { return null; } else { List<SearchResult> list = new List<SearchResult>(); int counter = 1; foreach (ScoreDoc sd in docs.ScoreDocs)//遍历搜索到的结果 { try { Document doc = searcher.Doc(sd.Doc); int id = int.Parse(doc.Get("id")); string title = doc.Get("title"); string content = doc.Get("content"); string blogTag = doc.Get("blogTag"); string url = doc.Get("url"); int flag = int.Parse(doc.Get("flag")); int clickQuantity = int.Parse(doc.Get("clickQuantity")); string createdate = doc.Get("createdate"); PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>"); PanGu.HighLight.Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new PanGu.Segment()); highlighter.FragmentSize = 50; content = highlighter.GetBestFragment(keyword, content); string titlehighlight = highlighter.GetBestFragment(keyword, title); if (titlehighlight != "") title = titlehighlight; list.Add(new SearchResult(title, content, url, blogTag, id, clickQuantity, flag)); } catch (Exception ex) { Console.WriteLine(ex.Message); } counter++; } return list; } //st.Stop(); //Response.Write("查询时间:" + st.ElapsedMilliseconds + " 毫秒<br/>"); }
/// <summary> /// 把content按照keywords进行高亮 /// </summary> /// <param name="keywords"></param> /// <param name="content"></param> /// <returns></returns> private static string Highlight(string keywords, string content) { SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<strong>", "</strong>"); Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment()); highlighter.FragmentSize = 200; return highlighter.GetBestFragment(keywords, content); }
public static List<SeekWebNewsItem> Search(String indexDir, String q, int pageLen, int pageNo, out int recCount) { string keywords = q; IndexSearcher search = new IndexSearcher(indexDir); q = GetKeyWordsSplitBySpace(q, new PanGuTokenizer()); QueryParser queryParser = new QueryParser("contents", new PanGuAnalyzer(true)); Query query = queryParser.Parse(q); QueryParser titleQueryParser = new QueryParser("title", new PanGuAnalyzer(true)); Query titleQuery = titleQueryParser.Parse(q); BooleanQuery bq = new BooleanQuery(); bq.Add(query, BooleanClause.Occur.SHOULD); bq.Add(titleQuery, BooleanClause.Occur.SHOULD); Hits hits = search.Search(bq); List<SeekWebNewsItem> result = new List<SeekWebNewsItem>(); recCount = hits.Length(); int i = (pageNo - 1) * pageLen; while (i < recCount && result.Count < pageLen) { SeekWebNewsItem news = null; try { news = new SeekWebNewsItem(); news.NewsId = long.Parse(hits.Doc(i).Get("id")); string Title = hits.Doc(i).Get("title"); string NewsText = hits.Doc(i).Get("contents"); String strTime = hits.Doc(i).Get("time"); PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>"); PanGu.HighLight.Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment()); highlighter.FragmentSize = 50; news.Abstract = highlighter.GetBestFragment(keywords, NewsText); news.TitleHighLighter = highlighter.GetBestFragment(keywords, Title); if (string.IsNullOrEmpty(news.TitleHighLighter)) { news.TitleHighLighter = Title; } } catch (Exception e) { Console.WriteLine(e.Message); } finally { result.Add(news); i++; } } search.Close(); return result; }
private string HighLight(string keyword, string content) { SimpleHTMLFormatter simpleHtmlFormatter = new SimpleHTMLFormatter("<font style=\"color:#cc0000;\"><b>", "</b></font>"); Highlighter highlighter = new Highlighter(simpleHtmlFormatter, new Segment()); highlighter.FragmentSize = 1000; return highlighter.GetBestFragment(keyword, content); }