edu.northwestern.at.morphadorner.corpuslinguistics.postagger.smoothing.lexical

## Class AbstractLexicalSmoother

- java.lang.Object
- - edu.northwestern.at.utils.IsCloseableObject
  - - edu.northwestern.at.morphadorner.corpuslinguistics.postagger.smoothing.lexical.AbstractLexicalSmoother

- All Implemented Interfaces:
    LexicalSmoother, UsesLogger
  Direct Known Subclasses:
    AdditiveLexicalSmoother

      public abstract class AbstractLexicalSmoother
      extends IsCloseableObject
      implements LexicalSmoother, UsesLogger

  Abstract lexical smoother.

  An abstract lexical smoother which provides implementations of common service methods such as setting the lexicon and the transition proability matrix. Extend this class and override the abstract method "lexicalProbability" to produce a new lexical smoother.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected Map2D<java.lang.String,java.lang.String,Probability>` | `cachedLexicalProbabilities` Cached lexical probabilities for words.
    `protected Logger` | `logger` Logger used for output.
    `protected PartOfSpeechTagger` | `partOfSpeechTagger` The part of speech tagger for which this smoother provides amoother contextual probabilities.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `AbstractLexicalSmoother()` Create an abstract lexical smoother.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `cachedProbabilitiesCount()` Get the number of cached lexical probabilities.
    `void` | `clearCachedProbabilities()` Clear cached probabilities..
    `Logger` | `getLogger()` Get the logger.
    `abstract Probability` | `lexicalProbability(java.lang.String word, java.lang.String tag)` Get lexically smoothed probability of a word given a tag.
    `void` | `setLogger(Logger logger)` Set the logger.
    `void` | `setPartOfSpeechTagger(PartOfSpeechTagger partOfSpeechTagger)` Set the part of speech tagger for this smoother.

    - ### Methods inherited from class edu.northwestern.at.utils.IsCloseableObject

      `close`

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

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

- - ### Field Detail

    - #### partOfSpeechTagger

          protected PartOfSpeechTagger partOfSpeechTagger

      The part of speech tagger for which this smoother provides amoother contextual probabilities.

    - #### cachedLexicalProbabilities

          protected Map2D<java.lang.String,java.lang.String,Probability> cachedLexicalProbabilities

      Cached lexical probabilities for words.

    - #### logger

          protected Logger logger

      Logger used for output.

  - ### Constructor Detail

    - #### AbstractLexicalSmoother

          public AbstractLexicalSmoother()

      Create an abstract lexical smoother.

  - ### Method Detail

    - #### getLogger

          public Logger getLogger()

      Get the logger.
      Specified by:
        `getLogger` in interface `UsesLogger`
      Returns:
        The logger.

    - #### setLogger

          public void setLogger(Logger logger)

      Set the logger.
      Specified by:
        `setLogger` in interface `UsesLogger`
      Parameters:
        `logger` - The logger.

    - #### setPartOfSpeechTagger

          public void setPartOfSpeechTagger(PartOfSpeechTagger partOfSpeechTagger)

      Set the part of speech tagger for this smoother.
      Specified by:
        `setPartOfSpeechTagger` in interface `LexicalSmoother`
      Parameters:
        `partOfSpeechTagger` - Part of speech tagger for which this smoother provides probabilities.

    - #### cachedProbabilitiesCount

          public int cachedProbabilitiesCount()

      Get the number of cached lexical probabilities.
      Specified by:
        `cachedProbabilitiesCount` in interface `LexicalSmoother`
      Returns:
        The number of cached lexical probabilities.

    - #### clearCachedProbabilities

          public void clearCachedProbabilities()

      Clear cached probabilities..
      Specified by:
        `clearCachedProbabilities` in interface `LexicalSmoother`

    - #### lexicalProbability

          public abstract Probability lexicalProbability(java.lang.String word,
                                       java.lang.String tag)

      Get lexically smoothed probability of a word given a tag.
      Specified by:
        `lexicalProbability` in interface `LexicalSmoother`
      Parameters:
        `word` - The word.
        `tag` - The part of speech tag.
      Returns:
        Lexically smoothed probability of word given tag, e.g., p( word | tag ).

        To avoid redoing potentially expensive probability calculations, you can use the "cachedLexicalProbabilities" HashMap2D to store probabilities once they are calculated. Your lexicalProbability method should look to see if the cache contains the needed lexical probability. If so, just retrieve it without recomputing it. If the cache does not contain the probability, compute it, and store it in the cache for future use.

        Here is what a lexicalProbability method should look like, using the cache.

              protected Probability lexicalProbability( String word , String tag )
              {
                                          //  See if the lexical probability
                                          //  p( word | tag ) is in the cache.

                  Probability result  =
                      (Probability)cachedLexicalProbabilities.get( word , tag );

                                          //  If the probability isn't in the
                                          //  cache, compute it.

                  if ( result == null )
                  {
                      double prob     = compute smoothed probability value

                                          //  Store computed probability in
                                          //  the cache.

                      result  = new Probability( prob );

                      cachedLexicalProbabilities.put( word , tag , result );
                  }

                  return result;
              }