Skip to content

Approximate string comparison and pattern matching in Java

aburkov edited this page Jun 30, 2015 · 6 revisions

With xpresso you can perform an approximate string comparison and pattern matching in Java using the Python's FuzzyWuzzy algorithm.

Approximate string comparison:

x.print(x.String("Hello World").similarity("Hello Wold!"))

Console: 91

The output similarity value is the same as that of the fuzz.ratio function of FuzzyWuzzy.

By the way, xpresso has all string comparison methods of FuzzyWuzzy:

import com.wantedtech.common.xpresso.strings.FuzzyWuzzy;

x.assertTrue(FuzzyWuzzy.ratio("this is a test", "this is a test!") == 97);
x.assertTrue(FuzzyWuzzy.partial_ratio("this is a test", "this is a test!") == 100);
x.assertTrue(FuzzyWuzzy.ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear") == 91);
x.assertTrue(FuzzyWuzzy.token_sort_ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear", null) == 100);
x.assertTrue(FuzzyWuzzy.token_sort_ratio("fuzzy was a bear", "fuzzy fuzzy was a bear", null) == 84);
x.assertTrue(FuzzyWuzzy.token_set_ratio("fuzzy was a bear", "fuzzy fuzzy was a bear", null) == 100);

x.assertTrue(FuzzyWuzzy.extract("new york jets", choices, null, null, 2).equals(x.list(x.tuple2("New York Jets", 100), x.tuple2("New York Giants", 79))));
					    
x.assertTrue(FuzzyWuzzy.extractOne("cowboys", choices, null, null, null).equals(x.tuple2("Dallas Cowboys", 90)));

Approximate pattern matching:

x.print(x.String("You are cooding in Java.").search("coding"));

Console: 8

Get similar strings:

list<String> lookAlikes = x.String("apple").lookAlikes(x.list("ape", "apples", "peach", "puppy"),50);

x.print(lookAlikes);

Console: [ape, apples]