Skip to content

Commit

Permalink
Move Keyer and Distance registration to controller.js (#6487)
Browse files Browse the repository at this point in the history
Follow-up to #1906.
  • Loading branch information
wetneb committed Mar 29, 2024
1 parent 4e4baad commit 519948a
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 15 deletions.
Expand Up @@ -45,13 +45,6 @@ public class KeyerFactory {
// We cannot derive this from the hashmap as the order matters
static final private List<String> _keyerNames = new LinkedList<>();

static {
put("fingerprint", new FingerprintKeyer());
put("ngram-fingerprint", new NGramFingerprintKeyer());
put("metaphone3", new Metaphone3Keyer());
put("cologne-phonetic", new ColognePhoneticKeyer());
}

/**
* Returns the keyer registered under a given name, or null if it does not exist.
*/
Expand Down
Expand Up @@ -33,9 +33,6 @@
import java.util.List;
import java.util.Map;

import edu.mit.simile.vicino.distances.LevenshteinDistance;
import edu.mit.simile.vicino.distances.PPMDistance;

/**
* Registry of distances for kNN clustering.
*
Expand All @@ -47,11 +44,6 @@ public class DistanceFactory {
// We cannot derive this from the hashmap as the order matters
private static List<String> _distanceNames = new LinkedList<>();

static {
put("levenshtein", new VicinoDistance(new LevenshteinDistance()));
put("ppm", new VicinoDistance(new PPMDistance()));
}

/**
* Returns the distance registered under this name, or null if it does not exist.
*/
Expand Down
Expand Up @@ -34,6 +34,7 @@

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.google.refine.RefineTest;
Expand Down Expand Up @@ -62,6 +63,12 @@ public class BinningClustererTests extends RefineTest {
+ " [{\"v\":\"c\",\"c\":1},{\"v\":\"ĉ\",\"c\":1}]"
+ "]";

@BeforeTest
public void registerKeyers() {
KeyerFactory.put("fingerprint", new FingerprintKeyer());
KeyerFactory.put("ngram-fingerprint", new NGramFingerprintKeyer());
}

@Test
public void testSerializeBinningClustererConfig() throws JsonParseException, JsonMappingException, IOException {
BinningClustererConfig config = ParsingUtilities.mapper.readValue(configJson, BinningClustererConfig.class);
Expand Down
Expand Up @@ -34,6 +34,8 @@

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import edu.mit.simile.vicino.distances.PPMDistance;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.google.refine.RefineTest;
Expand All @@ -55,6 +57,11 @@ public class kNNClustererTests extends RefineTest {
+ " [{\"v\":\"ab\",\"c\":1},{\"v\":\"abc\",\"c\":1}]"
+ "]";

@BeforeTest
public void registerDistance() {
DistanceFactory.put("ppm", new VicinoDistance(new PPMDistance()));
}

@Test
public void serializekNNClustererConfig() throws JsonParseException, JsonMappingException, IOException {
kNNClustererConfig config = ParsingUtilities.mapper.readValue(configJson, kNNClustererConfig.class);
Expand Down
Expand Up @@ -41,9 +41,14 @@
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.mit.simile.vicino.distances.LevenshteinDistance;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.google.refine.clustering.binning.KeyerFactory;
import com.google.refine.clustering.binning.Metaphone3Keyer;
import com.google.refine.clustering.knn.DistanceFactory;
import com.google.refine.clustering.knn.VicinoDistance;
import com.google.refine.commands.Command;
import com.google.refine.util.JSONUtilities;
import com.google.refine.util.ParsingUtilities;
Expand All @@ -66,6 +71,9 @@ public void setUp() {
} catch (IOException e) {
e.printStackTrace();
}

DistanceFactory.put("levenshtein", new VicinoDistance(new LevenshteinDistance()));
KeyerFactory.put("metaphone3", new Metaphone3Keyer());
}

@Test
Expand Down
Expand Up @@ -30,13 +30,23 @@
import static org.testng.Assert.assertEquals;

import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.google.refine.RefineTest;
import com.google.refine.clustering.binning.ColognePhoneticKeyer;
import com.google.refine.clustering.binning.KeyerFactory;
import com.google.refine.clustering.binning.Metaphone3Keyer;
import com.google.refine.expr.EvalError;

public class PhoneticTests extends RefineTest {

@BeforeTest
public void registerKeyers() {
KeyerFactory.put("metaphone3", new Metaphone3Keyer());
KeyerFactory.put("cologne-phonetic", new ColognePhoneticKeyer());
}

@Test
public void testtoPhoneticInvalidParams() {
Assert.assertTrue(invoke("phonetic") instanceof EvalError); // if no arguments are provided
Expand Down
17 changes: 17 additions & 0 deletions main/webapp/modules/core/MOD-INF/controller.js
Expand Up @@ -319,6 +319,21 @@ function registerImporting() {
);
}

function registerDistances() {
var DF = Packages.com.google.refine.clustering.knn.DistanceFactory;
var VicinoDistance = Packages.com.google.refine.clustering.knn.VicinoDistance;
DF.put("levenshtein", new VicinoDistance(new Packages.edu.mit.simile.vicino.distances.LevenshteinDistance()));
DF.put("ppm", new VicinoDistance(new Packages.edu.mit.simile.vicino.distances.PPMDistance()));
}

function registerKeyers() {
var KF = Packages.com.google.refine.clustering.binning.KeyerFactory;
KF.put("fingerprint", new Packages.com.google.refine.clustering.binning.FingerprintKeyer());
KF.put("ngram-fingerprint", new Packages.com.google.refine.clustering.binning.NGramFingerprintKeyer());
KF.put("metaphone3", new Packages.com.google.refine.clustering.binning.Metaphone3Keyer());
KF.put("cologne-phonetic", new Packages.com.google.refine.clustering.binning.ColognePhoneticKeyer());
}

/*
* This optional function is invoked from the module's init() Java function.
*/
Expand All @@ -328,6 +343,8 @@ function init() {
registerCommands();
registerOperations();
registerImporting();
registerDistances();
registerKeyers();

var commonModules = [
"3rdparty/jquery.js",
Expand Down

0 comments on commit 519948a

Please sign in to comment.