これは正規表現の問題ではありません。
言語は指定しませんが、Java を使用している場合getLevenshteinDistance
は StringUtils のメソッドがあります。javadoc から:
2 つの弦の間のレーベンシュタイン距離を求めます。
これは、1 つの文字列を別の文字列に変更するために必要な変更の数です。各変更は 1 文字の変更 (削除、挿入、または置換) です。
使用法:
int distance = StringUtils.getLevenshteinDistance(
"some filename contains few words.txt",
"some filename not contains few words.txt"
);
distance
あるパーセンテージで一致させるには、入力文字列の長さが異なる可能性があるため"cat"
、どの文字列が「マスター」であるかを決定する必要があり"cataract"
ます5
。「90% 一致」とは何かを定義することも少し難しいです。私たちのcat
例を見てください。文字列「cat」の 100% は「cataract」にありますが、まったく同じ文字列ではありません。ユースケースに応じて、これらのルールを決定する必要があります。
アップデート
「違い」が単語ベースである必要がある場合、単語境界で文字列を分割しMap
、結果の単語から各単語のカウントまでを構築するのは比較的簡単です。各文字列に対して生成されたマップを比較すると、大まかな「類似性」の測定値が得られます。例えば:
public HashMap<String, Integer> countWords(String str) {
HashMap<String, Integer> counts = new HashMap<String, Integer>();
for(String s : str.split("\\s+")) {
if(!s.isEmpty()) {
if(counts.containsKey(s)) {
counts.put(s, counts.get(s) + 1);
} else {
counts.put(s, 1);
}
}
}
return counts;
}
// ...
String s1 = "some filename contains few words.txt";
String s2 = "some filename not contains few words.txt";
HashMap<String, Integer> s1Counts = countWords(s1);
HashMap<String, Integer> s2Counts = countWords(s2);
// assume s1 is "master" string, count the total number of words
int s1Total = 0, s2Total = 0;
for(Integer i : s1Counts.values()) {
s1Total += i;
}
// iterate over words in s1, find the number of matching words in s2
for(Map.Entry<String, Integer> entry : s1Counts.entrySet()) {
if(s2Counts.containsKey(entry.getKey())) {
if(s2Counts.get(entry.getKey()) >= entry.getValue()) {
s2Total += entry.getValue();
} else {
s2Total += s2Counts.get(entry.getKey());
}
}
}
// result
System.out.println(s2Total + " out of " + s1Total + " words match.");