File tree Expand file tree Collapse file tree 17 files changed +164
-139
lines changed
main/java/info/debatty/java
test/java/info/debatty/java/stringsimilarity Expand file tree Collapse file tree 17 files changed +164
-139
lines changed Original file line number Diff line number Diff line change 2727import info .debatty .java .stringsimilarity .interfaces .NormalizedStringDistance ;
2828import java .util .Map ;
2929
30- import info .debatty .java .utils .NullEmptyUtil ;
3130import net .jcip .annotations .Immutable ;
3231
3332/**
@@ -71,10 +70,16 @@ public Cosine() {
7170 * @return
7271 */
7372 public final double similarity (final String s1 , final String s2 ) {
74- Double nullEmptySimilarity = NullEmptyUtil .normalizedSimilarity (s1 , s2 );
73+ if (s1 == null ) {
74+ throw new NullPointerException ("s1 must not be null" );
75+ }
76+
77+ if (s2 == null ) {
78+ throw new NullPointerException ("s2 must not be null" );
79+ }
7580
76- if (nullEmptySimilarity != null ) {
77- return nullEmptySimilarity ;
81+ if (s1 . equals ( s2 ) ) {
82+ return 1 ;
7883 }
7984
8085 if (s1 .length () < getK () || s2 .length () < getK ()) {
Original file line number Diff line number Diff line change 2626import info .debatty .java .stringsimilarity .interfaces .MetricStringDistance ;
2727import java .util .HashMap ;
2828
29- import info .debatty .java .utils .NullEmptyUtil ;
3029import net .jcip .annotations .Immutable ;
3130
3231/**
@@ -57,10 +56,16 @@ public class Damerau implements MetricStringDistance {
5756 */
5857 public final double distance (final String s1 , final String s2 ) {
5958
60- Double nullEmptyDistance = NullEmptyUtil .lengthDistance (s1 , s2 );
59+ if (s1 == null ) {
60+ throw new NullPointerException ("s1 must not be null" );
61+ }
62+
63+ if (s2 == null ) {
64+ throw new NullPointerException ("s2 must not be null" );
65+ }
6166
62- if (nullEmptyDistance != null ) {
63- return nullEmptyDistance ;
67+ if (s1 . equals ( s2 ) ) {
68+ return 0 ;
6469 }
6570
6671 // INFinite distance is the max possible distance
Original file line number Diff line number Diff line change 3131import java .util .Map ;
3232import java .util .Set ;
3333
34- import info .debatty .java .utils .NullEmptyUtil ;
3534import net .jcip .annotations .Immutable ;
3635
3736/**
@@ -76,10 +75,16 @@ public Jaccard() {
7675 * @return
7776 */
7877 public final double similarity (final String s1 , final String s2 ) {
79- Double nullEmptySimilarity = NullEmptyUtil .normalizedSimilarity (s1 , s2 );
78+ if (s1 == null ) {
79+ throw new NullPointerException ("s1 must not be null" );
80+ }
81+
82+ if (s2 == null ) {
83+ throw new NullPointerException ("s2 must not be null" );
84+ }
8085
81- if (nullEmptySimilarity != null ) {
82- return nullEmptySimilarity ;
86+ if (s1 . equals ( s2 ) ) {
87+ return 1 ;
8388 }
8489
8590 Map <String , Integer > profile1 = getProfile (s1 );
Original file line number Diff line number Diff line change 44import info .debatty .java .stringsimilarity .interfaces .NormalizedStringDistance ;
55import java .util .Arrays ;
66
7- import info .debatty .java .utils .NullEmptyUtil ;
87import net .jcip .annotations .Immutable ;
98
109/**
@@ -62,10 +61,16 @@ public final double getThreshold() {
6261 * @return
6362 */
6463 public final double similarity (final String s1 , final String s2 ) {
65- Double nullEmptySimilarity = NullEmptyUtil .normalizedSimilarity (s1 , s2 );
64+ if (s1 == null ) {
65+ throw new NullPointerException ("s1 must not be null" );
66+ }
67+
68+ if (s2 == null ) {
69+ throw new NullPointerException ("s2 must not be null" );
70+ }
6671
67- if (nullEmptySimilarity != null ) {
68- return nullEmptySimilarity ;
72+ if (s1 . equals ( s2 ) ) {
73+ return 1 ;
6974 }
7075
7176 int [] mtp = matches (s1 , s2 );
Original file line number Diff line number Diff line change 11package info .debatty .java .stringsimilarity ;
22
33import info .debatty .java .stringsimilarity .interfaces .MetricStringDistance ;
4- import info .debatty .java .utils .NullEmptyUtil ;
54import net .jcip .annotations .Immutable ;
65
76/**
@@ -39,10 +38,12 @@ public class Levenshtein implements MetricStringDistance {
3938 * @return
4039 */
4140 public final double distance (final String s1 , final String s2 ) {
42- Double nullEmptyDistance = NullEmptyUtil .lengthDistance (s1 , s2 );
41+ if (s1 == null ) {
42+ throw new NullPointerException ("s1 must not be null" );
43+ }
4344
44- if (nullEmptyDistance ! = null ) {
45- return nullEmptyDistance ;
45+ if (s2 = = null ) {
46+ throw new NullPointerException ( "s2 must not be null" ) ;
4647 }
4748
4849 if (s1 .equals (s2 )) {
Original file line number Diff line number Diff line change 11package info .debatty .java .stringsimilarity ;
22
33import info .debatty .java .stringsimilarity .interfaces .StringDistance ;
4- import info .debatty .java .utils .NullEmptyUtil ;
54import net .jcip .annotations .Immutable ;
65
76/**
@@ -37,10 +36,16 @@ public class LongestCommonSubsequence implements StringDistance {
3736 * |s2| - 2 * |LCS(s1, s2)|
3837 */
3938 public final double distance (final String s1 , final String s2 ) {
40- Double nullEmptyDistance = NullEmptyUtil .lengthDistance (s1 , s2 );
39+ if (s1 == null ) {
40+ throw new NullPointerException ("s1 must not be null" );
41+ }
42+
43+ if (s2 == null ) {
44+ throw new NullPointerException ("s2 must not be null" );
45+ }
4146
42- if (nullEmptyDistance != null ) {
43- return nullEmptyDistance ;
47+ if (s1 . equals ( s2 ) ) {
48+ return 0 ;
4449 }
4550
4651 return s1 .length () + s2 .length () - 2 * length (s1 , s2 );
Original file line number Diff line number Diff line change 2626
2727import info .debatty .java .stringsimilarity .interfaces .MetricStringDistance ;
2828import info .debatty .java .stringsimilarity .interfaces .NormalizedStringDistance ;
29- import info .debatty .java .utils .NullEmptyUtil ;
3029import net .jcip .annotations .Immutable ;
3130
3231/**
@@ -50,10 +49,16 @@ public class MetricLCS
5049 * @return
5150 */
5251 public final double distance (final String s1 , final String s2 ) {
53- Double nullEmptyDistance = NullEmptyUtil .normalizedDistance (s1 , s2 );
52+ if (s1 == null ) {
53+ throw new NullPointerException ("s1 must not be null" );
54+ }
55+
56+ if (s2 == null ) {
57+ throw new NullPointerException ("s2 must not be null" );
58+ }
5459
55- if (nullEmptyDistance != null ) {
56- return nullEmptyDistance ;
60+ if (s1 . equals ( s2 ) ) {
61+ return 0 ;
5762 }
5863
5964 int mLen = Math .max (s1 .length (), s2 .length ());
Original file line number Diff line number Diff line change 11package info .debatty .java .stringsimilarity ;
22
33import info .debatty .java .stringsimilarity .interfaces .NormalizedStringDistance ;
4- import info .debatty .java .utils .NullEmptyUtil ;
54import net .jcip .annotations .Immutable ;
65
76/**
@@ -43,18 +42,24 @@ public NGram() {
4342 * @return
4443 */
4544 public final double distance (final String s0 , final String s1 ) {
46- Double nullEmptyDistance = NullEmptyUtil .normalizedDistance (s0 , s1 );
45+ if (s0 == null ) {
46+ throw new NullPointerException ("s0 must not be null" );
47+ }
4748
48- if (nullEmptyDistance != null ) {
49- return nullEmptyDistance ;
49+ if (s1 == null ) {
50+ throw new NullPointerException ("s1 must not be null" );
51+ }
52+
53+ if (s0 .equals (s1 )) {
54+ return 0 ;
5055 }
5156
5257 final char special = '\n' ;
5358 final int sl = s0 .length ();
5459 final int tl = s1 .length ();
5560
56- if (s0 . equals ( s1 ) ) {
57- return 0 ;
61+ if (sl == 0 || tl == 0 ) {
62+ return 1 ;
5863 }
5964
6065 int cost = 0 ;
Original file line number Diff line number Diff line change 2525
2626import info .debatty .java .stringsimilarity .interfaces .NormalizedStringSimilarity ;
2727import info .debatty .java .stringsimilarity .interfaces .NormalizedStringDistance ;
28- import info .debatty .java .utils .NullEmptyUtil ;
2928import net .jcip .annotations .Immutable ;
3029
3130/**
@@ -50,10 +49,16 @@ public class NormalizedLevenshtein implements
5049 */
5150 public final double distance (final String s1 , final String s2 ) {
5251
53- Double nullEmptyDistance = NullEmptyUtil .normalizedDistance (s1 , s2 );
52+ if (s1 == null ) {
53+ throw new NullPointerException ("s1 must not be null" );
54+ }
55+
56+ if (s2 == null ) {
57+ throw new NullPointerException ("s2 must not be null" );
58+ }
5459
55- if (nullEmptyDistance != null ) {
56- return nullEmptyDistance ;
60+ if (s1 . equals ( s2 ) ) {
61+ return 0 ;
5762 }
5863
5964 int mLen = Math .max (s1 .length (), s2 .length ());
Original file line number Diff line number Diff line change 2424package info .debatty .java .stringsimilarity ;
2525
2626import info .debatty .java .stringsimilarity .interfaces .StringDistance ;
27- import info .debatty .java .utils .NullEmptyUtil ;
2827import net .jcip .annotations .Immutable ;
2928
3029/**
@@ -52,10 +51,16 @@ public final class OptimalStringAlignment implements StringDistance {
5251 * @return the OSA distance
5352 */
5453 public final double distance (final String s1 , final String s2 ) {
55- Double nullEmptyDistance = NullEmptyUtil .lengthDistance (s1 , s2 );
54+ if (s1 == null ) {
55+ throw new NullPointerException ("s1 must not be null" );
56+ }
57+
58+ if (s2 == null ) {
59+ throw new NullPointerException ("s2 must not be null" );
60+ }
5661
57- if (nullEmptyDistance != null ) {
58- return nullEmptyDistance ;
62+ if (s1 . equals ( s2 ) ) {
63+ return 0 ;
5964 }
6065
6166 int n = s1 .length (), m = s2 .length ();
You can’t perform that action at this time.
0 commit comments