百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程网 > 正文

java文本对比工具源码5(java 文本对比)

yuyutoo 2025-06-23 23:15 3 浏览 0 评论

/**

* Locate the best instance of 'pattern' in 'text' near 'loc' using the

* Bitap algorithm. Returns -1 if no match found.

* @param text The text to search.

* @param pattern The pattern to search for.

* @param loc The location to search around.

* @return Best match index or -1.

*/

protected int match_bitap(String text, String pattern, int loc) {

assert (Match_MaxBits == 0 || pattern.length() <= Match_MaxBits)

: "Pattern too long for this application.";

// Initialise the alphabet.

Map<Character, Integer> s = match_alphabet(pattern);

// Highest score beyond which we give up.

double score_threshold = Match_Threshold;

// Is there a nearby exact match? (speedup)

int best_loc = text.indexOf(pattern, loc);

if (best_loc != -1) {

score_threshold = Math.min(match_bitapScore(0, best_loc, loc, pattern),

score_threshold);

// What about in the other direction? (speedup)

best_loc = text.lastIndexOf(pattern, loc + pattern.length());

if (best_loc != -1) {

score_threshold = Math.min(match_bitapScore(0, best_loc, loc, pattern),

score_threshold);

}

}

// Initialise the bit arrays.

int matchmask = 1 << (pattern.length() - 1);

best_loc = -1;

int bin_min, bin_mid;

int bin_max = pattern.length() + text.length();

// Empty initialization added to appease Java compiler.

int[] last_rd = new int[0];

for (int d = 0; d < pattern.length(); d++) {

// Scan for the best match; each iteration allows for one more error.

// Run a binary search to determine how far from 'loc' we can stray at

// this error level.

bin_min = 0;

bin_mid = bin_max;

while (bin_min < bin_mid) {

if (match_bitapScore(d, loc + bin_mid, loc, pattern)

<= score_threshold) {

bin_min = bin_mid;

} else {

bin_max = bin_mid;

}

bin_mid = (bin_max - bin_min) / 2 + bin_min;

}

// Use the result from this iteration as the maximum for the next.

bin_max = bin_mid;

int start = Math.max(1, loc - bin_mid + 1);

int finish = Math.min(loc + bin_mid, text.length()) + pattern.length();

int[] rd = new int[finish + 2];

rd[finish + 1] = (1 << d) - 1;

for (int j = finish; j >= start; j--) {

int charMatch;

if (text.length() <= j - 1 || !s.containsKey(text.charAt(j - 1))) {

// Out of range.

charMatch = 0;

} else {

charMatch = s.get(text.charAt(j - 1));

}

if (d == 0) {

// First pass: exact match.

rd[j] = ((rd[j + 1] << 1) | 1) & charMatch;

} else {

// Subsequent passes: fuzzy match.

rd[j] = (((rd[j + 1] << 1) | 1) & charMatch)

| (((last_rd[j + 1] | last_rd[j]) << 1) | 1) | last_rd[j + 1];

}

if ((rd[j] & matchmask) != 0) {

double score = match_bitapScore(d, j - 1, loc, pattern);

// This match will almost certainly be better than any existing

// match. But check anyway.

if (score <= score_threshold) {

// Told you so.

score_threshold = score;

best_loc = j - 1;

if (best_loc > loc) {

// When passing loc, don't exceed our current distance from loc.

start = Math.max(1, 2 * loc - best_loc);

} else {

// Already passed loc, downhill from here on in.

break;

}

}

}

}

if (match_bitapScore(d + 1, loc, loc, pattern) > score_threshold) {

// No hope for a (better) match at greater error levels.

break;

}

last_rd = rd;

}

return best_loc;

}

/**

* Compute and return the score for a match with e errors and x location.

* @param e Number of errors in match.

* @param x Location of match.

* @param loc Expected location of match.

* @param pattern Pattern being sought.

* @return Overall score for match (0.0 = good, 1.0 = bad).

*/

private double match_bitapScore(int e, int x, int loc, String pattern) {

float accuracy = (float) e / pattern.length();

int proximity = Math.abs(loc - x);

if (Match_Distance == 0) {

// Dodge divide by zero error.

return proximity == 0 ? accuracy : 1.0;

}

return accuracy + (proximity / (float) Match_Distance);

}

/**

* Initialise the alphabet for the Bitap algorithm.

* @param pattern The text to encode.

* @return Hash of character locations.

*/

protected Map<Character, Integer> match_alphabet(String pattern) {

Map<Character, Integer> s = new HashMap<Character, Integer>();

char[] char_pattern = pattern.toCharArray();

for (char c : char_pattern) {

s.put(c, 0);

}

int i = 0;

for (char c : char_pattern) {

s.put(c, s.get(c) | (1 << (pattern.length() - i - 1)));

i++;

}

return s;

}

// PATCH FUNCTIONS

/**

* Increase the context until it is unique,

* but don't let the pattern expand beyond Match_MaxBits.

* @param patch The patch to grow.

* @param text Source text.

*/

