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" を設定すると、その強調表示はありません。