edu.northwestern.at.morphadorner.corpuslinguistics.inflector.wordrule

## Class AbstractRegexReplacementRule

- java.lang.Object
- - edu.northwestern.at.morphadorner.corpuslinguistics.inflector.wordrule.AbstractRegexReplacementRule

- All Implemented Interfaces:
    WordRule
  Direct Known Subclasses:
    IrregularMappingRule, RegexReplacementRule

      public abstract class AbstractRegexReplacementRule
      extends java.lang.Object
      implements WordRule

  An abstract rule specified using a regular expression and replacement.

  Subclasses must implement `replace(java.util.regex.Matcher)` to perform the actual replacement.

  Original code written by Tom White under the Apache v2 license. Modified by Philip R. Burns for integration into MorphAdorner.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected java.util.regex.Pattern` | `pattern`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `AbstractRegexReplacementRule(java.lang.String regex)` Construct a rule using the given regular expression.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `applies(java.lang.String word)` Tests to see if this rule applies for the given word.
    `java.lang.String` | `apply(java.lang.String word)` Applies this rule to the word, and transforming it into a new form.
    `static java.lang.String` | `disjunction(java.util.Set<java.lang.String> patterns)` Form the disjunction of the given regular expression patterns.
    `static java.lang.String` | `disjunction(java.lang.String[] patterns)` Form the disjunction of the given regular expression patterns.
    `abstract java.lang.String` | `replace(java.util.regex.Matcher matcher)` Use the state in the given `Matcher` to perform a replacement.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### pattern

          protected final java.util.regex.Pattern pattern

  - ### Constructor Detail

    - #### AbstractRegexReplacementRule

          public AbstractRegexReplacementRule(java.lang.String regex)

      Construct a rule using the given regular expression.
      Parameters:
        `regex` - The regular expression used to match words. Match information is available to subclasses in the `replace(java.util.regex.Matcher)` method.

  - ### Method Detail

    - #### applies

          public boolean applies(java.lang.String word)

      Tests to see if this rule applies for the given word.
      Specified by:
        `applies` in interface `WordRule`
      Parameters:
        `word` - The word that is being tested
      Returns:
        `true` if this rule should be applied, `false` otherwise

    - #### apply

          public java.lang.String apply(java.lang.String word)

      Applies this rule to the word, and transforming it into a new form.
      Specified by:
        `apply` in interface `WordRule`
      Parameters:
        `word` - The word to which to apply this rule.
      Returns:
        The transformed word.
      Throws:
        `java.lang.IllegalArgumentException` - when the word does not match the pattern.

    - #### disjunction

          public static java.lang.String disjunction(java.lang.String[] patterns)

      Form the disjunction of the given regular expression patterns.
      Parameters:
        `patterns` - An array of regular expression patterns.
      Returns:
        A pattern that matches if any of the input patterns match.

        For example, if "patterns" contains "a" and "b", then the disjunction is "(a|b)", that is, "a or b".

    - #### disjunction

          public static java.lang.String disjunction(java.util.Set<java.lang.String> patterns)

      Form the disjunction of the given regular expression patterns.
      Parameters:
        `patterns` - A set of regular expression patterns.
      Returns:
        A pattern that matches if any of the input patterns match.

        For example, if "patterns" contains "a" and "b", then the disjunction is "(a|b)", that is, "a or b".

    - #### replace

          public abstract java.lang.String replace(java.util.regex.Matcher matcher)

      Use the state in the given `Matcher` to perform a replacement.
      Parameters:
        `matcher` - The matcher used to match the word.
      Returns:
        The transformed word.