lcs文本相似度

常见的问题是检测并显示两个文本的差异(尤其是几百行或几千行)。 使用纯java.lang.String类方法可能是一种解决方案,但是对于此类操作最重要的问题是,“性能”将不能令人满意。 我们需要一种有效的解决方案,其可能具有以下观点:

文字差异工具示例

该问题包含两个部分:

  • 检测两个文本的差异:为了检测差异,此解决方案中使用了一种有效的LCS(最长公共子序列)动态算法 。 该解决方案具有O(text1WordCount * text2WordCount)复杂度,并在下面被编码为“ longestCommonSubsequence”方法。
  • 可视化差异:为了进行可视化,使用了基于HTML标签的方法,该方法将text2的新单词标记为绿色,将text1的旧单词标记为红色。 此解决方案具有O(changedWordsCount *(text1WordCount + text2WordCount))复杂度,并在下面被编码为“ markTextDifferences”方法。

注意1:为简单起见,“ normalizeText”方法用于删除\ n,\ t和多个空格字符。 注意2:此类创建为Vaadin组件。 但是,“ longestCommonSubsequence”是纯通用的,“ markTextDifferences”方法是基于HTML的可视组件的通用,因此它们也可以用于不同的框架。

import java.util.ArrayList;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Label;
import com.vaadin.ui.Layout;
import com.vaadin.ui.VerticalLayout;/**
* Text comparison component which marks differences of two texts with colors.
*
* @author cb
*/
public class TextCompareComponent extends CustomComponent {private Layout mainLayout = new VerticalLayout();private ArrayList<String> longestCommonSubsequenceList;private static final String INSERT_COLOR = "#99FFCC";private static final String DELETE_COLOR = "#CB6D6D";public TextCompareComponent(String text1, String text2) {text1 = normalizeText(text1);text2 = normalizeText(text2);this.longestCommonSubsequenceList = longestCommonSubsequence(text1, text2);String result = markTextDifferences(text1, text2, longestCommonSubsequenceList, INSERT_COLOR, DELETE_COLOR);Label label = new Label(result, Label.CONTENT_XHTML);mainLayout.addComponent(label);setCompositionRoot(mainLayout);}/*** Finds a list of longest common subsequences (lcs) of given two texts.* * @param text1* @param text2* @return - longest common subsequence list*/private ArrayList<String> longestCommonSubsequence(String text1,String text2){String[] text1Words = text1.split(" ");String[] text2Words = text2.split(" ");int text1WordCount = text1Words.length;int text2WordCount = text2Words.length;int[][] solutionMatrix = new int[text1WordCount + 1][text2WordCount + 1];for (int i = text1WordCount - 1; i >=0; i--) {for (int j = text2WordCount - 1; j >= 0; j--) {if (text1Words[i].equals(text2Words[j])){solutionMatrix[i][j] = solutionMatrix[i + 1][j + 1] + 1;}else {solutionMatrix[i][j] = Math.max(solutionMatrix[i + 1][j],solutionMatrix[i][j + 1]);}}}int i = 0, j = 0;ArrayList<String> lcsResultList =new ArrayList<String>();while (i < text1WordCount && j < text2WordCount) {if (text1Words[i].equals(text2Words[j])) {lcsResultList.add(text2Words[j]);i++;j++;} else if (solutionMatrix[i + 1][j] >= solutionMatrix[i][j + 1]) {i++;}else {j++;}}return lcsResultList;}/*** Normalizes given string by deleting \n, \t and extra spaces.* * @param text - initial string* @return - normalized string*/private String normalizeText(String text) {text = text.trim();text = text.replace("\n", " ");text = text.replace("\t", " ");while (text.contains("  ")) {text = text.replace("  ", " ");}return text;}/*** Returns colored inserted/deleted text representation of given two texts.* Uses longestCommonSubsequenceList to determine colored sections.** @param text1* @param text2* @param lcsList* @param insertColor* @param deleteColor* @return - colored result text*/private String markTextDifferences(String text1, String text2,ArrayList<String> lcsList, String insertColor, String deleteColor) {StringBuffer stringBuffer = new StringBuffer();if (text1 != null && lcsList != null) {String[] text1Words = text1.split(" ");String[] text2Words = text2.split(" ");int i = 0, j = 0, word1LastIndex = 0, word2LastIndex = 0;for (int k = 0; k < lcsList.size(); k++) {for (i = word1LastIndex, j = word2LastIndex;i < text1Words.length && j < text2Words.length;) {if (text1Words[i].equals(lcsList.get(k)) &&text2Words[j].equals(lcsList.get(k))) {stringBuffer.append("<SPAN>" + lcsList.get(k) + " </SPAN>");word1LastIndex = i + 1;word2LastIndex = j + 1;i = text1Words.length;j = text2Words.length;}else if (!text1Words[i].equals(lcsList.get(k))) {for (; i < text1Words.length &&!text1Words[i].equals(lcsList.get(k)); i++) {stringBuffer.append("<SPAN style='BACKGROUND-COLOR:" +deleteColor + "'>" + text1Words[i] + " </SPAN>");}} else if (!text2Words[j].equals(lcsList.get(k))) {for (; j < text2Words.length &&!text2Words[j].equals(lcsList.get(k)), j++) {stringBuffer.append("<SPAN style='BACKGROUND-COLOR:" +insertColor + "'>" + text2Words[j] + " </SPAN>");}}}}for (; word1LastIndex < text1Words.length; word1LastIndex++) {stringBuffer.append("<SPAN style='BACKGROUND-COLOR:" +deleteColor + "'>" + text1Words[word1LastIndex] + " </SPAN>");}for (; word2LastIndex < text2Words.length; word2LastIndex++) {stringBuffer.append("<SPAN style='BACKGROUND-COLOR:" +insertColor + "'>" + text2Words[word2LastIndex] + " </SPAN>");}}return stringBuffer.toString();}
}

