Skip to content

Commit

Permalink
Stateful API example
Browse files Browse the repository at this point in the history
  • Loading branch information
kpu committed Mar 29, 2016
1 parent ef488c5 commit 277403c
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions python/example.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python
import os
import kenlm

Expand Down Expand Up @@ -26,3 +27,17 @@ def score(s):
for w in words:
if not w in model:
print('"{0}" is an OOV'.format(w))

#Stateful query
state = kenlm.State()
state2 = kenlm.State()
#Use <s> as context. If you don't want <s>, use model.NullContextWrite(state).
model.BeginSentenceWrite(state)
accum = 0.0
accum += model.BaseScore(state, "a", state2)
accum += model.BaseScore(state2, "sentence", state)
#score defaults to bos = True and eos = True. Here we'll check without the end
#of sentence marker.
assert (abs(accum - model.score("a sentence", eos = False)) < 1e-3)
accum += model.BaseScore(state, "</s>", state2)
assert (abs(accum - model.score("a sentence")) < 1e-3)

0 comments on commit 277403c

Please sign in to comment.