/
StringMetricBuilderExample.java
273 lines (229 loc) · 8 KB
/
StringMetricBuilderExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* #%L
* Simmetrics Examples
* %%
* Copyright (C) 2014 - 2016 Simmetrics Authors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package org.simmetrics.example;
import static com.google.common.base.Predicates.in;
import static org.simmetrics.builders.StringMetricBuilder.with;
import java.util.Set;
import org.simmetrics.StringMetric;
import org.simmetrics.metrics.CosineSimilarity;
import org.simmetrics.metrics.Levenshtein;
import org.simmetrics.simplifiers.Simplifiers;
import org.simmetrics.tokenizers.Tokenizers;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.Multiset;
import com.google.common.collect.Sets;
/**
* The string metric builder can be used to compose similarity metrics for
* strings.
*/
public final class StringMetricBuilderExample {
/**
* Simply comparing strings through a metric may not be very effective. By
* adding simplifiers, tokenizers and filters and transform the
* effectiveness of a metric can be improved.
*
* The exact combination is generally domain specific. The
* StringMetricBuilder supports these domain specific customizations. Some
* example usages are shown below
*/
public static float example00() {
String a = "Chilpéric II son of Childeric II";
String b = "chilperic ii son of childeric ii";
StringMetric metric = new Levenshtein();
return metric.compare(a, b); // 0.7812
}
/**
* Simplification
*
* Simplification increases the effectiveness of a metric by removing noise
* and reducing the dimensionality of the problem. The process maps a a
* complex string to a simpler format. This allows string from different
* sources to be compared in the same form.
*
* The Simplifiers utility class contains a collection of common, useful
* simplifiers. For a custom simplifier you can implement the Simplifier
* interface.
*/
public static float example01() {
String a = "Chilpéric II son of Childeric II";
String b = "Chilperic II son of Childeric II";
StringMetric metric =
with(new Levenshtein())
.simplify(Simplifiers.removeDiacritics())
.build();
return metric.compare(a, b); // 1.0000
}
/**
* Simplifiers can also be chained.
*/
public static float example02() {
String a = "Chilpéric II son of Childeric II";
String b = "chilperic ii son of childeric ii";
StringMetric metric =
with(new Levenshtein())
.simplify(Simplifiers.removeDiacritics())
.simplify(Simplifiers.toLowerCase())
.build();
return metric.compare(a, b); // 1.0000
}
/**
* Tokenization
*
* A metric can be used to measure the similarity between strings. However
* not all metrics can operate on strings directly. Some operate on lists,
* sets or multisets. To compare strings with a metric that works on a
* collection a tokenizer is required. Tokenization cuts up a string into
* parts.
*
* Example:
*
* `chilperic ii son of childeric ii`
*
* By splitting on whitespace is tokenized into:
*
* `[chilperic, ii, son, of, childeric, ii]`
*
* The choice of the tokenizer can influence the effectiveness of a metric.
* For example when comparing individual words a q-gram tokenizer will be
* more effective while a whitespace tokenizer will be more effective when
* comparing documents.
*
* The Tokenizers utility class contains a collection of common, useful
* tokenizers. For a custom tokenizer you can implement the Tokenizer
* interface. Though it is recommended that you extend the
* AbstractTokenizer.
*/
public static float example03() {
String a = "A quirky thing it is. This is a sentence.";
String b = "This sentence is similar; a quirky thing it is.";
StringMetric metric =
with(new CosineSimilarity<String>())
.tokenize(Tokenizers.whitespace())
.build();
return metric.compare(a, b); // 0.7777
}
/**
* Tokenizers can also be chained.
*
* `chilperic ii son of childeric ii`
*
* By splitting on whitespace is tokenized into:
*
* `[chilperic, ii, son, of, childeric, ii]`
*
* After using a q-gram with a q of 2:
*
* `[ch,hi,il,il,lp,pe,er,ri,ic, ii, so,on, of, ch,hi,il,ld,de,er,ri,ic,
* ii]`
*
*/
public static float example04() {
String a = "A quirky thing it is. This is a sentence.";
String b = "This sentence is similar; a quirky thing it is.";
StringMetric metric =
with(new CosineSimilarity<String>())
.tokenize(Tokenizers.whitespace())
.tokenize(Tokenizers.qGram(3))
.build();
return metric.compare(a, b); // 0.8292
}
/**
* Tokens can be filtered to avoid comparing strings on common but otherwise
* low information words. Tokens can be filtered after any tokenization step
* and filters can be applied repeatedly.
*
* A filter can be implemented by implementing a the {@link Predicate}
* interface. By chaining predicates more complicated filters can be build.
* */
public static float example05() {
Set<String> commonWords = Sets.newHashSet("it", "is");
Set<String> otherCommonWords = Sets.newHashSet("a");
String a = "A quirky thing it is. This is a sentence.";
String b = "This sentence is similar; a quirky thing it is.";
StringMetric metric =
with(new CosineSimilarity<String>())
.simplify(Simplifiers.toLowerCase())
.simplify(Simplifiers.removeNonWord())
.tokenize(Tokenizers.whitespace())
.filter(Predicates.not(in(commonWords)))
.filter(Predicates.not(in(otherCommonWords)))
.tokenize(Tokenizers.qGram(3))
.build();
return metric.compare(a, b); // 0.6902
}
/**
* Tokens can be transformed to a simpler form. This may be used to reduce
* the possible token space. Tokens can be transformed after any
* tokenization step and the transformation can be applied repeatedly.
*
* A transformation can be implemented by implementing a the Function
* interface.
*/
public static float example06() {
Function<String, String> reverse = new Function<String, String>() {
@Override
public String apply(String input) {
return new StringBuilder(input).reverse().toString();
}
};
String a = "A quirky thing it is. This is a sentence.";
String b = "This sentence is similar; a quirky thing it is.";
StringMetric metric =
with(new CosineSimilarity<String>())
.simplify(Simplifiers.toLowerCase())
.simplify(Simplifiers.removeNonWord())
.tokenize(Tokenizers.whitespace())
.transform(reverse)
.tokenize(Tokenizers.qGram(3))
.build();
return metric.compare(a, b); // 0.6902
}
/**
* Tokenization and simplification can be expensive operations. To avoid
* executing expensive operations repeatedly, intermediate results can be
* cached. Note that Caching itself also has a non-trivial cost. Base your
* decision on metrics!
*/
public static float example07() {
String a = "A quirky thing it is. This is a sentence.";
String b = "This sentence is similar; a quirky thing it is.";
Cache<String,String> stringCache =
CacheBuilder.newBuilder()
.maximumSize(2)
.build();
Cache<String,Multiset<String>> tokenCache =
CacheBuilder.newBuilder()
.maximumSize(2)
.build();
StringMetric metric =
with(new CosineSimilarity<String>())
.simplify(Simplifiers.toLowerCase())
.simplify(Simplifiers.removeNonWord())
.cacheStrings(stringCache)
.tokenize(Tokenizers.qGram(3))
.cacheTokens(tokenCache)
.build();
return metric.compare(a, b); // 0.6902
}
}