Erik,

Thank you so very much for the working example of using Highlighter with the search results. I got the sample code to work. I am kinda curious, why the Highlighter class only returns Fragments of the document. Are there any constraints for doing that? Also I looked at the code of the Highligher class and all of the methods are for getBestFragments / getBestDocFragments. Is there a way to get the whole document in result. Highlighted for the query string? Is it something that would have to be done by the individual?

Thanks a lot for all your help, support and time.

Erik Hatcher wrote:

On Jun 28, 2004, at 5:18 PM, Hetan Shah wrote:

Is it possible to use highlighter successfully in the demos the web demo to be specific. Has any one tried out there? If so can they explain me how to go about it any code sample is really very appreciated.


Straight from Lucene in Action:

public class HighlightIt {
  private static final String text =
      "Contrary to popular belief, Lorem Ipsum is" +
      " not simply random text. It has roots in a piece of" +
      " classical Latin literature from 45 BC, making it over" +
      " 2000 years old. Richard McClintock, a Latin professor" +
      " at Hampden-Sydney College in Virginia, looked up one" +
      " of the more obscure Latin words, consectetur, from" +
      " a Lorem Ipsum passage, and going through the cites" +
      " of the word in classical literature, discovered the" +
      " undoubtable source. Lorem Ipsum comes from sections" +
      " 1.10.32 and 1.10.33 of \"de Finibus Bonorum et" +
      " Malorum\" (The Extremes of Good and Evil) by Cicero," +
      " written in 45 BC. This book is a treatise on the" +
      " theory of ethics, very popular during the" +
      " Renaissance. The first line of Lorem Ipsum, \"Lorem" +
      " ipsum dolor sit amet..\", comes from a line in" +
      " section 1.10.32.";  // from http://www.lipsum.com/

  public static void main(String[] args) throws IOException {
    String filename = args[0];

    if (filename == null) {
      System.err.println("Usage: HighlightIt <filename>");
      System.exit(-1);
    }

//    TermQuery query = new TermQuery(new Term("f", "ipsum"));
    PhraseQuery query = new PhraseQuery();
    query.add(new Term("f", "lorem"));
    query.add(new Term("f", "ipsum"));
    QueryScorer scorer = new QueryScorer(query);
    SimpleHTMLFormatter formatter =
        new SimpleHTMLFormatter("<span class=\"highlight\">",
            "</span>");
    Highlighter highlighter = new Highlighter(formatter, scorer);
    Fragmenter fragmenter = new SimpleFragmenter(50);
    highlighter.setTextFragmenter(fragmenter);

    TokenStream tokenStream = new StandardAnalyzer()
        .tokenStream("f", new StringReader(text));

    String result =
        highlighter.getBestFragments(tokenStream, text, 5, "...");

    FileWriter writer = new FileWriter(filename);
    writer.write("<html>");
    writer.write("<style>\n" +
        ".highlight {\n" +
        " background: yellow;\n" +
        "}\n" +
        "</style>");
    writer.write("<body>");
    writer.write(result);
    writer.write("</body></html>");
    writer.close();
  }
}

I just added the PhraseQuery in there instead of the TermQuery that is commented out. Highlighter works well with phrases also (although highlights each term individually, not the breadth of the phrase by itself). The above code runs like it says in the usage statement, give it a filename to save an HTML file that shows the terms highlighted in yellow.

    Erik


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to