You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by ke...@hotmail.com on 2003/04/10 06:09:05 UTC
a simple highlight resolvent
hi everyone,
I am new one in lucene and got a lot help in last 2 weeks. Here I want to share a a simple highlight resolvent. Of course I have try de.iqcomputing.lucene. Here just a simple way base the demo. Here is the source. It's a little inept. And I don't don't why it can't compile if I use String.replaceAll(), So I write another method to handle it. Any suggestion are welcomed.
Kerr.
to use it in results.jsp. --------------------------------
hl = new org.apache.lucene.demo.HightLighter(query.toString("contents"));
...
summary = hl.getHighlightFile(path);
if (summary == null || summary.length() < 100 ){
summary = doc.get("summary");
} catch (Exception e){
summary = doc.get("summary");
}
...
---------------------------------------
package org.apache.lucene.demo;
import java.io.*;
import java.util.*;
import org.apache.lucene.demo.html.HTMLParser;
public class HightLighter {
String[] keys;
boolean[] b;
public HightLighter(String query) {
HashMap highlight = new HashMap();
StringTokenizer st = new StringTokenizer(query, "\"");
String aToken;
int off = 0;
while(st.hasMoreTokens()){
aToken = st.nextToken();
char aChar;
String dest = "";
for(int i=0; i<aToken.length(); i++){
aChar = aToken.charAt(i);
if (aChar != '+' &&
aChar != '-' &&
aChar != '(' &&
aChar != ')' &&
aChar != ' '){
dest = dest + aChar;
}
}
if (dest.length() > 0) {
highlight.put(Integer.toString(off) , dest);
off = off + 1;
}
}
keys = new String[highlight.size()];
b = new boolean[highlight.size()];
for(int c=0; c<highlight.size(); c++){
keys[c] = (String)highlight.get(Integer.toString(c));
b[c] = false;
}
}
public String replaceKey(String sourceString, String key) {
String destString = "";
int i = sourceString.indexOf(key);
while ( i != -1 ) {
destString = destString + sourceString.substring(0,i)
+ "<font color=\"red\">" + key + "</font>";
sourceString = sourceString.substring(i+key.length());
i = sourceString.indexOf(key);
}
destString = destString+sourceString;
return destString;
}
public String getHighlight(String string){
for(int i=0; i<keys.length; i++){
if (string.indexOf(keys[i]) != -1){
//string = string.replaceAll(keys[i], "<font color=\"red\">" + keys[i] + "</font>");
string = replaceKey(string, keys[i]);
}
}
return string;
}
public String getHighlightFile(String path)
throws IOException, InterruptedException {
HTMLParser parser = new HTMLParser(new File(path));
Reader fr = parser.getReader();
String hlString = this.getHighlight(fr);
fr.close();
return hlString;
}
public String getHighlight(Reader reader){
try{
int size = 5 - keys.length;
if ( size > 0) {
size = size * 100;
} else {
size = 100;
}
char[] buffer = new char[size];
StringBuffer last = new StringBuffer("..");
String temp;
boolean end = false;
for(int c=0; c<b.length; c++){
b[c] = false;
}
while ((reader.read(buffer) != -1) && !end){
temp = new String(buffer);
//System.out.println(temp);
//temp = temp.replaceAll("\r\n", " ");
for(int i=0; i<keys.length; i++){
if (!b[i] && temp.indexOf(keys[i]) != -1){
b[i] = true;
//temp = temp.replaceAll(keys[i], "<font color=\"red\">" + keys[i] + "</font>");
last.append("." + temp + "..");
}
}
for(int i=0; i<keys.length; i++){
if (!b[i]){
end = false;
break;
} else {
end = true;
}
}
}
last.append(".");
if (last.length() < 100){
return null;
} else {
return(this.getHighlight(last.toString()));
}
} catch (Exception e){
return null;
}
}
}