adding Metaphone3 algorithm

Many thanks to Lawrence Philips for donating the code to us under the BSD license.


git-svn-id: http://google-refine.googlecode.com/svn/trunk@2029 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Stefano Mazzocchi 2011-03-01 00:17:48 +00:00
parent 87e7f9a7a4
commit 610de0d33a
6 changed files with 7575 additions and 65 deletions

View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<classpath> <classpath>
<classpathentry kind="src" path="main/src"/> <classpathentry kind="src" path="main/src"/>
<classpathentry kind="src" path="broker/core/src"/>
<classpathentry kind="src" path="extensions/jython/src"/> <classpathentry kind="src" path="extensions/jython/src"/>
<classpathentry kind="src" path="server/src"/> <classpathentry kind="src" path="server/src"/>
<classpathentry kind="src" path="extensions/freebase/src"/> <classpathentry kind="src" path="extensions/freebase/src"/>
@ -55,5 +54,5 @@
<classpathentry kind="lib" path="extensions/gdata/module/MOD-INF/lib/gdata-spreadsheet-3.0.jar"/> <classpathentry kind="lib" path="extensions/gdata/module/MOD-INF/lib/gdata-spreadsheet-3.0.jar"/>
<classpathentry kind="lib" path="extensions/gdata/module/MOD-INF/lib/google-collect-1.0-rc1.jar"/> <classpathentry kind="lib" path="extensions/gdata/module/MOD-INF/lib/google-collect-1.0-rc1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/jsoup-1.4.1.jar"/> <classpathentry kind="lib" path="main/webapp/WEB-INF/lib/jsoup-1.4.1.jar"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="build"/>
</classpath> </classpath>

View File

@ -40,9 +40,9 @@ import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties; import java.util.Properties;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.Map.Entry;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -73,6 +73,7 @@ public class BinningClusterer extends Clusterer {
_keyers.put("ngram-fingerprint", new NGramFingerprintKeyer()); _keyers.put("ngram-fingerprint", new NGramFingerprintKeyer());
_keyers.put("metaphone", new MetaphoneKeyer()); _keyers.put("metaphone", new MetaphoneKeyer());
_keyers.put("double-metaphone", new DoubleMetaphoneKeyer()); _keyers.put("double-metaphone", new DoubleMetaphoneKeyer());
_keyers.put("metaphone3", new Metaphone3Keyer());
_keyers.put("soundex", new SoundexKeyer()); _keyers.put("soundex", new SoundexKeyer());
} }

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,50 @@
/*
Copyright 2010, Google Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.refine.clustering.binning;
public class Metaphone3Keyer extends Keyer {
public Metaphone3Keyer() {
}
public String key(String s, Object... o) {
Metaphone3 _metaphone3 = new Metaphone3();
//_metaphone3.SetEncodeVowels(true);
//_metaphone3.SetEncodeExact(true);
_metaphone3.SetWord(s);
_metaphone3.Encode();
return _metaphone3.GetMetaph();
}
}

View File

@ -39,6 +39,7 @@ import org.json.JSONException;
import org.json.JSONWriter; import org.json.JSONWriter;
import com.google.refine.clustering.binning.DoubleMetaphoneKeyer; import com.google.refine.clustering.binning.DoubleMetaphoneKeyer;
import com.google.refine.clustering.binning.Metaphone3Keyer;
import com.google.refine.clustering.binning.MetaphoneKeyer; import com.google.refine.clustering.binning.MetaphoneKeyer;
import com.google.refine.clustering.binning.SoundexKeyer; import com.google.refine.clustering.binning.SoundexKeyer;
import com.google.refine.expr.EvalError; import com.google.refine.expr.EvalError;
@ -47,6 +48,7 @@ import com.google.refine.grel.Function;
public class Phonetic implements Function { public class Phonetic implements Function {
static private Metaphone3Keyer metaphone3 = new Metaphone3Keyer();
static private DoubleMetaphoneKeyer metaphone2 = new DoubleMetaphoneKeyer(); static private DoubleMetaphoneKeyer metaphone2 = new DoubleMetaphoneKeyer();
static private MetaphoneKeyer metaphone = new MetaphoneKeyer(); static private MetaphoneKeyer metaphone = new MetaphoneKeyer();
static private SoundexKeyer soundex = new SoundexKeyer(); static private SoundexKeyer soundex = new SoundexKeyer();
@ -58,11 +60,14 @@ public class Phonetic implements Function {
if (o1 != null && o2 != null && o2 instanceof String) { if (o1 != null && o2 != null && o2 instanceof String) {
String str = (o1 instanceof String) ? (String) o1 : o1.toString(); String str = (o1 instanceof String) ? (String) o1 : o1.toString();
String encoding = ((String) o2).toLowerCase(); String encoding = ((String) o2).toLowerCase();
if ("doublemetaphone".equals(encoding)) { if (encoding == null) encoding = "metaphone3";
if ("doublemetaphone".equalsIgnoreCase(encoding)) {
return metaphone2.key(str); return metaphone2.key(str);
} else if ("metaphone".equals(encoding)) { } else if ("metaphone3".equalsIgnoreCase(encoding)) {
return metaphone3.key(str);
} else if ("metaphone".equalsIgnoreCase(encoding)) {
return metaphone.key(str); return metaphone.key(str);
} else if ("soundex".equals(encoding)) { } else if ("soundex".equalsIgnoreCase(encoding)) {
return soundex.key(str); return soundex.key(str);
} else { } else {
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " doesn't know how to handle the '" + encoding + "' encoding."); return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " doesn't know how to handle the '" + encoding + "' encoding.");
@ -77,7 +82,7 @@ public class Phonetic implements Function {
writer.object(); writer.object();
writer.key("description"); writer.value("Returns the a phonetic encoding of s (optionally indicating which encoding to use')"); writer.key("description"); writer.value("Returns the a phonetic encoding of s (optionally indicating which encoding to use')");
writer.key("params"); writer.value("string s, string encoding (optional, defaults to 'DoubleMetaphone')"); writer.key("params"); writer.value("string s, string encoding (optional, defaults to 'metaphone3')");
writer.key("returns"); writer.value("string"); writer.key("returns"); writer.value("string");
writer.endObject(); writer.endObject();
} }

View File

@ -23,7 +23,7 @@
<div class="binning-controls">Keying&nbsp;Function&nbsp;<select bind="keyingFunctionSelector"> <div class="binning-controls">Keying&nbsp;Function&nbsp;<select bind="keyingFunctionSelector">
<option selected="true">fingerprint</option> <option selected="true">fingerprint</option>
<option>ngram-fingerprint</option> <option>ngram-fingerprint</option>
<option>double-metaphone</option> <option>metaphone3</option>
</select></div> </select></div>
<div class="knn-controls hidden">Distance&nbsp;Function&nbsp;<select bind="distanceFunctionSelector"> <div class="knn-controls hidden">Distance&nbsp;Function&nbsp;<select bind="distanceFunctionSelector">
<option selected="true">levenshtein</option> <option selected="true">levenshtein</option>