private string Highlight(Highlighter highlighter, string description) { description = SimpleHTMLEncoder.HtmlEncode(description); if (highlighter != null) { var stream = _analyzer.TokenStream("", new StringReader(description)); var sample = highlighter.GetBestFragments(stream, description, 2, "..."); if (!string.IsNullOrEmpty(sample)) { return(sample); } } return(description); }
protected string Highlight(LuceneQuery query, string text, bool htmlEncodeOutput) { if (string.IsNullOrEmpty(text)) { return(string.Empty); } try { Encoder encoder; if (htmlEncodeOutput) { encoder = new SimpleHTMLEncoder(); } else { encoder = new DefaultEncoder(); } if (query == null) { return(encoder.encodeText(text)); // nothing to highlight } // Build the highlighter. var formatter = new SimpleHTMLFormatter(_configuration.StartTag, _configuration.EndTag); var highlighter = new LuceneHighlighter(formatter, encoder, new QueryScorer(query)); highlighter.setTextFragmenter(new NullFragmenter()); // Perform highlighting. var highlightedHtml = highlighter.getBestFragment(_contentAnalyzer, string.Empty, text); return(highlightedHtml ?? encoder.encodeText(text)); } catch (Exception) { // on error just return the original string return(text); } }
protected string Summarize(LuceneQuery query, string text, bool htmlEncodeOutput) { if (query == null || string.IsNullOrEmpty(text)) { return(null); } try { // Build the highlighter. var formatter = new SimpleHTMLFormatter(_configuration.StartTag, _configuration.EndTag); var scorer = new QueryScorer(query); Encoder encoder; if (htmlEncodeOutput) { encoder = new SimpleHTMLEncoder(); } else { encoder = new DefaultEncoder(); } var highlighter = new LuceneHighlighter(formatter, encoder, scorer); highlighter.setTextFragmenter(new SimpleSpanFragmenter(scorer, _configuration.FragmentSize)); // Perform highlighting. var tokenStream = _contentAnalyzer.tokenStream(string.Empty, new java.io.StringReader(text)); return(highlighter.getBestFragments(tokenStream, text, _configuration.MaxFragments, _configuration.Separator)); } catch (Exception) { // on error just return the original string return(text); } }