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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
package com.wcohen.ss.lookup;
import java.io.*;
import java.util.*;
import com.wcohen.ss.*;
import com.wcohen.ss.api.*;
import com.wcohen.ss.tokens.*;
/**
* Looks up nearly-matching strings in a dictionary, using a string distance.
*
* A typical use:
*<code><pre>
* SoftDictionary softDict = new SoftDictionary(new SimpleTokenizer(true,true));
* String alias[] = new String[]{"william cohen", "wwcohen", "einat minkov", "eminkov", .... };
* for (int i=0; i<alias.length; i++) {
* softDict.put( alias[i], null );
* }
* String query = "w. cohen";
* StringWrapper w = (StringWrapper)softDict.lookup( query );
* String closestMatchToQuery = w.unwrap();
*</pre></code>
*/
public class SoftDictionary
{
private static final boolean DEBUG=false;
//
// private data
//
// used to learn the distance measure to use in evaluation
private StringDistanceLearner distanceLearner;
// distance measure to use in evaluation
private StringDistance distance;
// tokenizes stored strings
private Tokenizer tokenizer;
// maps tokens to strings containing these tokens
private Map index;
// maps stored strings to their associated value
private Map map;
// maps stored strings to their associated 'id'
private Map idMap;
// count things in map
private int totalEntries;
//
// config options
//
// max fraction of total entries an index entry can have to be useful
private double maxFraction = 1.0;
//
// constructors
//
public SoftDictionary()
{
this(new JaroWinklerTFIDF(), NGramTokenizer.DEFAULT_TOKENIZER);
}
public SoftDictionary(StringDistanceLearner distanceLearner)
{
this(distanceLearner, NGramTokenizer.DEFAULT_TOKENIZER);
}
public SoftDictionary(Tokenizer tokenizer)
{
this(new JaroWinklerTFIDF(), tokenizer);
}
public SoftDictionary(StringDistanceLearner distanceLearner,Tokenizer tokenizer)
{
this.distanceLearner = distanceLearner;
this.distance = null;
this.tokenizer = tokenizer;
this.index = new HashMap();
this.map = new HashMap();
this.idMap = new HashMap();
this.totalEntries = 0;
}
/** Return the number of entries in the dictionary. */
public int size()
{
return totalEntries;
}
/** Prepare a string for quicker lookup.
*/
public StringWrapper prepare(String s)
{
return new MyWrapper(s);
}
/** Insert all lines in a file as items mapping to themselves.
*/
public void load(File file) throws IOException,FileNotFoundException
{
load(file,false);
}
/** Insert all lines in a file as items mapping to themselves. If
* 'ids' is true, then make the line number of an item its id.
*
*<p>This is mostly for testing the id feature.
*/
public void load(File file,boolean ids) throws IOException,FileNotFoundException
{
LineNumberReader in = new LineNumberReader(new FileReader(file));
String line;
while ((line = in.readLine())!=null) {
if (ids) put(Integer.toString(in.getLineNumber()), line, line);
else put(line,line);
}
in.close();
}
/** Load a file of identifiers, each of which has multiple
* aliases. Each line is a list of tab-separated strings, the
* first of which is the identifier, the remainder of which
* are aliases.
*/
public void loadAliases(File file) throws IOException,FileNotFoundException
{
LineNumberReader in = new LineNumberReader(new FileReader(file));
String line;
while ((line = in.readLine())!=null) {
String[] parts = line.split("\\t");
for (int j=1; j<parts.length; j++) {
put( parts[j], parts[j], parts[0] );
}
}
in.close();
}
/** Insert a string into the dictionary.
*
* <p>Id is a special tag used to handle 'leave one out'
* lookups. If you do a lookup on a string with a non-null
* id, you get the closest matches that do not have the same
* id.
*/
public void put(String id,String string,Object value)
{
if (DEBUG && id!=null) System.out.println(id+":"+string+" => "+value);
put(id, new MyWrapper(string), value);
}
/** Insert a string into the dictionary.
*/
public void put(String string,Object value)
{
put((String)null, new MyWrapper(string), value);
}
/** Insert a prepared string into the dictionary.
*
* <p>Id is a special tag used to handle 'leave one out'
* lookups. If you do a lookup on a string with a non-null
* id, you get the closest matches that do not have the same
* id.
*/
public void put(String id, StringWrapper toInsert,Object value)
{
MyWrapper wrapper = asMyWrapper(toInsert);
Token[] tokens = wrapper.getTokens();
for (int i=0; i<tokens.length; i++) {
ArrayList stringsWithToken = (ArrayList) index.get(tokens[i]);
if (stringsWithToken==null) index.put( tokens[i], (stringsWithToken=new ArrayList()) );
stringsWithToken.add( wrapper );
}
map.put( wrapper, value );
if (id!=null) idMap.put( wrapper, id );
distance = null; // mark distance as "out of date"
totalEntries++;
}
// caches result of last 'get'
private HashSet closeMatches;
private MyWrapper closestMatch;
private double distanceToClosestMatch;
private StringWrapper lastLookup;
/** Lookup a string in the dictionary, cache result in closeMatches.
*
* <p>If id==null, consider any match. If id is non-null, consider
* only matches to strings that don't have the same id, or that have
* a null id.
*/
private void doLookup(String id,StringWrapper toFind)
{
// retrain if necessary
if (distance==null) {
distance = new MyTeacher().train( distanceLearner );
}
// used cached values if it's ok
if (lastLookup==toFind) return;
closeMatches = new HashSet();
closestMatch = null;
distanceToClosestMatch = -Double.MAX_VALUE;
// lookup best match to wrapper
MyWrapper wrapper = asMyWrapper(toFind);
Token[] tokens = wrapper.getTokens();
for (int i=0; i<tokens.length; i++) {
ArrayList stringsWithToken = (ArrayList) index.get(tokens[i]);
if (stringsWithToken!=null && ((double)stringsWithToken.size()/totalEntries) < maxFraction) {
for (Iterator j=stringsWithToken.iterator(); j.hasNext(); ) {
MyWrapper wj = (MyWrapper)j.next();
String wjId = (String)idMap.get(wj);
//if (DEBUG) System.out.println("id:"+id+" wjId:"+wjId);
if (!closeMatches.contains(wj) && (wjId==null || !wjId.equals(id))) {
double score = distance.score( wrapper.getDistanceWrapper(), wj.getDistanceWrapper() );
if (DEBUG) System.out.println("score for "+wj+": "+score);
//if (DEBUG) System.out.println(distance.explainScore(wrapper.getDistanceWrapper(),wj.getDistanceWrapper()));
closeMatches.add( wj );
if (score>=distanceToClosestMatch) {
//if (DEBUG) System.out.println("closest so far");
distanceToClosestMatch = score;
closestMatch = wj;
}
}
}
}
}
lastLookup = toFind;
}
/** Lookup a string in the dictionary.
*
* <p>If id is non-null, then consider only strings with different ids (or null ids).
*/
public Object lookup(String id,String toFind)
{
return lookup(id,new MyWrapper(toFind));
}
/** Lookup a prepared string in the dictionary.
*
* <p>If id is non-null, then consider only strings with different ids (or null ids).
*/
public Object lookup(String id,StringWrapper toFind)
{
doLookup(id,toFind);
return closestMatch;
}
/** Return the distance to the best match.
*
* <p>If id is non-null, then consider only strings with different ids (or null ids).
*/
public double lookupDistance(String id,String toFind)
{
return lookupDistance(id,new MyWrapper(toFind));
}
/** Return the distance to the best match.
*
* <p>If id is non-null, then consider only strings with different ids (or null ids).
*/
public double lookupDistance(String id,StringWrapper toFind)
{
doLookup(id,toFind);
return distanceToClosestMatch;
}
/** Lookup a string in the dictionary.
*/
public Object lookup(String toFind)
{
return lookup(null,new MyWrapper(toFind));
}
/** Lookup a prepared string in the dictionary.
*/
public Object lookup(StringWrapper toFind)
{
doLookup(null,toFind);
return closestMatch;
}
/** Return the distance to the best match.
*/
public double lookupDistance(String toFind)
{
return lookupDistance(null,new MyWrapper(toFind));
}
/** Return the distance to the best match.
*/
public double lookupDistance(StringWrapper toFind)
{
doLookup(null,toFind);
return distanceToClosestMatch;
}
/** Return a teacher that can 'train' a distance metric
* from the information in the dictionary. Since there are
* no known distances, this means unsupervised training,
* e.g. accumulating TFIDF weights, etc.
*/
public StringDistanceTeacher getTeacher() { return new MyTeacher(); }
//
// a tokenized version of the string, plus one prepared for the distance metric
//
private class MyWrapper implements StringWrapper {
private StringWrapper w;
private Token[] tokens;
public MyWrapper(String s)
{
this.w = prepare(s);
this.tokens = tokenizer.tokenize(s);
}
public String unwrap() { return w.unwrap(); }
public char charAt(int i) { return w.charAt(i); }
public int length() { return w.length(); }
public Token[] getTokens() { return tokens; }
public StringWrapper getDistanceWrapper() { return w; }
public int hashCode() { return unwrap().hashCode(); }
public boolean equals(Object o) {
if (!(o instanceof MyWrapper)) return false;
return ((MyWrapper)o).unwrap().equals( this.unwrap() );
}
private StringWrapper prepare(String s) {
StringWrapperIterator i = distanceLearner.prepare(
new BasicStringWrapperIterator( Collections.singleton(new BasicStringWrapper(s)).iterator()) );
return i.nextStringWrapper();
}
public String toString() { return "[SoftDictionaryWrapper '"+unwrap()+"']"; }
}
// lazily convert to a MyWrapper
private MyWrapper asMyWrapper(StringWrapper w)
{
if (w instanceof MyWrapper) return (MyWrapper)w;
else return new MyWrapper(w.unwrap());
}
// simple teacher that only supports unsupervised training
private class MyTeacher extends StringDistanceTeacher {
protected StringWrapperIterator stringWrapperIterator() {
return new BasicStringWrapperIterator(map.keySet().iterator());
}
protected DistanceInstanceIterator distanceInstancePool() {
return new BasicDistanceInstanceIterator( Collections.EMPTY_SET.iterator() );
}
protected DistanceInstanceIterator distanceExamplePool() {
return new BasicDistanceInstanceIterator( Collections.EMPTY_SET.iterator() );
}
protected DistanceInstance labelInstance(DistanceInstance distanceInstance) {
return null;
}
protected boolean hasAnswers() {
return false;
}
}
/** Simple main for testing.
*/
static public void main(String[] argv) throws IOException,FileNotFoundException
{
SoftDictionary m = new SoftDictionary();
System.out.println("loading...");
m.loadAliases(new File(argv[0]));
System.out.println("loaded...");
for (int i=1; i<argv.length; i++) {
System.out.println("lookup: "+argv[i]);
String[] f = argv[i].split(":");
if (f.length==1) {
System.out.println(argv[i] +" => "+m.lookup(argv[i])+" at "+m.lookupDistance(argv[i]));
} else {
System.out.println(f[1] +" => "+m.lookup(f[0],f[1])+" at "+m.lookupDistance(f[0],f[1]));
}
}
}
}
|