JAVA:(格式塔)difflib.sequencematcher的模式匹配或替换
我正在寻找一种方法来比较两个字符串。 但不是简单的equals()。 我需要一些指标来说明那些字符串匹配的可能性有多大。 因此,例如(值是一个猜测,没有调用): “汽车”和“汽车”翻新1.0版 “汽车狗”和“汽车”跑0.5 “坐着”和“坐着”跑0.45 等等 基本上是用于Java的:difflib.sqeuencematcher()的替代品JAVA:(格式塔)difflib.sequencematcher的模式匹配或替换,java,pattern-matching,Java,Pattern Matching,我正在寻找一种方法来比较两个字符串。 但不是简单的equals()。 我需要一些指标来说明那些字符串匹配的可能性有多大。 因此,例如(值是一个猜测,没有调用): “汽车”和“汽车”翻新1.0版 “汽车狗”和“汽车”跑0.5 “坐着”和“坐着”跑0.45 等等 基本上是用于Java的:difflib.sqeuencematcher()的替代品 我已经查看了@java diff-utils,但是我没有找到一种方法来实现这一点……您应该看看Apache Commons LangStringUtils。
我已经查看了@java diff-utils,但是我没有找到一种方法来实现这一点……您应该看看Apache Commons Lang
StringUtils
。尤其是您应该看看ApacheCommonsLangStringUtils
。特别是要获得与您所要求的接近的值,可以使用以下方法:先获取字符串的大小,然后获取StringUtils。删除您的匹配尝试,然后将原始大小减去剩余的大小,再除以原始大小
public double matchString(final String stringToMatch, final String matchPattern) {
final int testSize = stringToMatch.length();
if (testSize == 0 && matchPattern.length() == 0) {
return 1.0;
} else if (testSize == 0) {
return 0.0;
}
final String remainderString = StringUtils.remove(stringToMatch, matchPattern);
final int remainderSize = remainderString.length();
final double result = (double) (testSize - remainderSize) / (double) testSize;
return result;
}
@Test
public void testMatchString() {
final double emptyResult = matchString("", "");
final double delta = 0.01;
Assert.assertEquals(1.0, emptyResult, delta);
final double emptyCarResult = matchString("", "Car");
Assert.assertEquals(0.0, emptyCarResult, delta);
final double dogCatResult = matchString("CarDog", "Car");
Assert.assertEquals(0.5, dogCatResult, delta);
final double carResult = matchString("Car", "Car");
Assert.assertEquals(1.0, carResult, delta);
final double carsCarResult = matchString("Cars", "Car");
Assert.assertEquals(0.75, carsCarResult, delta);
final double sittingSitResult = matchString("Sitting", "Sit");
Assert.assertEquals(0.4286, sittingSitResult, delta);
// no match since the 'S' in Sitting is uppercased and is not in sit.
// this can be fixed up lowercasing both the stringToMatch and matchPattern
// in matchString
final double sittingSit2Result = matchString("Sitting", "sit");
Assert.assertEquals(0.0, sittingSit2Result, delta);
// note the Sit match pattern matches two instences in 'Sit Sitting'
final double sittingSit3Result = matchString("Sitter Sitting", "Sit");
Assert.assertEquals(0.4286, sittingSit3Result, delta);
}
要获得与您所要求的接近的近似值,可以使用字符串的大小,然后使用StringUtils。删除您的匹配尝试,然后将原始大小减去剩余的大小除以原始大小
public double matchString(final String stringToMatch, final String matchPattern) {
final int testSize = stringToMatch.length();
if (testSize == 0 && matchPattern.length() == 0) {
return 1.0;
} else if (testSize == 0) {
return 0.0;
}
final String remainderString = StringUtils.remove(stringToMatch, matchPattern);
final int remainderSize = remainderString.length();
final double result = (double) (testSize - remainderSize) / (double) testSize;
return result;
}
@Test
public void testMatchString() {
final double emptyResult = matchString("", "");
final double delta = 0.01;
Assert.assertEquals(1.0, emptyResult, delta);
final double emptyCarResult = matchString("", "Car");
Assert.assertEquals(0.0, emptyCarResult, delta);
final double dogCatResult = matchString("CarDog", "Car");
Assert.assertEquals(0.5, dogCatResult, delta);
final double carResult = matchString("Car", "Car");
Assert.assertEquals(1.0, carResult, delta);
final double carsCarResult = matchString("Cars", "Car");
Assert.assertEquals(0.75, carsCarResult, delta);
final double sittingSitResult = matchString("Sitting", "Sit");
Assert.assertEquals(0.4286, sittingSitResult, delta);
// no match since the 'S' in Sitting is uppercased and is not in sit.
// this can be fixed up lowercasing both the stringToMatch and matchPattern
// in matchString
final double sittingSit2Result = matchString("Sitting", "sit");
Assert.assertEquals(0.0, sittingSit2Result, delta);
// note the Sit match pattern matches two instences in 'Sit Sitting'
final double sittingSit3Result = matchString("Sitter Sitting", "Sit");
Assert.assertEquals(0.4286, sittingSit3Result, delta);
}
逐个匹配字符,然后除以最大字符的长度(忽略空格)或任何你的重量计算。@SotiriosDelimanolis:Python的
difflib
比这聪明得多。“Car dog”不应该是.5,因为不到一半的字符串匹配(“dog”是不匹配的)。@larsman我不怀疑这一点。如果算法可用,OP可以获得灵感。逐个匹配字符,然后除以最大字符的长度(忽略空格)或任何你的权重计算。@SotiriosDelimanolis:Python的difflib
比这聪明得多。“Car dog”不应该是.5,因为少于一半的字符串匹配(“dog”是无与伦比的。)@larsman我不怀疑这一点。如果算法可用,OP可以获得灵感。通过阅读文档,那里使用的算法不同于Levenshtein(=最小编辑距离):“这不会产生最小的编辑序列,但会产生对人们来说“看起来正确”的匹配。嗯,这与difflib的算法不同。但我想这是目前java中可用的最好的可能性。我还没有找到对底层算法的足够详细的描述,不知道我是否可以自己实现它!!从阅读文档来看,这里使用的算法不同于Levenshtein(=最小编辑距离):“这不会产生最小的编辑序列,但会产生人们“看得对”的匹配结果。”嗯,这和difflib使用的算法不同。但我想这是目前java中可用的最好的可能性。我还没有找到对底层算法的足够详细的描述,不知道我是否可以自己实现它!!