class_docstr: Query interface for backoff N-gram language models
  
  The ngrams format is best explained by an example query: P( world | <s>,
  hello ), i.e. trigram model, probability of "world" given "<s> hello", is:
  `ngrams[2][("<s>", "hello")]["world"]`
  
  On the top level, ngrams is a dict of different history lengths, and each
  order is a dict, with contexts (tuples) as keys and (log-)distributions
  (dicts) as values.
  
  The backoffs format is a little simpler. On the top level, backoffs is a
  list of different context-orders, and each order is a mapping (dict) from
  backoff context to backoff (log-)weight
  
  Arguments
  ---------
  ngrams : dict
      The N-gram log probabilities.
      This is a triply nested dict.
      The first layer is indexed by N-gram order (integer).
      The second layer is indexed by the context (tuple of tokens).
      The third layer is indexed by tokens, and maps to the log prob.
      Example:
      log(P(fox|a quick red)) = -5.3 is accessed by:
      `ngrams[4][('a', 'quick', 'red')]['fox']`
  backoffs : dict
      The backoff log weights.
      This is a doubly nested dict.
      The first layer is indexed by N-gram order (integer).
      The second layer is indexed by the backoff history (tuple of tokens)
      i.e. the context on which the probability distribution is conditioned
      on. This maps to the log weights.
      Example:
      If log(P(fox|a quick red)) is not listed, we find
      log(backoff(a quick red)) = -23.4, which is accessed:
      `backoffs[3][('a', 'quick', 'red')]`
      This dict needs to have entries for orders up to at least N-1 (even if
      they are empty). It may also have entries for order N, though those
      can never be accessed.
  
  Example
  -------
  >>> import math
  >>> ngrams = {1: {tuple(): {'a': -0.6931, 'b': -0.6931}},
  ...           2: {('a',): {'a': -0.6931, 'b': -0.6931},
  ...               ('b',): {'a': -0.6931}}}
  >>> backoffs = {1: {('b',): 0.}}
  >>> lm = BackoffNgramLM(ngrams, backoffs)
  >>> round(math.exp(lm.logprob('a', ('b',))), 1)
  0.5
  >>> round(math.exp(lm.logprob('b', ('b',))), 1)
  0.5
class_docstr_tok: ["Query","interface","for","backoff","N-gram","language","models","The","ngrams","format","is","best","explained","by","an","example","query",":","P","(","world","|","<","s",">",",","hello",")",",","i.e",".","trigram","model",",","probability","of","``","world","''","given","``","<","s",">","hello","''",",","is",":","`","ngrams","[","2","]","[","(","``","<","s",">","''",",","``","hello","''",")","]","[","``","world","''","]","`","On","the","top","level",",","ngrams","is","a","dict","of","different","history","lengths",",","and","each","order","is","a","dict",",","with","contexts","(","tuples",")","as","keys","and","(","log-",")","distributions","(","dicts",")","as","values",".","The","backoffs","format","is","a","little","simpler",".","On","the","top","level",",","backoffs","is","a","list","of","different","context-orders",",","and","each","order","is","a","mapping","(","dict",")","from","backoff","context","to","backoff","(","log-",")","weight","Arguments","--","--","--","--","-","ngrams",":","dict","The","N-gram","log","probabilities",".","This","is","a","triply","nested","dict",".","The","first","layer","is","indexed","by","N-gram","order","(","integer",")",".","The","second","layer","is","indexed","by","the","context","(","tuple","of","tokens",")",".","The","third","layer","is","indexed","by","tokens",",","and","maps","to","the","log","prob",".","Example",":","log","(","P","(","fox|a","quick","red",")",")","=","-5.3","is","accessed","by",":","`","ngrams","[","4","]","[","(","'","a","'",",","'quick","'",",","'red","'",")","]","[","'fox","'","]","`","backoffs",":","dict","The","backoff","log","weights",".","This","is","a","doubly","nested","dict",".","The","first","layer","is","indexed","by","N-gram","order","(","integer",")",".","The","second","layer","is","indexed","by","the","backoff","history","(","tuple","of","tokens",")","i.e",".","the","context","on","which","the","probability","distribution","is","conditioned","on",".","This","maps","to","the","log","weights",".","Example",":","If","log","(","P","(","fox|a","quick","red",")",")","is","not","listed",",","we","find","log","(","backoff","(","a","quick","red",")",")","=","-23.4",",","which","is","accessed",":","`","backoffs","[","3","]","[","(","'","a","'",",","'quick","'",",","'red","'",")","]","`","This","dict","needs","to","have","entries","for","orders","up","to","at","least","N-1","(","even","if","they","are","empty",")",".","It","may","also","have","entries","for","order","N",",","though","those","can","never","be","accessed",".","Example","--","--","--","-",">",">",">","import","math",">",">",">","ngrams","=","{","1",":","{","tuple","(",")",":","{","'","a","'",":","-0.6931",",","'","b","'",":","-0.6931","}","}",",","...","2",":","{","(","'","a","'",",",")",":","{","'","a","'",":","-0.6931",",","'","b","'",":","-0.6931","}",",","...","(","'","b","'",",",")",":","{","'","a","'",":","-0.6931","}","}","}",">",">",">","backoffs","=","{","1",":","{","(","'","b","'",",",")",":","0",".","}","}",">",">",">","lm","=","BackoffNgramLM","(","ngrams",",","backoffs",")",">",">",">","round","(","math.exp","(","lm.logprob","(","'","a","'",",","(","'","b","'",",",")",")",")",",","1",")","0.5",">",">",">","round","(","math.exp","(","lm.logprob","(","'","b","'",",","(","'","b","'",",",")",")",")",",","1",")","0.5"]
docstr: Computes the backoff log weights and applies them.
docstr_tok: ["Computes","the","backoff","log","weights","and","applies","them","."]
code: def logprob(self, token, context=tuple()):
      """Computes the backoff log weights and applies them."""
      # If a longer context is given than we can ever use,
      # just use less context.
      query_order = len(context) + 1
      if query_order > self.top_order:
          return self.logprob(token, context[1:])
      # Now, let's see if we have both:
      # a distribution for the query context at all
      # and if so, a probability for the token.
      # Then we'll just return that.
      if (
          context in self.ngrams[query_order]
          and token in self.ngrams[query_order][context]
      ):
          return self.ngrams[query_order][context][token]
      # If we're here, no direct probability stored for the query.
      # Missing unigram queries are a special case, the recursion will stop.
      if query_order == 1:
          return NEGINFINITY  # Zeroth order for not found
      # Otherwise, we'll backoff to lower order model.
      # First, we'll get add the backoff log weight
      context_order = query_order - 1
      backoff_log_weight = self.backoffs[context_order].get(
          context, 0.0
      )
      # And then just recurse:
      lp = self.logprob(token, context[1:])
      return lp + backoff_log_weight
  
imports: import collections
label_desc: Use image node_id 2 for calling the BackoffNgramLM obj's underlying member method code with example usage: obj.logprob(token, context) and returns: unknown, self, self, NEGINFINITY