All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.liblevenshtein.transducer.factory.StateTransitionFactory Maven / Gradle / Ivy

There is a newer version: 3.0.1
Show newest version
package com.github.liblevenshtein.transducer.factory;

import com.github.liblevenshtein.transducer.MergeFunction;
import com.github.liblevenshtein.transducer.Position;
import com.github.liblevenshtein.transducer.StateTransitionFunction;
import com.github.liblevenshtein.transducer.UnsubsumeFunction;

import java.io.Serializable;
import java.util.Comparator;

/**
 * Builds (and recycles) instances of {@link StateTransitionFunction}.
 * @author Dylon Edwards
 * @since 2.1.0
 */
public class StateTransitionFactory implements Serializable {

  private static final long serialVersionUID = 1L;

  /**
   * Compares Levenshtein-state positions.
   */
  private Comparator comparator;

  /**
   * Builds Levenshtein states.
   */
  private StateFactory stateFactory;

  /**
   * Builds position-transition functions.
   */
  private PositionTransitionFactory positionTransitionFactory;

  /**
   * Merges Levenshtein states together.
   */
  private MergeFunction merge;

  /**
   * Removes subsumed positions from Levenshtein states.
   */
  private UnsubsumeFunction unsubsume;

  public StateTransitionFactory comparator(Comparator comparator) {
    this.comparator = comparator;
    return this;
  }

  public StateTransitionFactory stateFactory(StateFactory stateFactory) {
    this.stateFactory = stateFactory;
    return this;
  }

  public StateTransitionFactory positionTransitionFactory(PositionTransitionFactory positionTransitionFactory) {
    this.positionTransitionFactory = positionTransitionFactory;
    return this;
  }

  public StateTransitionFactory merge(MergeFunction merge) {
    this.merge = merge;
    return this;
  }

  public StateTransitionFactory unsubsume(UnsubsumeFunction unsubsume) {
    this.unsubsume = unsubsume;
    return this;
  }

  /**
   * Builds a new state-transition function that only considers spelling
   * candidates no more than {@code maxDistance} errors from the query term.
   * @param maxDistance Maximum number of errors tolerated in transforming the
   *   query term into the spelling candidate.
   * @param queryLength Length of the query term.
   * @return New state-transition function that only considers spelling
   *   candidates within {@code maxDistance} errors from the query term.
   */
  public StateTransitionFunction build(
      final int maxDistance,
      final int queryLength) {
    return new StateTransitionFunction()
      .comparator(comparator)
      .stateFactory(stateFactory)
      .transitionFactory(positionTransitionFactory)
      .merge(merge)
      .unsubsume(unsubsume)
      .maxDistance(maxDistance)
      .queryLength(queryLength);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy