Skip to content

Commit

Permalink
Add sentence perplexity
Browse files Browse the repository at this point in the history
  • Loading branch information
kpu committed Jun 27, 2016
1 parent 1d64c39 commit ff50c09
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions python/kenlm.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ cdef class Model:
if eos:
total += self.model.BaseScore(&state, self.vocab.EndSentence(), &out_state)
return total

def perplexity(self, sentence):
"""
Compute perplexity of a sentence.
@param sentence One full sentence to score. Do not include <s> or </s>.
"""
words = len(to_str(sentence).split()) + 1 # For </s>
return 10.0**(-self.score(sentence) / words)

def full_scores(self, sentence, bos = True, eos = True):
"""
Expand Down

0 comments on commit ff50c09

Please sign in to comment.