Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. # Linear interpolation (for trigram model only)
  2. def LinearInterpolation(ngram, freqs):
  3. global A, B, C # meta-parameters given by global variables A, B, C
  4. n = len(ngram)
  5. if n != 3:
  6. raise NotImplementedError, "linear interpolation is only defined for trigrams"
  7.  
  8.  
  9. condProb3 = MLE(ngram, freqs)
  10. condProb2 = MLE(ngram[1:], freqs)
  11. condProb1 = MLE(ngram[2], freqs)
  12.  
  13. if condProb2 == 0:
  14. return condProb1
  15.  
  16. if condProb3 == 0:
  17. norm = 1.0/(B+C)
  18. b = B*norm
  19. c = C*norm
  20. return b*condProb2 + c*condProb1
  21.  
  22. return A*condProb3 + B*condProb2 + C*condProb1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement