KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > lucene > search > LuceneHighlighter


1 /*
2  * Copyright 24/04/2005 - Vicinity - www.vicinity.com All rights reserveds
3  */

4 package org.javabb.lucene.search;
5
6
7 import java.io.IOException JavaDoc;
8 import java.io.StringReader JavaDoc;
9
10 import org.apache.lucene.analysis.Analyzer;
11 import org.apache.lucene.analysis.TokenStream;
12 import org.apache.lucene.document.Document;
13 import org.apache.lucene.document.Field;
14 import org.apache.lucene.index.IndexReader;
15 import org.apache.lucene.index.IndexWriter;
16 import org.apache.lucene.queryParser.QueryParser;
17 import org.apache.lucene.search.Query;
18 import org.apache.lucene.search.highlight.Formatter;
19 import org.apache.lucene.search.highlight.Highlighter;
20 import org.apache.lucene.search.highlight.QueryScorer;
21 import org.apache.lucene.search.highlight.Scorer;
22 import org.apache.lucene.search.highlight.SimpleFragmenter;
23 import org.apache.lucene.store.Directory;
24 import org.apache.lucene.store.RAMDirectory;
25
26 import org.javabb.lucene.analysis.PortugueseAnalyzer;
27
28
29 /**
30  *
31  * @author Marcos Silva Pereira - marcos.pereira@vicinity.com
32  *
33  * @since 24/04/2005
34  *
35  * @version $Id$
36  */

37 public class LuceneHighlighter {
38
39     private static final String JavaDoc CONTENTS_FIELD = "contents";
40
41     // TODO Remove and make Spring hold it
42
private static final Analyzer analyzer = new PortugueseAnalyzer();
43
44     /**
45      *
46      */

47     public String JavaDoc highlight( String JavaDoc text, String JavaDoc query, String JavaDoc separator,
48             int fragSize, int numFrags, boolean complete ) {
49
50         String JavaDoc result = "";
51         try {
52
53             Directory ramDir = new RAMDirectory();
54
55             addDocument(text, ramDir);
56
57             IndexReader reader = IndexReader.open(ramDir);
58
59             Query queryObj;
60             queryObj = QueryParser.parse(query, CONTENTS_FIELD, analyzer);
61             queryObj = queryObj.rewrite(reader);
62
63             Scorer scorer = new QueryScorer(queryObj);
64             Formatter formatter = new CSSFormatter("highlight");
65             Highlighter highlighter = new Highlighter(formatter, scorer);
66
67             highlighter.setTextFragmenter(new SimpleFragmenter(fragSize));
68
69             TokenStream token;
70             token = analyzer
71                     .tokenStream(CONTENTS_FIELD, new StringReader JavaDoc(text));
72
73             if (!complete) {
74
75                 result = highlighter.getBestFragments(token, text, numFrags,
76                     separator);
77
78             } else {
79
80                 result = highlighter.getCompleteTextHighlight(token, text);
81
82             }
83
84         } catch (Exception JavaDoc e) {
85
86             // TODO logger
87

88         } finally {
89
90             result = avoidEmpty(result, text);
91
92         }
93
94         return result;
95
96     }
97
98     /**
99      * @param actualText
100      * @param ramDir
101      * @throws IOException
102      */

103     private void addDocument( String JavaDoc actualText, Directory ramDir )
104             throws IOException JavaDoc {
105
106         Field field = new Field(CONTENTS_FIELD, actualText, true, true, true);
107
108         Document document = new Document();
109
110         document.add(field);
111
112         IndexWriter writer = new IndexWriter(ramDir, analyzer, true);
113
114         writer.addDocument(document);
115         writer.optimize();
116         writer.close();
117
118     }
119
120     private String JavaDoc avoidEmpty( String JavaDoc string , String JavaDoc text) {
121
122         String JavaDoc result = string;
123
124         if (string == null || "".equals(string.trim())) {
125
126             int length = Math.min(200, text.length());
127             result = text.substring(0, length);
128
129         }
130
131         return result;
132     }
133 }
134
Popular Tags