Comments (4)
Yes. X would be your query term, {Y_i} would be your dictionary, and K would be your max edit distance (the maximum Levenshtein distance that spelling candidates may be from the query term). If you want all terms with distances from your query term that are strictly less than K, then just decrement K by 1 when you query the library. What is your use case? If you want the standard, Levenshtein distance then specify Algorithm.STANDARD when you build your transducer. If you're looking to correct typos then you probably want Algorithm.TRANSPOSITION. If you're dealing with optical character recognition then you probably want Algorithm.MERGE_AND_SPLIT. Take a look at this wiki and let me know if you have any more questions:
https://github.com/universal-automata/liblevenshtein/wiki/usage.java
from liblevenshtein-java.
Thanks for your reply. I have a list of words w_i and a set of documents d_j. For each d_j, I want to find all w_i in d_j, allowing for spelling mistakes in w_i.
from liblevenshtein-java.
Something like this would work (by all means, make it fancier!):
package com.github.dylon.liblevenshtein.demo;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Scanner;
import java.util.SortedSet;
import java.util.TreeSet;
import com.github.dylon.liblevenshtein.levenshtein.Algorithm;
import com.github.dylon.liblevenshtein.levenshtein.Candidate;
import com.github.dylon.liblevenshtein.levenshtein.ITransducer;
import com.github.dylon.liblevenshtein.levenshtein.factory.TransducerBuilder;
public class Main {
/**
* For every document, finds all strings within the specified number of errors
* from some query term.
* Example Usage:
* java com.github.dylon.liblevenshtein.demo.Main foo 2 $HOME/Documents/*.txt
*/
public static void main(final String... args) throws IOException {
int argsIdx = 0;
final String queryTerm = args[argsIdx ++];
final int maxDistance = Integer.parseInt(args[argsIdx ++]);
for (int docIdx = argsIdx; docIdx < args.length; docIdx += 1) {
final Path docPath = Paths.get(args[docIdx]);
final Collection<String> dictionary = buildDictionary(docPath);
final ITransducer<Candidate> transducer = new TransducerBuilder()
.algorithm(Algorithm.TRANSPOSITION)
.defaultMaxDistance(maxDistance)
.dictionary(dictionary, true)
.build();
for (final Candidate candidate : transducer.transduce(queryTerm)) {
System.out.printf("%s: d(%s, %s) = %d%n",
docPath, queryTerm, candidate.term(), candidate.distance());
}
}
}
private static Collection<String> buildDictionary(final Path docPath) throws IOException {
try (final Scanner terms = new Scanner(docPath)) {
final SortedSet<String> dictionary = new TreeSet<>((a,b) -> a.compareTo(b));
while (terms.hasNext()) {
final String term = terms.next();
dictionary.add(term);
}
return dictionary;
}
}
}
from liblevenshtein-java.
Great. Thanks. We will give it a try.
from liblevenshtein-java.
Related Issues (20)
- Support sophisticated, parsing rules when reading a dictionary HOT 1
- Create specialized DawgNodes for common characters
- Consider dynamically-compiling node classes that return constant values
- Support customizing the dictionary-search strategy
- Add development wiki
- Test the example code before generating the documentation
- Create integration test suite
- Create regression test suite
- Cache commands across template tasks for same, Gradle process
- Support variable, error penalties by error type (weighted errors)
- Support variable, error penalties by error type and context
- Support generic, dictionary nodes HOT 1
- Periodically-tweet about resolved issues
- Collapse consecutive, dictionary nodes with only one, outgoing edge HOT 1
- Create a Coverity Scan, Java model
- Before each release, ensure that every closed bug has a regression test
- Add support for One-JAR HOT 1
- Generating string variants constrained by Levenshtein distance HOT 10
- Weighted Levenshtein distance support HOT 2
- Is a transducer thread-safe ? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from liblevenshtein-java.