Created
April 23, 2019 06:03
-
-
Save asderix/0757e6bc82cabc45cf5babecfdc42f38 to your computer and use it in GitHub Desktop.
Example how to use ESC with Java.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* author: Ronny Fuchs, info@asderix.com | |
* license: Apache license 2.0 - https://www.apache.org/licenses/ | |
*/ | |
package esc.java.example; | |
import esc.commons.FinderMatch; | |
import esc.commons.IndexPerson; | |
import esc.index.Finder; | |
import esc.index.Indexer; | |
import esc.index.IndexFactory; | |
import esc.configuration.SimilarityConfig; | |
import esc.configuration.SimilarityConfigFactory; | |
import esc.similarity.NameSimilarity; | |
import esc.utils.CollectionConv; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.util.Arrays; | |
import java.util.List; | |
public class Main { | |
public static void main(String[] args) { | |
try { | |
// --- Generall stuff ---: | |
// Config: | |
SimilarityConfig config = SimilarityConfigFactory.createDefaultConfig(); | |
// --- Similarity ---: | |
NameSimilarity sim = new NameSimilarity(config); | |
// Calculate the similarity of two persons: | |
System.out.println("Per.-Sim: " + | |
sim.getPersonNameSimilarity("Bill Gates", "William Henry Gates").similarity()); | |
// Calculate the similarity of two organisations: | |
System.out.println("Org.-Sim: " + | |
sim.getOrganisationNameSimilarity("Microsoft (Schweiz) AG", "Microspot (Switerland) Ltd.").similarity()); | |
// --- Indexer ---: | |
// Create an indexer: | |
Indexer indexer = IndexFactory.openOrCreateIndex("C:\\Tmp\\", "JavaExampleIndex1", config); | |
// A person to index. | |
IndexPerson person = new IndexPerson("id1234", | |
"exid1234", | |
"Hanspeter Müller", | |
CollectionConv.toScalaList(Arrays.asList("31.12.1980")), | |
CollectionConv.toScalaList(Arrays.asList("Switzerland", "DE")), | |
""); | |
// Index the person. | |
indexer.addPerson(person); | |
// Commit and close the indexer. | |
indexer.commit(); | |
indexer.close(); | |
// Create a Finder: | |
Finder finder = IndexFactory.openIndexForSearch("C:\\Tmp\\", "JavaExampleIndex1", config); | |
// Find a person. | |
List<FinderMatch> matches = CollectionConv.toJavaList(finder.findPerson("Hans-Peter Müller-Meyer", | |
CollectionConv.toScalaList(Arrays.asList("1981")), CollectionConv.emptyScalaStringList(), "")); | |
System.out.println("Matches:"); | |
System.out.println(matches.toString()); | |
System.out.println("Access matchPairs:"); | |
System.out.println(matches.get(0).matchResult().matchPairs().toString()); | |
java.io.BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); | |
in.readLine(); // press any key to exit the application. | |
} | |
catch (Exception ex) { | |
// Handle Exception | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment