25
25
26
26
import info .debatty .java .stringsimilarity .interfaces .NormalizedStringSimilarity ;
27
27
import info .debatty .java .stringsimilarity .interfaces .NormalizedStringDistance ;
28
- import java .util .*;
28
+ import java .util .List ;
29
+ import java .util .ArrayList ;
30
+ import java .util .Iterator ;
29
31
30
32
import net .jcip .annotations .Immutable ;
31
33
36
38
* characters in the two strings. Matching characters are those in the longest
37
39
* common subsequence plus, recursively, matching characters in the unmatched
38
40
* region on either side of the longest common subsequence.
39
- * The Ratcliff/Obershelp distance is computed as 1 - Ratcliff/Obershelp similarity.
41
+ * The Ratcliff/Obershelp distance is computed as 1 - Ratcliff/Obershelp
42
+ * similarity.
40
43
*
41
44
* @author Ligi https://github.com/dxpux (as a patch for fuzzystring)
42
45
* Ported to java from .net by denmase
@@ -53,32 +56,29 @@ public class RatcliffObershelp implements
53
56
* @return The RatcliffObershelp similarity in the range [0, 1]
54
57
* @throws NullPointerException if s1 or s2 is null.
55
58
*/
56
- public final double similarity (String source , String target ) {
57
- if (source == null ) {
58
- throw new NullPointerException ("source must not be null" );
59
+ public final double similarity (final String s1 , final String s2 ) {
60
+ if (s1 == null ) {
61
+ throw new NullPointerException ("s1 must not be null" );
59
62
}
60
63
61
- if (target == null ) {
62
- throw new NullPointerException ("target must not be null" );
64
+ if (s2 == null ) {
65
+ throw new NullPointerException ("s2 must not be null" );
63
66
}
64
67
65
- if (source .equals (target )) {
66
- return 1 ;
68
+ if (s1 .equals (s2 )) {
69
+ return 1.0d ;
67
70
}
68
71
69
- List <String > matches ; // = new ArrayList<>();
70
- matches = getMatchQueue (source , target );
71
- int sumOfMatches = 0 ;
72
- Iterator it ;
73
- it = matches .iterator ();
72
+ List <String > matches = getMatchList (s1 , s2 );
73
+ int sumofmatches = 0 ;
74
+ Iterator it = matches .iterator ();
74
75
75
- // Display element by element using Iterator
76
76
while (it .hasNext ()) {
77
77
String element = it .next ().toString ();
78
- //System.out.println(element);
79
- sumOfMatches += element .length ();
78
+ sumofmatches += element .length ();
80
79
}
81
- return 2.0d * sumOfMatches / (source .length () + target .length ());
80
+
81
+ return 2.0d * sumofmatches / (s1 .length () + s2 .length ());
82
82
}
83
83
84
84
/**
@@ -90,41 +90,44 @@ public final double similarity(String source, String target) {
90
90
* @throws NullPointerException if s1 or s2 is null.
91
91
*/
92
92
public final double distance (final String s1 , final String s2 ) {
93
- return 1.0 - similarity (s1 , s2 );
93
+ return 1.0d - similarity (s1 , s2 );
94
94
}
95
95
96
- private static List <String > getMatchQueue ( String source , String target ) {
96
+ private static List <String > getMatchList ( final String s1 , final String s2 ) {
97
97
List <String > list = new ArrayList <>();
98
- String match = frontMaxMatch (source , target );
98
+ String match = frontMaxMatch (s1 , s2 );
99
+
99
100
if (match .length () > 0 ) {
100
- String frontSource = source .substring (0 , source .indexOf (match ));
101
- String frontTarget = target .substring (0 , target .indexOf (match ));
102
- List <String > frontQueue = getMatchQueue ( frontSource , frontTarget );
101
+ String frontsource = s1 .substring (0 , s1 .indexOf (match ));
102
+ String fronttarget = s2 .substring (0 , s2 .indexOf (match ));
103
+ List <String > frontqueue = getMatchList ( frontsource , fronttarget );
103
104
104
- String endSource = source .substring (source .indexOf (match ) + match .length ());
105
- String endTarget = target .substring (target .indexOf (match ) + match .length ());
106
- List <String > endQueue = getMatchQueue ( endSource , endTarget );
105
+ String endsource = s1 .substring (s1 .indexOf (match ) + match .length ());
106
+ String endtarget = s2 .substring (s2 .indexOf (match ) + match .length ());
107
+ List <String > endqueue = getMatchList ( endsource , endtarget );
107
108
108
109
list .add (match );
109
- list .addAll (frontQueue );
110
- list .addAll (endQueue );
110
+ list .addAll (frontqueue );
111
+ list .addAll (endqueue );
111
112
}
113
+
112
114
return list ;
113
115
}
114
116
115
- private static String frontMaxMatch (String firstString , String secondString ) {
117
+ private static String frontMaxMatch (final String s1 , final String s2 ) {
116
118
int longest = 0 ;
117
- String longestSubstring = "" ;
119
+ String longestsubstring = "" ;
118
120
119
- for (int i = 0 ; i < firstString .length (); ++i ) {
120
- for (int j = i + 1 ; j <= firstString .length (); ++j ) {
121
- String substring = firstString .substring (i , j );
122
- if (secondString .contains (substring ) && substring .length () > longest ) {
121
+ for (int i = 0 ; i < s1 .length (); ++i ) {
122
+ for (int j = i + 1 ; j <= s1 .length (); ++j ) {
123
+ String substring = s1 .substring (i , j );
124
+ if (s2 .contains (substring ) && substring .length () > longest ) {
123
125
longest = substring .length ();
124
- longestSubstring = substring ;
126
+ longestsubstring = substring ;
125
127
}
126
128
}
127
129
}
128
- return longestSubstring ;
130
+
131
+ return longestsubstring ;
129
132
}
130
- }
133
+ }
0 commit comments