import java.io.File;
import java.io.FileOutputStream;
import java.io.StringReader;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.highlight.Fragmenter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.util.Version;
public class myclass {
public static void main(String[] args) {
FileOutputStream file = null;
String result = "<html><body><div>(i) the recognised association shall have the approval of the Forward Markets Commission established under the Forward Contracts (Regulation) Act, 1952 (74 of 1952) in respect of trading in derivatives and shall function in accordance with the guidelines or conditions laid down by the Forward Markets Commission; </div> <body> </html>";
try {
TermQuery query = new TermQuery(new Term("f", "Forward Markets"));
QueryScorer scorer = new QueryScorer(query);
SimpleHTMLFormatter formatter = new SimpleHTMLFormatter(
"<span class=\"highlight\">", "</span>");
org.apache.lucene.search.highlight.Highlighter highlighter = new org.apache.lucene.search.highlight.Highlighter(
formatter, scorer);
Fragmenter fragmenter = new SimpleFragmenter(result.length());
highlighter.setTextFragmenter(fragmenter);
TokenStream tokenStream = new StandardAnalyzer(Version.LUCENE_29)
.tokenStream("f",
new StringReader(result));
String result1 = highlighter.getBestFragments(tokenStream, result,
1, "...");
if (result1.length() == 0) {
result1 = result;
}
String finalhtml = "<html>" + "<style>\n" + ".highlight{\n"
+ " background: yellow;\n" + "}\n" + "</style>" + "<body>"
+ result1 + "</body></html>";
byte[] contentInBytes = finalhtml.getBytes();
file = new FileOutputStream(new File("E:\\myhtml.html"));
file.write(contentInBytes);
file.flush();
file.close();
} catch (Exception ex) {
}
}
}
这是我的代码,当我设置文本“已识别”或“关联”其突出显示完美但是当我设置 text="Forward Markets" 它的 noe 突出显示请告诉我在哪里做错请帮助我如何突出显示文本,如果空间会来。