Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nltk
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
nltk
Commits
e1ba28c2
Commit
e1ba28c2
authored
Mar 21, 2015
by
Ilia Kurenkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added sanity checks and cleaned up a bit
parent
40f27767
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
4 deletions
+11
-4
nltk/model/ngram.py
+11
-4
No files found.
nltk/model/ngram.py
View file @
e1ba28c2
...
...
@@ -174,6 +174,9 @@ class NgramModel(ModelI):
def
_alpha
(
self
,
context
):
"""Get the backoff alpha value for the given context
"""
error_message
=
"Alphas and backoff are not defined for unigram models"
assert
not
self
.
_unigram_model
,
error_message
if
context
in
self
.
_backoff_alphas
:
return
self
.
_backoff_alphas
[
context
]
else
:
...
...
@@ -219,8 +222,8 @@ class NgramModel(ModelI):
return
text
def
_generate_one
(
self
,
context
):
context
=
(
self
.
_lpad
+
tuple
(
context
))[
-
self
.
_n
+
1
:]
# print "Context (%d): <%s>" % (self._n, ','.join(context))
context
=
(
self
.
_lpad
+
tuple
(
context
))[
-
self
.
_n
+
1
:]
if
context
in
self
:
return
self
[
context
]
.
generate
()
elif
self
.
_n
>
1
:
...
...
@@ -258,10 +261,14 @@ class NgramModel(ModelI):
return
pow
(
2.0
,
self
.
entropy
(
text
))
def
__contains__
(
self
,
item
):
return
tuple
(
item
)
in
self
.
_model
if
not
isinstance
(
item
,
tuple
):
item
=
(
item
,)
return
item
in
self
.
_model
def
__getitem__
(
self
,
item
):
return
self
.
_model
[
tuple
(
item
)]
if
not
isinstance
(
item
,
tuple
):
item
=
(
item
,)
return
self
.
_model
[
item
]
def
__repr__
(
self
):
return
'<NgramModel with
%
d
%
d-grams>'
%
(
len
(
self
.
_ngrams
),
self
.
_n
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment