Example #1
0
        private string Highlight(Scorer scorer, string text, bool isHtml)
        {
            var encoder = isHtml ? (LuceneEncoder) new DefaultEncoder() : new SimpleHTMLEncoder();

            if (scorer == null)
            {
                return(encoder.encodeText(text)); // nothing to highlight
            }
            // Build the TokenStream on top of the text.

            var analyzer    = isHtml ? (Analyzer) new SimpleHtmlAnalyzer() : new SimpleAnalyzer();
            var tokenStream = analyzer.tokenStream(Field, new java.io.StringReader(text));

            // Build the highlighter.

            var highlighter = new LuceneHighlighter(
                new SimpleHTMLFormatter(_startTag, _endTag),
                encoder, scorer);

            highlighter.setTextFragmenter(new NullFragmenter());

            // Perform highlighting.

            var highlightedText = highlighter.getBestFragment(tokenStream, text);

            return(highlightedText ?? encoder.encodeText(text));
        }
Example #2
0
        public string GetBestContent(string text)
        {
            if (_contentScorer == null)
            {
                return(string.Empty);
            }

            // Build the TokenStream on top of the text.

            Analyzer analyzer    = new SimpleAnalyzer();
            var      tokenStream = analyzer.tokenStream(Field, new java.io.StringReader(text));

            // Build the highlighter.

            var highlighter = new LuceneHighlighter(
                new SimpleHTMLFormatter(_startTag, _endTag),
                new SimpleHTMLEncoder(), _contentScorer);

            highlighter.setTextFragmenter(new SimpleSpanFragmenter(_contentScorer, FragmentSize));

            // Perform highlighting.

            var highlightedText = highlighter.getBestFragments(tokenStream, text,
                                                               MaxFragments, Separator);

            return(!string.IsNullOrEmpty(highlightedText)
                ? Separator + highlightedText + Separator
                : string.Empty);
        }
Example #3
0
        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);
            }
        }
Example #4
0
        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);
            }
        }