2525
2626import info .debatty .java .stringsimilarity .interfaces .NormalizedStringSimilarity ;
2727import 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 ;
2931
3032import net .jcip .annotations .Immutable ;
3133
3638 * characters in the two strings. Matching characters are those in the longest
3739 * common subsequence plus, recursively, matching characters in the unmatched
3840 * 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.
4043 *
4144 * @author Ligi https://github.com/dxpux (as a patch for fuzzystring)
4245 * Ported to java from .net by denmase
@@ -53,32 +56,29 @@ public class RatcliffObershelp implements
5356 * @return The RatcliffObershelp similarity in the range [0, 1]
5457 * @throws NullPointerException if s1 or s2 is null.
5558 */
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" );
5962 }
6063
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" );
6366 }
6467
65- if (source .equals (target )) {
66- return 1 ;
68+ if (s1 .equals (s2 )) {
69+ return 1.0d ;
6770 }
6871
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 ();
7475
75- // Display element by element using Iterator
7676 while (it .hasNext ()) {
7777 String element = it .next ().toString ();
78- //System.out.println(element);
79- sumOfMatches += element .length ();
78+ sumofmatches += element .length ();
8079 }
81- return 2.0d * sumOfMatches / (source .length () + target .length ());
80+
81+ return 2.0d * sumofmatches / (s1 .length () + s2 .length ());
8282 }
8383
8484 /**
@@ -90,41 +90,44 @@ public final double similarity(String source, String target) {
9090 * @throws NullPointerException if s1 or s2 is null.
9191 */
9292 public final double distance (final String s1 , final String s2 ) {
93- return 1.0 - similarity (s1 , s2 );
93+ return 1.0d - similarity (s1 , s2 );
9494 }
9595
96- private static List <String > getMatchQueue ( String source , String target ) {
96+ private static List <String > getMatchList ( final String s1 , final String s2 ) {
9797 List <String > list = new ArrayList <>();
98- String match = frontMaxMatch (source , target );
98+ String match = frontMaxMatch (s1 , s2 );
99+
99100 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 );
103104
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 );
107108
108109 list .add (match );
109- list .addAll (frontQueue );
110- list .addAll (endQueue );
110+ list .addAll (frontqueue );
111+ list .addAll (endqueue );
111112 }
113+
112114 return list ;
113115 }
114116
115- private static String frontMaxMatch (String firstString , String secondString ) {
117+ private static String frontMaxMatch (final String s1 , final String s2 ) {
116118 int longest = 0 ;
117- String longestSubstring = "" ;
119+ String longestsubstring = "" ;
118120
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 ) {
123125 longest = substring .length ();
124- longestSubstring = substring ;
126+ longestsubstring = substring ;
125127 }
126128 }
127129 }
128- return longestSubstring ;
130+
131+ return longestsubstring ;
129132 }
130- }
133+ }
0 commit comments