Java 中的搜索荧光笔

发布于 2024-10-13 03:39:45 字数 3401 浏览 8 评论 0原文

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 中的停用词 removeIgnoreCase() () 方法参考我的另一篇文章:Removing strings from another string in java

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑叹一世浮沉 2024-10-20 03:39:45

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

  1. 您可以调用静态方法

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

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

Freemarker 确实有很棒的文档,并且内置插件涵盖了许多情况。

Wow, well you could go about it a few different ways.

  1. You could call a static method.

    i.e.

    ${statics["java.lang.System"].currentTimeMillis()} 
    
  2. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文