gpt4 book ai didi

java - Java 中的搜索荧光笔

转载 作者:行者123 更新时间:2023-12-01 15:57:45 24 4
gpt4 key购买 nike

boldHighlight 方法采用文本字符串并通过 <b></b> 突出显示其中的 q 关键字标签

colorHighlight 方法采用文本字符串并通过 <b style='background-color: #color'></b> 突出显示 int q 关键字有 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); // 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

<小时/>

问题:

有什么我可以使用的东西,比如第三方库,可以完成与以下方法类似的工作?或者,如果您查看代码,是否有一些可以改进的地方,以使性能更好和/或使其更优雅?`

<小时/>
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;
}

用于删除停用词removeIgnoreCase()prepQuery()方法引用我的另一篇帖子:Removing strings from another string in java

最佳答案

哇,你可以用几种不同的方式来解决这个问题。

  1. 你可以call a static method .

    ${statics["java.lang.System"].currentTimeMillis()} 
  2. MVC 要做的事情是在处理模板之前进行此处理,但我知道您只是维护代码。

看起来它只是做了几次替换所有的操作,因此对 Java 方法的更改应该可以工作。我必须建议你看看escaping tools Freemarker 有。

Freemarker确实有很棒的documentation ,并且内置函数涵盖了许多情况。

关于java - Java 中的搜索荧光笔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4763014/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com