Class PatchCommandEncoder

java.lang.Object
org.egothor.stemmer.PatchCommandEncoder

public final class PatchCommandEncoder extends Object
Encodes a compact patch command that transforms one word form into another and applies such commands back to source words.

The historical Egothor patch language is defined for backward traversal, that is, from the logical end of a word toward its beginning. This implementation preserves that proven opcode semantics as the single internal representation. Forward traversal is implemented by translating source and target words to the equivalent reversed logical form at the API boundary and then delegating to the same backward encoder and decoder.

This design keeps the patch language stable, avoids maintaining two distinct opcode interpreters, and guarantees that forward traversal is semantically equivalent to running the historical algorithm on the reversed logical word form.

The encoder computes a minimum-cost edit script using weighted insert, delete, replace, and match transitions. The resulting trace is then serialized into the compact patch language.

This class is stateful and reuses internal dynamic-programming matrices across invocations to reduce allocation pressure during repeated use. Instances are therefore not suitable for unsynchronized concurrent access. The encode(String, String) method is synchronized so that a shared instance can still be used safely when needed.

  • Method Details

    • builder

      public static PatchCommandEncoder.Builder builder()
      Creates a fluent builder for constructing a direction-specialized encoder.
      Returns:
      new builder instance
    • encode

      public String encode(String source, String target)
      Produces a compact patch command that transforms source into target.
      Parameters:
      source - source word form
      target - target word form
      Returns:
      compact patch command, or null when any argument is null
    • applyWithConfiguredDirection

      public String applyWithConfiguredDirection(String source, String patchCommand)
      Applies a compact patch command using this encoder instance traversal direction.

      This is the branch-free instance-level fast path for repeated patch application in a known traversal direction.

      Parameters:
      source - original source word
      patchCommand - compact patch command
      Returns:
      transformed word, or null when source is null
    • apply

      public static String apply(String source, String patchCommand)
      Applies a compact patch command to the supplied source word using the historical backward traversal direction.
      Parameters:
      source - original source word
      patchCommand - compact patch command
      Returns:
      transformed word, or null when source is null
    • apply

      public static String apply(String source, String patchCommand, WordTraversalDirection traversalDirection)
      Applies a compact patch command to the supplied source word using the specified traversal direction.

      The implementation uses dedicated direction-specific patch decoders.

      Parameters:
      source - original source word
      patchCommand - compact patch command
      traversalDirection - traversal direction used by the patch command
      Returns:
      transformed word, or null when source is null