protected void patch_addContext(Patch patch, String text) {

if (text.length() == 0) {

return;

}

String pattern = text.substring(patch.start2, patch.start2 + patch.length1);

int padding = 0;

// Look for the first and last matches of pattern in text. If two different

// matches are found, increase the pattern length.

while (text.indexOf(pattern) != text.lastIndexOf(pattern)

&& pattern.length() < Match_MaxBits - Patch_Margin - Patch_Margin) {

padding += Patch_Margin;

pattern = text.substring(Math.max(0, patch.start2 - padding),

Math.min(text.length(), patch.start2 + patch.length1 + padding));

}

// Add one chunk for good luck.

padding += Patch_Margin;

// Add the prefix.

String prefix = text.substring(Math.max(0, patch.start2 - padding),

patch.start2);

if (prefix.length() != 0) {

patch.diffs.addFirst(new Diff(Operation.EQUAL, prefix));

}

// Add the suffix.

String suffix = text.substring(patch.start2 + patch.length1,

Math.min(text.length(), patch.start2 + patch.length1 + padding));

if (suffix.length() != 0) {

patch.diffs.addLast(new Diff(Operation.EQUAL, suffix));

}

// Roll back the start points.

patch.start1 -= prefix.length();

patch.start2 -= prefix.length();

// Extend the lengths.

patch.length1 += prefix.length() + suffix.length();

patch.length2 += prefix.length() + suffix.length();

}

相关推荐

Java 代理模式详解(java代理类应用场景)

1.代理模式代理模式是一种比较好理解的设计模式。简单来说就是我们使用代理对象来代替对真实对象(realobject)的访问,这样就可以在不修改原目标对象的前提下,提供额外的功能操作,扩展目标对象...

深入解析Java工厂模式及其应用场景

Java工厂模式(FactoryPattern)是一种创建型设计模式,它提供了一种创建对象的最佳实践,这种模式提供了一种抽象工厂,通过使用工厂方法来创建对象。工厂方法将对象的创建推迟到子类中,这样就...

java之数据格式化(java中格式化快捷键)

数据格式化概述1、对属性对象的输入/输出进行格式化,从其本质上讲依然属于“类型转换”的范畴。...

Java之程序中的套路(设计模式的介绍)

前言本文主要是给大家简单地介绍一下设计模式的概念,文中会使用通俗易懂的案例,使你更好地学习本章知识点并理解原理,做到有道无术一.什么是设计模式首先我们得知道什么是设计模式。所谓的...

java文本对比工具源码5(java 文本对比)

/***Locatethebestinstanceof'pattern'in'text'near'...

Java微服务-设计模式系列全套文章-适配器模式(Adapter Pattern)

模式动机适配器模式(AdapterPattern)是一种使用频率非常高的结构型模式,如果在系统中存在不兼容的接口,可以通过引入一个适配器来使得原本因为接口不兼容而不能一起工作的两个类可以协同工作。适配...

Java 20 发布,新特性一览:Amber、Loom 和 Panama 项目

作者|MichaelRedlich译者|张卫滨...

Java语法入门004(java语法合集)

上篇是java语法入门003,继续学习Java[1]。...

Java8优雅编码实战:10个技巧让你的代码焕然一新

引言:为什么你的Java代码还不够优雅?“代码质量直接决定开发效率与系统稳定性。据Gartner统计,60%的线上故障源于低级编码错误。本文基于10万+行生产代码优化经验,提炼Java8的10大核心...

Java中常见的设计模式汇总?(java三种常用设计模式和实例)

设计模式是一套经过验证的设计方案和最佳实践,这些经验和方案主要就是用来解决软件设计过程中一些特定的问题。设计模式并不是代码本身,而是一种用来解决某种问题的抽象的解决方案,也就是说设计模式是在不同的语言...

Java字符串拼接3大隐藏陷阱!你的代码为何越优化越慢-附提速代码

导语:“某电商平台因一行字符串拼接代码,每秒多消耗1GB内存!本文通过性能压测对比+字节码反编译,揭秘看似简单的字符串操作如何拖垮你的系统。文末附性能检测工具+优化模板,点击关注领取实战方案!”...

JDK21新特性:Pattern Matching for switch

PatternMatchingforswitchJEP441:PatternMatchingforswitch...

java设计模式-行为型:观察者、责任链、备忘录、命令、状态

责任链模式(ChainofResponsibilityPattern)是行为型设计模式的一种。在责任链模式中,多个处理器都有机会处理请求,但是每个处理器都决定它是否可以处理该请求以及它是否应该将...

Java设计模式之外观模式(外观模式类图)

一、外观模式介绍1.1外观模式定义外观模式(FacadePattern),也叫门面模式,外观模式的原始定义是:为子系统中的一组接口提供统一的接口。它定义了一个更高级别的接口,使子系统更易于使用...

java文本对比工具源码1(java快速对比数据)

/**DiffMatchandPatch*Copyright2018Thediff-match-patchAuthors....

取消回复欢迎 发表评论: