解决jive搜索结果中的中文搜索字符串高亮度显示的方法

解决jive搜索结果中的中文搜索字符串高亮度显示的方法
1.问题:在搜索结果中,命中的中文字符串不能正确地高亮度显示。
2.工作过程:Jive用搜索字符串生成一个正则表达式,然后在搜索得到的结果文本中用该表达式做替换操作,如果匹配成功,就进行替换操作。不能匹配,则不进行替换操作。替换操作的结果是将Html格式语句和搜索字符串插入到结果文本中,显示时候就可以看见高亮度的字符串。
jive中的jakarta-oro-XXX包中含有兼容Perl5正则表达式的类,jive使用其substitute()方法实现替换操作。
3.原因:jive生成如下的Perl5锚模式的正则表达式:
s/\b(XXX)\b/<font style='background-color:ffff00'><b>$1<\/b><\/font>/igm
XXX表示匹配字符串。上式含意看看正则表达式的资料就明白了。
问题出在模式部分"\b(XXX)\b"。这样的匹配是要求首尾做边界匹配(单词匹配),中文书写格式中,边界一般是开始的空格和句中及结尾的标点符号,所以完成匹配很困难。如:
月落乌啼霜满天,江枫渔火对愁眠。
只有”\b(月落乌啼霜满天)\b”和“\b(江枫渔火对愁眠)\b”两个匹配是正确的,诸如”\b(月落)\b”、”\b(乌啼)\b”、”\b(满天)\b”等都无法匹配。因此也就不能完成替换操作。只有搜索字符串正好被分隔符包围,才能完成替换操作。
所以实际使用中经常是有时中文可以高亮度显示,而大部分则不行。就是因为一般的查询很难满足上述条件。
3.修改方法:
(1)分析此处的匹配要求是:中文任意位置匹配,英文单词匹配。
要求写出这样的正则表达式挺难(不能?),如有熟悉正则表达式的请指正。
(2)最简单的方法是去掉匹配的参数b,做任意匹配处理。即使用这样的正则表达式:
s/(XXX)/<font style='background-color:ffff00'><b>$1<\/b><\/font>/igm
找到com.jivesoftware.util.StringUtils.java文件中的highlightWords()方法的下列语句,修改为注释中的语句。
regexp.insert(0, "s/\\b("); // 修改此句为:regexp.insert(0, "s/(");
// The word list is here already, so just append the rest.
regexp.append(")\\b/"); // 修改此句为regexp.append(")/");
regexp.append(startHighlight);
regexp.append("$1");
regexp.append(endHighlight);
regexp.append("/igm");
这样修改最方便,对中文效果不错,但是对英文会出现单词中匹配,如:
geve me a pen or pencil.
搜索“pen”时,pen和pencil中的前三个字母都会作为高亮度显示出来,后一种情况看起来很别扭。
(2)如果做的考究一点,可以将中文和英文分开处理。使用这样的正则表达式:
s/(XXX|\bYYY\b)/<font style='background-color:ffff00'><b>$1$2<\/b><\/font>/igm
XXX表示中文匹配字符串,YYY表示英文匹配字符串。修改的程序如下:
/**
* Highlights words in a string. Words matching ignores case. The actual
* higlighting method is specified with the start and end higlight tags.
* Those might be beginning and ending HTML bold tags, or anything else.<p>
*
* This method is under the Jive Open Source Software License and was
* written by Mark Imbriaco.
*
* @param string the String to highlight words in.
* @param words an array of words that should be highlighted in the string.
* @param startHighlight the tag that should be inserted to start highlighting.
* @param endHighlight the tag that should be inserted to end highlighting.
* @return a new String with the specified words highlighted.
*/
/**
* 一个匹配"中文" 和 “english"的正则表达式:
* 英文做单词匹配(单词首尾边界匹配),中文任意匹配。
* s/(中文|\benglish\b)/<font style='background-color:ffff00'><b>$1$2<\/b><\/font>/igm
* 选项说明
* i 忽略模式中的大小写
* g 改变模式中的所有匹配
* m 将待匹配串视为多行
*
* 2003-6-22
*/

public static final String highlightWords(String string, String[] words,
String startHighlight,
String endHighlight) {
if (string == null || words == null ||
startHighlight == null || endHighlight == null) {
return null;
}

StringBuffer regexp = new StringBuffer();

// Iterate through each word and generate a word list for the regexp.
for (int x = 0; x < words.length; x++) {
// Excape "|" and "/" to keep us out of trouble in our regexp.
words[x] = perl5Util.substitute("s#([\\|\\/\\.])#\\\\$1$2g", words[x]);
if (regexp.length() > 0) {
regexp.append("|");
}
if (words[x].charAt(0) > 127) {
// 中文不做边界匹配
regexp.append(words[x]);
}
else {
// 英文单词做边界匹配检查
try {
PatternCompiler compiler = new Perl5Compiler();
String regex = "\\b" + words[x].toString() + "\\b";
Pattern pattern = compiler.compile(regex);
PatternMatcher matcher = new Perl5Matcher();
if (matcher.contains(string, pattern)) {
// 如果有边界匹配的单词,正则表达式使用边界匹配
regexp.append(regex.toString());
}
else {
// 使用任意匹配
regexp.append(words[x]);
}
}
catch (Exception e) {
System.out.println(e);
}
}
}

// Escape the regular expression delimiter ("/").
startHighlight = perl5Util.substitute("s#\\/#\\\\/g", startHighlight);
endHighlight = perl5Util.substitute("s#\\/#\\\\/g", endHighlight);

// Build the regular expression. insert() the first part.
regexp.insert(0, "s/(");
// The word list is here already, so just append the rest.
regexp.append(")/");
regexp.append(startHighlight);
regexp.append("$1$2");
regexp.append(endHighlight);
regexp.append("/igm");

// Do the actual substitution via a simple regular expression.
return perl5Util.substitute(regexp.toString(), string);
}

4.给搜索结果的标题(subject)部分加上高亮度显示
在search.jsp文件中找到有highlightWords()方法的一行,在前面加入两句:
String subject = StringUtils.escapeHTMLTags(message.getUnfilteredSubject());
subject = StringUtils.highlightWords(subject, queryWords, "<font style='background-color:ffff00'><b>", "</b></font>");
后面将subject给浏览器就行了。参见下面代码的注释。
......
while (results.hasNext()) {
ForumMessage message = (ForumMessage)results.next();
// 添加的语句。subject中命中字符串的高亮显示
String subject = StringUtils.escapeHTMLTags(message.getUnfilteredSubject());
subject = StringUtils.highlightWords(subject, queryWords, "<font style='background-color:ffff00'><b>", "</b></font>");
//
String body = StringUtils.escapeHTMLTags(message.getUnfilteredBody());
body = StringUtils.chopAtWord(body, 150) + " ...";
body = StringUtils.highlightWords(body, queryWords, "<font style='background-color:ffff00'><b>", "</b></font>");
long mID = message.getID();
......
<td>
<font class=p2 face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>">
<a href="thread.jsp?forum=<%= fID %>&thread=<%= tID %>&message=<%= mID %>&redirect=true&hilite=true&q=<%= StringUtils.ISOtoGBK(queryText) %>"
<%-- 将message.getSubject()改为subject。subject高亮度显示 --%>
<%-- ><%= message.getSubject() %></a> --%>
><%= subject %></a>
......
<% } // end while loop
%>

参见正则表达式的有关资料。