参考:我们的JCG合作伙伴 Cagdas Basaraner在CodeBuild博客上使用LCS方法实现了通用文本比较工具 。

翻译自: https://www.javacodegeeks.com/2012/05/generic-text-comparison-tool-with-lcs.html

lcs文本相似度

lcs文本相似度_具有LCS方法的通用文本比较工具相关推荐

  1. 具有LCS方法的通用文本比较工具

    常见的问题是检测并显示两个文本(尤其是几百行或几千行)的差异. 使用纯java.lang.String类方法可能是一种解决方案,但是对于此类操作最重要的问题是,"性能"将不能令人满 ...

  2. nlp文本相似度_用几行代码在Python中搜索相似文本:一个NLP项目

    nlp文本相似度 自然语言处理 (Natural Language Processing) 什么是自然语言处理? (What is Natural Language Processing?) Natu ...

  3. python jieba 文本相似度_文本相似度分析(基于jieba和gensim)

    ##基础概念 本文在进行文本相似度分析过程分为以下几个部分进行, 文本分词 语料库制作 算法训练 结果预测 分析过程主要用两个包来实现jieba,gensim jieba:主要实现分词过程 gensi ...

  4. python 文本相似度_【机器学习】使用gensim 的 doc2vec 实现文本相似度检测

    环境 Python3, gensim,jieba,numpy ,pandas 原理:文章转成向量,然后在计算两个向量的余弦值. Gensim gensim是一个python的自然语言处理库,能够将文档 ...

  5. 文本相似度检测的流程以及文本相似度的数学度量方法

    ​1.流程: ①对于两个文本,通过特征提取的模型或手动实现,找出两个文本的关键字: ②从每个文本中各取出若干个关键词,把这些关键词合并成成一个集合,计算每个文本中各个词对于这个集合中的关键词的词频: ...

  6. 【NLP】文本相似度的BERT度量方法

    作者 | James Briggs 编译 | VK 来源 | Towards Data Science 这篇文章讨论的是关于BERT的序列相似性. NLP的很大一部分依赖于高维空间中的相似性.通常,一 ...

  7. Python比较文本相似度的7种方法(详细)

    1词袋模型 from gensim import corpora from gensim import models from gensim import similarities #from cor ...

  8. java 圈复杂度_降低java方法的圈复杂度

    我有以下方法: private void setClientAdditionalInfo(Map map, Client client, User user) { Map additionalInfo ...

  9. 文本相似度:A Survey of Text Similarity Approaches

    文章地址:https://research.ijcaonline.org/volume68/number13/pxc3887118.pdf 文章标题:A Survey of Text Similari ...

最新文章

  1. eclipsevue代码怎么运行_[Java教程]使用eclipse初步学习vue.js操作
  2. 编程语言python入门要电脑什么配置能带动-对于几乎是零基础的人,直接学 Python 编程合适吗?...
  3. S3C6410的Bootloader的两个阶段BL1和BL2编译相关学习
  4. 使用ffmpeg循环推流(循环读取视频文件)推送RTMP服务器的方法
  5. [SUCTF2018]babyre [ACTF新生赛2020]fungame
  6. 回归模型评估_评估回归模型的方法
  7. ASP .NET MVC 之Entity Framework入门教程及源码
  8. oracle 对应的JDBC驱动 版本
  9. 7、C语言 —— 字符串常用处理函数
  10. 如何验证登录oracle,Oracle登录验证方式详解
  11. 可编辑杂志模板|简单的得到一个完整的杂志预先设计版式
  12. [渝粤教育] 西南科技大学 机电传动控制 在线考试复习资料
  13. 转载:ie6,ie7兼容性总结
  14. oracle中同义词总结,ORACLE同义词总结
  15. MATLAB2017安装VLFeat
  16. 如何解决Mac电脑没声音了的问题
  17. 返回结果集Result类
  18. 移动应用程序设计基础——数据库实践——简单日记本
  19. 广州的11个辖区_广州市下辖11个区,其中GDP总值超过3000亿元的分别有哪些?
  20. 图像校正:霍夫直线校正

热门文章

  1. 上机不会做?在讲台上做做试试!
  2. React中路由组件与一般组件
  3. JavaScript(笔记)
  4. 什么是无监督学习(监督学习,半监督学习,无监督聚类)?
  5. 基于openfire源码开发插件
  6. selenium并行_如何在不同的浏览器中设置Selenium网格以并行执行
  7. jmeter负载测试测试_使用Apache JMeter负载测试Web应用程序
  8. spring框架介绍_Spring框架介绍
  9. jdbc如何插入clob_让我们回顾一下如何通过JDBC插入Clob或Blob
  10. primefaces_使用WildFly 8.2.0.Final,Primefaces 5.1和MySQL 5的JDBC领域和基于表单的身份验证...