Java 中的搜索荧光笔
boldHighlight 方法获取文本字符串并通过 标签突出显示其中的 q 关键字
colorHighlight 方法获取文本字符串并通过 具有 12 种交替颜色
String text = "The use of hello as a telephone greeting has been credited to Thomas
Edison; according to one source, he expressed his surprise with a
misheard Hullo. Alexander Graham Bell initially used Ahoy (as used on
ships) as a telephone greeting"
String keywords = "HELLO Surprise"
boldHighlight(text, keywords); // 将产生:
使用
hello
作为电话问候语归功于托马斯·爱迪生;据一位消息人士称,他用听错的“哈啰”表达了他的惊讶
。亚历山大·格雷厄姆·贝尔最初使用 Ahoy(船上使用的)作为电话问候语“
` colorHighlight(text, keywords); // 将产生:
使用
hello
作为电话问候语已被认为是托马斯·爱迪生的功劳;>根据一位消息来源,他用听错的哈啰表达了他的惊喜
。亚历山大·格雷厄姆·贝尔最初使用 Ahoy(船上使用的)作为电话问候语
问题:
我可以使用第三方库之类的东西来完成与以下方法类似的工作吗?或者,如果您查看代码,是否有一些可以改进的地方,以使性能更好和/或使其更优雅?`
private static final String[] colors = new String[]{"ffff66", "a0ffff", "99ff99", "ff9999", "ff66ff", "880000", "00aa00", "886800", "004699", "990099", "ffff66", "a0ffff"};
public static String safeCharWithSpace(String input) {
input = input.trim();
return Normalizer.normalize(input.toLowerCase(), Normalizer.Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", "")
.replaceAll("[^\\p{Alnum}]+", " ");
}
private static String prepQuery(String q) {
try {
log.debug("qr encoded: " + q);
q = URLDecoder.decode(q, "UTF-8");
} catch (UnsupportedEncodingException ignore) {
}
log.debug("qr decoded: " + q);
return removeIgnoreCase(q, stopWords);
}
public static String boldHighlight(String text, String q) {
return highlight(text, q, false);
}
public static String colorHighlight(String text, String q) {
return highlight(text, q, true);
}
private static String replaceWord(String text, String keyword, int colorNumber, boolean useColor) {
String color = "";
keyword = safeCharWithSpace(keyword);
if (StringUtils.isNotEmpty(keyword) && !StringUtils.isWhitespace(keyword)) {
if (useColor) color = " style='background-color: " + colors[colorNumber] + "'";
return text.replaceAll("(?i)(" + keyword + ")(?!([^<]+)?>>)", "<b" + color + ">$1</b>");
} else
return text;
}
public static String highlight(String text, String q, boolean useColor) {
String qr = prepQuery(q);
String rtn = null;
int i = 0;
if (qr.startsWith("\"")) {
String keywords = StringUtils.remove(qr, "\"");
rtn = replaceWord(text, keywords, 0, useColor);
} else {
String[] keywords = qr.split("\\s");
for (String keyword : keywords) {
rtn = replaceWord(text, keyword, i, useColor);
if (useColor) {
if (i < 11) i++;
else i = 0;
}
}
}
return rtn;
}
用于删除 prepQuery 中的停用词
方法参考我的另一篇文章:Removing strings from another string in javaremoveIgnoreCase()
()
boldHighlight method takes text string and highlights in it q keywords via <b></b>
tags
colorHighlight method takes text string and highlights int q keywords via <b style='background-color: #color'></b>
with 12 alternating colors
String text = "The use of hello as a telephone greeting has been credited to Thomas
Edison; according to one source, he expressed his surprise with a
misheard Hullo. Alexander Graham Bell initially used Ahoy (as used on
ships) as a telephone greeting"
String keywords = "HELLO Surprise"
boldHighlight(text, keywords); // will produce:
The use of
<b>hello</b>
as a telephone greeting has been credited to Thomas Edison; according to one source, he expressed his<b>surprise</b>
with a misheard Hullo. Alexander Graham Bell initially used Ahoy (as used on ships) as a telephone greeting`
colorHighlight(text, keywords); // will produce:
The use of
<b style='background-color:#ffff66'>hello</b>
as a telephone greeting has been credited to Thomas Edison;>according to one source, he expressed his<b style='background-color:#a0ffff'>surprise</b>
with a misheard Hullo. Alexander Graham Bell initially used Ahoy (as used on ships) as a telephone greeting
The question:
is there something I could use like third party library that would do similar job as bellow methods? Or if you look at the code, is there something that can be improved, to make the performance better and/or make it more elegant?`
private static final String[] colors = new String[]{"ffff66", "a0ffff", "99ff99", "ff9999", "ff66ff", "880000", "00aa00", "886800", "004699", "990099", "ffff66", "a0ffff"};
public static String safeCharWithSpace(String input) {
input = input.trim();
return Normalizer.normalize(input.toLowerCase(), Normalizer.Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", "")
.replaceAll("[^\\p{Alnum}]+", " ");
}
private static String prepQuery(String q) {
try {
log.debug("qr encoded: " + q);
q = URLDecoder.decode(q, "UTF-8");
} catch (UnsupportedEncodingException ignore) {
}
log.debug("qr decoded: " + q);
return removeIgnoreCase(q, stopWords);
}
public static String boldHighlight(String text, String q) {
return highlight(text, q, false);
}
public static String colorHighlight(String text, String q) {
return highlight(text, q, true);
}
private static String replaceWord(String text, String keyword, int colorNumber, boolean useColor) {
String color = "";
keyword = safeCharWithSpace(keyword);
if (StringUtils.isNotEmpty(keyword) && !StringUtils.isWhitespace(keyword)) {
if (useColor) color = " style='background-color: " + colors[colorNumber] + "'";
return text.replaceAll("(?i)(" + keyword + ")(?!([^<]+)?>>)", "<b" + color + ">$1</b>");
} else
return text;
}
public static String highlight(String text, String q, boolean useColor) {
String qr = prepQuery(q);
String rtn = null;
int i = 0;
if (qr.startsWith("\"")) {
String keywords = StringUtils.remove(qr, "\"");
rtn = replaceWord(text, keywords, 0, useColor);
} else {
String[] keywords = qr.split("\\s");
for (String keyword : keywords) {
rtn = replaceWord(text, keyword, i, useColor);
if (useColor) {
if (i < 11) i++;
else i = 0;
}
}
}
return rtn;
}
for removal of stop words removeIgnoreCase()
in prepQuery()
method refer to my other post: Removing strings from another string in java
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哇,好吧,你可以用几种不同的方式来解决这个问题。
您可以调用静态方法。
即
MVC 要做的事情是在处理模板之前进行此处理,但我知道您只是维护代码。
看起来它只是做了几次替换所有的操作,因此对 Java 方法的更改应该可以工作。我必须建议你看看 Freemarker 的转义工具。
Freemarker 确实有很棒的文档,并且内置插件涵盖了许多情况。
Wow, well you could go about it a few different ways.
You could call a static method.
i.e.
The MVC thing to do would be to do this processing before the template is processed, but I know your just maintaining the code.
It looks like it is just doing several replace all's, so a change to a Java method should work. I have to suggesst you look at the escaping tools Freemarker has.
Freemarker really has great documentation, and built ins cover many situations.