Advertisement
Guest User

Untitled

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