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
b912a222
Commit
b912a222
authored
Feb 08, 2012
by
Peter Ljunglöf
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Minor optimizations to align.py, and doctest fixes
parent
4722f1d5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
33 deletions
+46
-33
nltk/align.py
+20
-16
nltk/test/align.doctest
+26
-17
No files found.
nltk/align.py
View file @
b912a222
...
@@ -300,11 +300,16 @@ class IBMModel1(object):
...
@@ -300,11 +300,16 @@ class IBMModel1(object):
- Stage 2: Generates updated word alignments for the sentence pairs, based
- Stage 2: Generates updated word alignments for the sentence pairs, based
on the translation probabilities from Stage 1.
on the translation probabilities from Stage 1.
.. doctest::
>>> corpus = [AlignedSent(['the', 'house'], ['das', 'Haus']),
... AlignedSent(['the', 'book'], ['das', 'Buch']),
>> from nltk.corpus import comtrans
... AlignedSent(['a', 'book'], ['ein', 'Buch'])]
>> from nltk.align import IBMModel1
>>> ibm1 = IBMModel1(corpus)
>> ibm1 = IBMModel1(comtrans.aligned_sents())
>>> print "
%.1
f"
%
ibm1.probabilities['book', 'Buch']
1.0
>>> print "
%.1
f"
%
ibm1.probabilities['book', 'das']
0.0
>>> print "
%.1
f"
%
ibm1.probabilities['book', None]
0.5
:param aligned_sents: The parallel text ``corpus.Iterable`` containing
:param aligned_sents: The parallel text ``corpus.Iterable`` containing
AlignedSent instances of aligned sentence pairs from the corpus.
AlignedSent instances of aligned sentence pairs from the corpus.
...
@@ -326,9 +331,9 @@ class IBMModel1(object):
...
@@ -326,9 +331,9 @@ class IBMModel1(object):
def
_train
(
self
):
def
_train
(
self
):
"""
"""
Perform Expectation Maximization training to learn
Perform Expectation Maximization training to learn
word-to-word translation probabilities, and return
word-to-word translation probabilities.
the number of iterations that were required for convergence.
"""
"""
logging
.
debug
(
"Starting training"
)
# Collect up sets of all English and foreign words
# Collect up sets of all English and foreign words
english_words
=
set
()
english_words
=
set
()
...
@@ -338,15 +343,13 @@ class IBMModel1(object):
...
@@ -338,15 +343,13 @@ class IBMModel1(object):
foreign_words
.
update
(
aligned_sent
.
mots
)
foreign_words
.
update
(
aligned_sent
.
mots
)
# add the NULL token to the foreign word set.
# add the NULL token to the foreign word set.
foreign_words
.
add
(
None
)
foreign_words
.
add
(
None
)
num_probs
=
len
(
english_words
)
*
len
(
foreign_words
)
num_probs
=
len
(
english_words
)
*
len
(
foreign_words
)
# Initialise t(e|f) uniformly
# Initialise t(e|f) uniformly
t
=
defaultdict
(
lambda
:
float
(
1
)
/
len
(
english_words
))
default_prob
=
1.0
/
len
(
english_words
)
s_total
=
defaultdict
(
float
)
t
=
defaultdict
(
lambda
:
default_prob
)
for
e
in
english_words
:
for
f
in
foreign_words
:
z
=
t
[
e
,
f
]
convergent_threshold
=
self
.
convergent_threshold
globally_converged
=
False
globally_converged
=
False
iteration_count
=
0
iteration_count
=
0
while
not
globally_converged
:
while
not
globally_converged
:
...
@@ -356,6 +359,7 @@ class IBMModel1(object):
...
@@ -356,6 +359,7 @@ class IBMModel1(object):
total
=
defaultdict
(
float
)
total
=
defaultdict
(
float
)
for
aligned_sent
in
self
.
aligned_sents
:
for
aligned_sent
in
self
.
aligned_sents
:
s_total
=
{}
# Compute normalization
# Compute normalization
for
e_w
in
aligned_sent
.
words
:
for
e_w
in
aligned_sent
.
words
:
s_total
[
e_w
]
=
0.0
s_total
[
e_w
]
=
0.0
...
@@ -375,7 +379,7 @@ class IBMModel1(object):
...
@@ -375,7 +379,7 @@ class IBMModel1(object):
for
e_w
in
english_words
:
for
e_w
in
english_words
:
new_prob
=
count
[
e_w
,
f_w
]
/
total
[
f_w
]
new_prob
=
count
[
e_w
,
f_w
]
/
total
[
f_w
]
delta
=
abs
(
t
[
e_w
,
f_w
]
-
new_prob
)
delta
=
abs
(
t
[
e_w
,
f_w
]
-
new_prob
)
if
delta
<
self
.
convergent_threshold
:
if
delta
<
convergent_threshold
:
num_converged
+=
1
num_converged
+=
1
t
[
e_w
,
f_w
]
=
new_prob
t
[
e_w
,
f_w
]
=
new_prob
...
@@ -383,8 +387,8 @@ class IBMModel1(object):
...
@@ -383,8 +387,8 @@ class IBMModel1(object):
iteration_count
+=
1
iteration_count
+=
1
if
num_converged
==
num_probs
:
if
num_converged
==
num_probs
:
globally_converged
=
True
globally_converged
=
True
logging
.
debug
(
"
%
d/
%
d (
%.2
f
%%
) converged"
%
(
logging
.
debug
(
"
%
d/
%
d (
%.2
f
%%
) converged"
%
num_converged
,
num_probs
,
100.0
*
num_converged
/
num_probs
))
(
num_converged
,
num_probs
,
100.0
*
num_converged
/
num_probs
))
self
.
probabilities
=
dict
(
t
)
self
.
probabilities
=
dict
(
t
)
...
...
nltk/test/align.doctest
View file @
b912a222
...
@@ -53,13 +53,12 @@ the corresponding sentences:
...
@@ -53,13 +53,12 @@ the corresponding sentences:
...
...
IndexError: Alignment is outside boundary of mots
IndexError: Alignment is outside boundary of mots
.. in Python 2.6 version, we will support:
als.alignment = Alignment([(0, 0), (1, 4), (2, 1), (3, 3)])
You can set alignments with any sequence of tuples, so long as the first two
You can set alignments with any sequence of tuples, so long as the first two
indexes of the tuple are the alignment indices:
indexes of the tuple are the alignment indices:
als.alignment = Alignment([(0, 0), (1, 1), (2, 2, "boat"), (3, 3, False, (1,2))])
>>> Alignment([(0, 0), (1, 1), (2, 2, "boat"), (3, 3, False, (1,2))])
>>> Alignment([(0, 0), (1, 1), (2, 2, "boat"), (3, 3, False, (1,2))])
Alignment([(0, 0), (1, 1), (2, 2, 'boat'), (3, 3, False, (1, 2))])
Alignment([(0, 0), (1, 1), (2, 2, 'boat'), (3, 3, False, (1, 2))])
...
@@ -72,36 +71,46 @@ EM for IBM Model 1
...
@@ -72,36 +71,46 @@ EM for IBM Model 1
Here is an example from Kohn, 2010:
Here is an example from Kohn, 2010:
>>> from nltk.align import IBMModel1
# doctest +SKIP
>>> from nltk.align import IBMModel1
>>> corpus = [AlignedSent(['the', 'house'], ['das', 'Haus']),
>>> corpus = [AlignedSent(['the', 'house'], ['das', 'Haus']),
... AlignedSent(['the', 'book'], ['das', 'Buch']),
... AlignedSent(['the', 'book'], ['das', 'Buch']),
... AlignedSent(['a', 'book'], ['ein', 'Buch'])]
# doctest +SKIP
... AlignedSent(['a', 'book'], ['ein', 'Buch'])]
>>> em_ibm1 = IBMModel1(corpus, 1e-3)
# doctest +SKIP
>>> em_ibm1 = IBMModel1(corpus, 1e-3)
>>> print round(em_ibm1.probabilities['the', 'das'], 1)
# doctest +SKIP
>>> print round(em_ibm1.probabilities['the', 'das'], 1)
1.0
1.0
>>> print round(em_ibm1.probabilities['book', 'das'], 1)
# doctest +SKIP
>>> print round(em_ibm1.probabilities['book', 'das'], 1)
0.0
0.0
>>> print round(em_ibm1.probabilities['house', 'das'], 1)
# doctest +SKIP
>>> print round(em_ibm1.probabilities['house', 'das'], 1)
0.0
0.0
>>> print round(em_ibm1.probabilities['the', 'Buch'], 1)
# doctest +SKIP
>>> print round(em_ibm1.probabilities['the', 'Buch'], 1)
0.0
0.0
>>> print round(em_ibm1.probabilities['book', 'Buch'], 1)
# doctest +SKIP
>>> print round(em_ibm1.probabilities['book', 'Buch'], 1)
1.0
1.0
>>> print round(em_ibm1.probabilities['a', 'Buch'], 1)
# doctest +SKIP
>>> print round(em_ibm1.probabilities['a', 'Buch'], 1)
0.0
0.0
>>> print round(em_ibm1.probabilities['book', 'ein'], 1)
# doctest +SKIP
>>> print round(em_ibm1.probabilities['book', 'ein'], 1)
0.0
0.0
>>> print round(em_ibm1.probabilities['a', 'ein'], 1)
# doctest +SKIP
>>> print round(em_ibm1.probabilities['a', 'ein'], 1)
1.0
1.0
>>> print round(em_ibm1.probabilities['the', 'Haus'], 1)
# doctest +SKIP
>>> print round(em_ibm1.probabilities['the', 'Haus'], 1)
0.0
0.0
>>> print round(em_ibm1.probabilities['house', 'Haus'], 1)
# doctest +SKIP
>>> print round(em_ibm1.probabilities['house', 'Haus'], 1)
1.0
1.0
>>> print round(em_ibm1.probabilities['book', None], 1)
0.5
And using an NLTK corpus. We train on only 10 sentences, since it is so incredibly slow:
>>> from nltk.corpus import comtrans
>>> com_ibm1 = IBMModel1(comtrans.aligned_sents() [:10])
>>> print round(com_ibm1.probabilities['bitte', 'Please'], 1)
0.2
>>> print round(com_ibm1.probabilities['Sitzungsperiode', 'session'], 1)
1.0
Get the alignments:
Get the alignments:
>>> em_ibm1.aligned() # doctest: +
SKIP
>>> em_ibm1.aligned() # doctest: +
NORMALIZE_WHITESPACE
[AlignedSent(['the', 'house'], ['das', 'Haus'],
[AlignedSent(['the', 'house'], ['das', 'Haus'],
Alignment([(0, 0), (1, 1)])),
Alignment([(0, 0), (1, 1)])),
AlignedSent(['the', 'book'], ['das', 'Buch'],
AlignedSent(['the', 'book'], ['das', 'Buch'],
...
...
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