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

## Class AbstractContextualSmoother

- java.lang.Object
- - edu.northwestern.at.utils.IsCloseableObject
  - - edu.northwestern.at.morphadorner.corpuslinguistics.postagger.smoothing.contextual.AbstractContextualSmoother

- All Implemented Interfaces:
    ContextualSmoother, UsesLogger
  Direct Known Subclasses:
    AdditiveContextualSmoother, DeletedInterpolationContextualSmoother

      public abstract class AbstractContextualSmoother
      extends IsCloseableObject
      implements ContextualSmoother, UsesLogger

  Abstract contextual smoother.

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

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected Map3D<java.lang.String,java.lang.String,java.lang.String,Probability>` | `cachedContextualProbabilities` Cached contextual probabilities.
    `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
    `AbstractContextualSmoother()` Create an abstract contextual smoother.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `cachedProbabilitiesCount()` Get the number of cached contextual probabilities.
    `void` | `clearCachedProbabilities()` Clear cached probabilities..
    `abstract Probability` | `contextualProbability(java.lang.String word, java.lang.String tag)` Get contextually smoothed probability of a word given a tag.
    `Logger` | `getLogger()` Get the logger.
    `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`

    - ### Methods inherited from interface edu.northwestern.at.morphadorner.corpuslinguistics.postagger.smoothing.contextual.ContextualSmoother

      `contextualProbability`

- - ### Field Detail

    - #### partOfSpeechTagger

          protected PartOfSpeechTagger partOfSpeechTagger

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

    - #### cachedContextualProbabilities

          protected Map3D<java.lang.String,java.lang.String,java.lang.String,Probability> cachedContextualProbabilities

      Cached contextual probabilities.

    - #### logger

          protected Logger logger

      Logger used for output.

  - ### Constructor Detail

    - #### AbstractContextualSmoother

          public AbstractContextualSmoother()

      Create an abstract contextual 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 `ContextualSmoother`
      Parameters:
        `partOfSpeechTagger` - Part of speech tagger for which this smoother provides probabilities.

    - #### cachedProbabilitiesCount

          public int cachedProbabilitiesCount()

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

    - #### clearCachedProbabilities

          public void clearCachedProbabilities()

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

    - #### contextualProbability

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

      Get contextually smoothed probability of a word given a tag.
      Specified by:
        `contextualProbability` in interface `ContextualSmoother`
      Parameters:
        `word` - The word.
        `tag` - The part of speech tag.
      Returns:
        Contextually smoothed probability of word given tag, e.g., p( word | tag ).

        To avoid redoing potentially expensive probability calculations, you can use the "cachedContextualProbabilities" HashMap2D to store probabilities once they are calculated. Your contextualProbability method should look to see if the cache contains the needed contextual 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 contextualProbability method should look like, using the cache.

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

                  Probability result  =
                      (Probability)cachedContextualProbabilities.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 );

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

                  return result;
              }