edu.berkeley.nlp.lm.cache
Class ArrayEncodedCachingLmWrapper<W>

java.lang.Object
  extended by edu.berkeley.nlp.lm.AbstractNgramLanguageModel<W>
      extended by edu.berkeley.nlp.lm.AbstractArrayEncodedNgramLanguageModel<W>
          extended by edu.berkeley.nlp.lm.cache.ArrayEncodedCachingLmWrapper<W>
Type Parameters:
W -
All Implemented Interfaces:
ArrayEncodedNgramLanguageModel<W>, NgramLanguageModel<W>, Serializable

public class ArrayEncodedCachingLmWrapper<W>
extends AbstractArrayEncodedNgramLanguageModel<W>

This class wraps ArrayEncodedNgramLanguageModel with a cache.

Author:
adampauls
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface edu.berkeley.nlp.lm.ArrayEncodedNgramLanguageModel
ArrayEncodedNgramLanguageModel.DefaultImplementations
 
Nested classes/interfaces inherited from interface edu.berkeley.nlp.lm.NgramLanguageModel
NgramLanguageModel.StaticMethods
 
Field Summary
 
Fields inherited from class edu.berkeley.nlp.lm.AbstractNgramLanguageModel
lmOrder, oovWordLogProb
 
Method Summary
 float getLogProb(int[] ngram, int startPos, int endPos)
          Calculate language model score of an n-gram.
static
<W> ArrayEncodedCachingLmWrapper<W>
wrapWithCacheNotThreadSafe(ArrayEncodedNgramLanguageModel<W> lm)
          To use this wrapper in a multithreaded environment, you should create one wrapper per thread.
static
<W> ArrayEncodedCachingLmWrapper<W>
wrapWithCacheNotThreadSafe(ArrayEncodedNgramLanguageModel<W> lm, int cacheBits)
           
static
<W> ArrayEncodedCachingLmWrapper<W>
wrapWithCacheThreadSafe(ArrayEncodedNgramLanguageModel<W> lm)
          This type of caching is threadsafe and (internally) maintains a separate cache for each thread that calls it.
static
<W> ArrayEncodedCachingLmWrapper<W>
wrapWithCacheThreadSafe(ArrayEncodedNgramLanguageModel<W> lm, int cacheBits)
           
 
Methods inherited from class edu.berkeley.nlp.lm.AbstractArrayEncodedNgramLanguageModel
getLogProb, getLogProb, scoreSentence
 
Methods inherited from class edu.berkeley.nlp.lm.AbstractNgramLanguageModel
getLmOrder, getWordIndexer, setOovWordLogProb
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface edu.berkeley.nlp.lm.NgramLanguageModel
getLmOrder, getWordIndexer, setOovWordLogProb
 

Method Detail

wrapWithCacheNotThreadSafe

public static <W> ArrayEncodedCachingLmWrapper<W> wrapWithCacheNotThreadSafe(ArrayEncodedNgramLanguageModel<W> lm)
To use this wrapper in a multithreaded environment, you should create one wrapper per thread.

Type Parameters:
T -
Parameters:
lm -
Returns:

wrapWithCacheNotThreadSafe

public static <W> ArrayEncodedCachingLmWrapper<W> wrapWithCacheNotThreadSafe(ArrayEncodedNgramLanguageModel<W> lm,
                                                                             int cacheBits)

wrapWithCacheThreadSafe

public static <W> ArrayEncodedCachingLmWrapper<W> wrapWithCacheThreadSafe(ArrayEncodedNgramLanguageModel<W> lm)
This type of caching is threadsafe and (internally) maintains a separate cache for each thread that calls it. Note each thread has its own cache, so if you have lots of threads, memory usage could be substantial.

Type Parameters:
W -
Parameters:
lm -
Returns:

wrapWithCacheThreadSafe

public static <W> ArrayEncodedCachingLmWrapper<W> wrapWithCacheThreadSafe(ArrayEncodedNgramLanguageModel<W> lm,
                                                                          int cacheBits)

getLogProb

public float getLogProb(int[] ngram,
                        int startPos,
                        int endPos)
Description copied from interface: ArrayEncodedNgramLanguageModel
Calculate language model score of an n-gram. Warning: if you pass in an n-gram of length greater than getLmOrder(), this call will silently ignore the extra words of context. In other words, if you pass in a 5-gram (endPos-startPos == 5) to a 3-gram model, it will only score the words from startPos + 2 to endPos.

Specified by:
getLogProb in interface ArrayEncodedNgramLanguageModel<W>
Specified by:
getLogProb in class AbstractArrayEncodedNgramLanguageModel<W>
Parameters:
ngram - array of words in integer representation
startPos - start of the portion of the array to be read
endPos - end of the portion of the array to be read.
Returns: