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
30eeccca
Commit
30eeccca
authored
Dec 01, 2011
by
Steven Bird
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
moved demonstration code to doctest
parent
80a66033
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
13 deletions
+14
-13
nltk/model/ngram.py
+14
-13
No files found.
nltk/model/ngram.py
View file @
30eeccca
...
...
@@ -37,6 +37,17 @@ class NgramModel(ModelI):
from the text and may allow generation of ngrams not seen during
training.
>>> from nltk.corpus import brown
>>> from nltk.probability import LidstoneProbDist
>>> estimator = lambda fdist, bins: LidstoneProbDist(fdist, 0.2)
>>> lm = NgramModel(3, brown.words(categories='news'), estimator)
>>> lm.entropy(['The', 'Fulton', 'County', 'Grand', 'Jury', 'said',
... 'Friday', 'an', 'investigation', 'of', "Atlanta's", 'recent',
... 'primary', 'election', 'produced', '``', 'no', 'evidence',
... "''", 'that', 'any', 'irregularities', 'took', 'place', '.'])
... # doctest: +ELLIPSIS
1.682...
:param n: the order of the language model (ngram size)
:type n: int
:param train: the training text
...
...
@@ -194,16 +205,6 @@ class NgramModel(ModelI):
return
'<NgramModel with
%
d
%
d-grams>'
%
(
len
(
self
.
_ngrams
),
self
.
_n
)
def
demo
():
from
nltk.corpus
import
brown
from
nltk.probability
import
LidstoneProbDist
,
WittenBellProbDist
estimator
=
lambda
fdist
,
bins
:
LidstoneProbDist
(
fdist
,
0.2
)
lm
=
NgramModel
(
3
,
brown
.
words
(
categories
=
'news'
),
estimator
)
print
lm
#print lm.entropy(sent)
text
=
lm
.
generate
(
100
)
import
textwrap
print
'
\n
'
.
join
(
textwrap
.
wrap
(
' '
.
join
(
text
)))
if
__name__
==
'__main__'
:
demo
()
if
__name__
==
"__main__"
:
import
doctest
doctest
.
testmod
(
optionflags
=
doctest
.
NORMALIZE_WHITESPACE
)
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