Giter VIP home page Giter VIP logo

Comments (4)

dylon avatar dylon commented on July 16, 2024

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.

szeke avatar szeke commented on July 16, 2024

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.

dylon avatar dylon commented on July 16, 2024

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.

szeke avatar szeke commented on July 16, 2024

Great. Thanks. We will give it a try.

from liblevenshtein-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.