比较两段文本的相同或不同,并用html标记

1、核心jar包

        <dependency>
            <groupId>org.bitbucket.cowwoc</groupId>
            <artifactId>diff-match-patch</artifactId>
            <version>1.2</version>
        </dependency>

2、代码实现

package com.yiscn.smartfilesys.backserver.utils.textsign;

import org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 道长
 * @Date:2021/7/6 11:05
 * 文本标记工具类
 */

public class TextSignUtil {

    private static TextSignUtil textSignUtil = null;

    private static final String OPEN = "<span style=\"color:red;\">";

    private static final String CLOSE = "</span>";

    /**
     * 标签的open
     */
    private String open;

    /**
     * 标签的close
     */
    private String close;

    private TextSignUtil(){
        this.open = OPEN;
        this.close = CLOSE;
    }

    private TextSignUtil(String open, String close){
        this.open = open;
        this.close = close;
    }

    public String getOpen() {
        return open;
    }

    public void setOpen(String open) {
        this.open = open;
    }

    public String getClose() {
        return close;
    }

    public void setClose(String close) {
        this.close = close;
    }

    public static TextSignUtil getInstance(){
        if (textSignUtil == null || !textSignUtil.getOpen().equals(OPEN)){
            synchronized (TextSignUtil.class){
                textSignUtil = new TextSignUtil();
            }
        }
        return textSignUtil;
    }

    public static TextSignUtil getInstance(String open, String close){
        if (textSignUtil == null || !textSignUtil.getOpen().equals(open)){
            synchronized (TextSignUtil.class){
                textSignUtil = new TextSignUtil(open,close);
            }
        }
        return textSignUtil;
    }

    /**
     *
     * @param text 文本内容
     * @param comparisonText 被比较的文本内容
     * @param bool true 标记相同的内容   false  标记不同的内容
     * @return
     */
    public SignVO signTextDifferent(String text, String comparisonText, boolean bool){
        DiffMatchPatch dmp = new DiffMatchPatch();
        /*
                这个是他们内部的枚举类
         public static enum Operation {
                    DELETE,//DELETE 是text  相对于comparesonText来说 text有  而comparisonText没有
                    INSERT,// text 相对于comparsesonText来说 comparisonText 中新添加的数据
                    EQUAL;//是两段相同的文本

                    private Operation() {
                    }
                }*
         */
        LinkedList<DiffMatchPatch.Diff> diff = dmp.diffMain(text, comparisonText, false);
        //原文本的集合
        List<DiffMatchPatch.Diff> textDiffList = diff.stream()
                .filter(d -> d.operation.equals(DiffMatchPatch.Operation.EQUAL) || d.operation.equals(DiffMatchPatch.Operation.DELETE))
                .collect(Collectors.toList());

        List<DiffMatchPatch.Diff> comparisonDiffList = diff.stream()
                .filter(d -> d.operation.equals(DiffMatchPatch.Operation.EQUAL) || d.operation.equals(DiffMatchPatch.Operation.INSERT))
                .collect(Collectors.toList());


        return singTextByListDiff(textDiffList,comparisonDiffList,bool);
    }

    private SignVO singTextByListDiff(List<DiffMatchPatch.Diff> textDiffList, List<DiffMatchPatch.Diff> comparisonDiffList, boolean bool){
        if (bool){
            /*标记相同的文本*/
            String text = signSpanForEqual(textDiffList);
            String comparisonText = signSpanForEqual(comparisonDiffList);
            return new SignVO(text,comparisonText);
        }else {
            /*标记不同的文本*/
            String text = signSpanDifferent(textDiffList);
            String comparisonText = signSpanDifferent(comparisonDiffList);
            return new SignVO(text,comparisonText);
        }
    }


    /**
     * 将文本中的差异部分标记成颜色
     * @param diffs
     * @return 标记后的文本
     */
    private String signSpanDifferent(List<DiffMatchPatch.Diff> diffs){
        StringBuilder builder = new StringBuilder();
        for (DiffMatchPatch.Diff diff : diffs) {
            //差异的文本需要被标记上标签
            if (!diff.operation.equals(DiffMatchPatch.Operation.EQUAL)){
                builder.append(getOpen());
                builder.append(diff.text);
                builder.append(getClose());
            }else {
                builder.append(diff.text);
            }
        }
        return builder.toString();
    }


    /**
     * 给相同的内容打上标签
     * @param diffs
     * @return
     */
    private String signSpanForEqual(List<DiffMatchPatch.Diff> diffs){
        StringBuilder builder = new StringBuilder();
        for (DiffMatchPatch.Diff diff : diffs) {
            if (diff.operation.equals(DiffMatchPatch.Operation.EQUAL)){
                builder.append(getOpen());
                builder.append(diff.text);
                builder.append(getClose());
            }else {
                builder.append(diff.text);
            }
        }
        return builder.toString();
    }




}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值