Skip to content

Commit fb3a08c

Browse files
committed
Fix some failing doctests
1 parent f402781 commit fb3a08c

File tree

3 files changed

+38
-40
lines changed

3 files changed

+38
-40
lines changed

nltk/collections.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
import bisect
99

10-
# this unused import is for python 2.7
11-
from collections import Counter, defaultdict, deque
1210
from functools import total_ordering
1311
from itertools import chain, islice
1412

@@ -61,7 +59,6 @@ def copy(self):
6159
return d
6260

6361
def items(self):
64-
# returns iterator under python 3 and list under python 2
6562
return zip(self.keys(), self.values())
6663

6764
def keys(self, data=None, keys=None):
@@ -106,7 +103,6 @@ def update(self, data):
106103
self._keys.append(key)
107104

108105
def values(self):
109-
# returns iterator under python 3
110106
return map(self.get, self._keys)
111107

112108

nltk/corpus/reader/wordnet.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -588,14 +588,13 @@ def closure(self, rel, depth=-1):
588588
(from 'animal.n.01' to 'entity.n.01'):
589589
590590
>>> dog = wn.synset('dog.n.01')
591-
>>> hyp = lambda s:s.hypernyms()
592-
>>> print(list(dog.closure(hyp)))
593-
[Synset('canine.n.02'), Synset('domestic_animal.n.01'), Synset('carnivore.n.01'),\
594-
Synset('animal.n.01'), Synset('placental.n.01'), Synset('organism.n.01'),\
595-
Synset('mammal.n.01'), Synset('living_thing.n.01'), Synset('vertebrate.n.01'),\
596-
Synset('whole.n.02'), Synset('chordate.n.01'), Synset('object.n.01'),\
597-
Synset('physical_entity.n.01'), Synset('entity.n.01')]
598-
591+
>>> hyp = lambda s:sorted(s.hypernyms())
592+
>>> print(sorted(dog.closure(hyp)))
593+
[Synset('animal.n.01'), Synset('canine.n.02'), Synset('carnivore.n.01'),\
594+
Synset('chordate.n.01'), Synset('domestic_animal.n.01'), Synset('entity.n.01'),\
595+
Synset('living_thing.n.01'), Synset('mammal.n.01'), Synset('object.n.01'),\
596+
Synset('organism.n.01'), Synset('physical_entity.n.01'), Synset('placental.n.01'),\
597+
Synset('vertebrate.n.01'), Synset('whole.n.02')]
599598
UserWarning: Discarded redundant search for Synset('animal.n.01') at depth 7
600599
"""
601600

@@ -619,7 +618,7 @@ def tree(self, rel, depth=-1, cut_mark=None):
619618
>>> from nltk.corpus import wordnet as wn
620619
>>> from pprint import pprint
621620
>>> computer = wn.synset('computer.n.01')
622-
>>> topic = lambda s:s.topic_domains()
621+
>>> topic = lambda s:sorted(s.topic_domains())
623622
>>> pprint(computer.tree(topic))
624623
[Synset('computer.n.01'), [Synset('computer_science.n.01')]]
625624
@@ -629,7 +628,7 @@ def tree(self, rel, depth=-1, cut_mark=None):
629628
But keep duplicate branches (from 'animal.n.01' to 'entity.n.01'):
630629
631630
>>> dog = wn.synset('dog.n.01')
632-
>>> hyp = lambda s:s.hypernyms()
631+
>>> hyp = lambda s:sorted(s.hypernyms())
633632
>>> pprint(dog.tree(hyp))
634633
[Synset('dog.n.01'),
635634
[Synset('canine.n.02'),

nltk/util.py

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ def unweighted_minimum_spanning_digraph(tree, children=iter, shapes=None, attr=N
353353
>>> import nltk
354354
>>> wn=nltk.corpus.wordnet
355355
>>> from nltk.util import unweighted_minimum_spanning_digraph as umsd
356-
>>> print(umsd(wn.synset('bound.a.01'), lambda s:s.also_sees()))
356+
>>> print(umsd(wn.synset('bound.a.01'), lambda s:sorted(s.also_sees())))
357357
digraph G {
358358
"Synset('bound.a.01')" -> "Synset('unfree.a.02')";
359359
"Synset('unfree.a.02')" -> "Synset('confined.a.02')";
@@ -377,7 +377,7 @@ def unweighted_minimum_spanning_digraph(tree, children=iter, shapes=None, attr=N
377377
##########################################################################
378378

379379

380-
def acyclic_breadth_first(tree, children=iter, maxdepth=-1):
380+
def acyclic_breadth_first(tree, children=iter, maxdepth=-1, verbose=False):
381381
"""Traverse the nodes of a tree in breadth-first order,
382382
discarding eventual cycles.
383383
@@ -397,17 +397,18 @@ def acyclic_breadth_first(tree, children=iter, maxdepth=-1):
397397
if child not in traversed:
398398
queue.append((child, depth + 1))
399399
else:
400-
warnings.warn(
401-
"Discarded redundant search for {} at depth {}".format(
402-
child, depth + 1
403-
),
404-
stacklevel=2,
405-
)
400+
if verbose:
401+
warnings.warn(
402+
"Discarded redundant search for {} at depth {}".format(
403+
child, depth + 1
404+
),
405+
stacklevel=2,
406+
)
406407
except TypeError:
407408
pass
408409

409410

410-
def acyclic_depth_first(tree, children=iter, depth=-1, cut_mark=None, traversed=None):
411+
def acyclic_depth_first(tree, children=iter, depth=-1, cut_mark=None, traversed=None, verbose=False):
411412
"""Traverse the nodes of a tree in depth-first order,
412413
discarding eventual cycles within any branch,
413414
adding cut_mark (when specified) if cycles were truncated.
@@ -422,7 +423,7 @@ def acyclic_depth_first(tree, children=iter, depth=-1, cut_mark=None, traversed=
422423
>>> from nltk.util import acyclic_depth_first as acyclic_tree
423424
>>> wn=nltk.corpus.wordnet
424425
>>> from pprint import pprint
425-
>>> pprint(acyclic_tree(wn.synset('dog.n.01'), lambda s:s.hypernyms(),cut_mark='...'))
426+
>>> pprint(acyclic_tree(wn.synset('dog.n.01'), lambda s:sorted(s.hypernyms()),cut_mark='...'))
426427
[Synset('dog.n.01'),
427428
[Synset('canine.n.02'),
428429
[Synset('carnivore.n.01'),
@@ -454,12 +455,13 @@ def acyclic_depth_first(tree, children=iter, depth=-1, cut_mark=None, traversed=
454455
)
455456
]
456457
else:
457-
warnings.warn(
458-
"Discarded redundant search for {} at depth {}".format(
459-
child, depth - 1
460-
),
461-
stacklevel=3,
462-
)
458+
if verbose:
459+
warnings.warn(
460+
"Discarded redundant search for {} at depth {}".format(
461+
child, depth - 1
462+
),
463+
stacklevel=3,
464+
)
463465
if cut_mark:
464466
out_tree += [f"Cycle({child},{depth - 1},{cut_mark})"]
465467
except TypeError:
@@ -470,7 +472,7 @@ def acyclic_depth_first(tree, children=iter, depth=-1, cut_mark=None, traversed=
470472

471473

472474
def acyclic_branches_depth_first(
473-
tree, children=iter, depth=-1, cut_mark=None, traversed=None
475+
tree, children=iter, depth=-1, cut_mark=None, traversed=None, verbose=False
474476
):
475477
"""Traverse the nodes of a tree in depth-first order,
476478
discarding eventual cycles within the same branch,
@@ -488,7 +490,7 @@ def acyclic_branches_depth_first(
488490
>>> from nltk.util import acyclic_branches_depth_first as tree
489491
>>> wn=nltk.corpus.wordnet
490492
>>> from pprint import pprint
491-
>>> pprint(tree(wn.synset('certified.a.01'), lambda s:s.also_sees(), cut_mark='...', depth=4))
493+
>>> pprint(tree(wn.synset('certified.a.01'), lambda s:sorted(s.also_sees()), cut_mark='...', depth=4))
492494
[Synset('certified.a.01'),
493495
[Synset('authorized.a.01'),
494496
[Synset('lawful.a.01'),
@@ -527,12 +529,13 @@ def acyclic_branches_depth_first(
527529
)
528530
]
529531
else:
530-
warnings.warn(
531-
"Discarded redundant search for {} at depth {}".format(
532-
child, depth - 1
533-
),
534-
stacklevel=3,
535-
)
532+
if verbose:
533+
warnings.warn(
534+
"Discarded redundant search for {} at depth {}".format(
535+
child, depth - 1
536+
),
537+
stacklevel=3,
538+
)
536539
if cut_mark:
537540
out_tree += [f"Cycle({child},{depth - 1},{cut_mark})"]
538541
except TypeError:
@@ -563,7 +566,7 @@ def unweighted_minimum_spanning_dict(tree, children=iter):
563566
>>> from nltk.corpus import wordnet as wn
564567
>>> from nltk.util import unweighted_minimum_spanning_dict as umsd
565568
>>> from pprint import pprint
566-
>>> pprint(umsd(wn.synset('bound.a.01'), lambda s:s.also_sees()))
569+
>>> pprint(umsd(wn.synset('bound.a.01'), lambda s:sorted(s.also_sees())))
567570
{Synset('bound.a.01'): [Synset('unfree.a.02')],
568571
Synset('classified.a.02'): [],
569572
Synset('confined.a.02'): [],
@@ -605,7 +608,7 @@ def unweighted_minimum_spanning_tree(tree, children=iter):
605608
>>> from nltk.util import unweighted_minimum_spanning_tree as mst
606609
>>> wn=nltk.corpus.wordnet
607610
>>> from pprint import pprint
608-
>>> pprint(mst(wn.synset('bound.a.01'), lambda s:s.also_sees()))
611+
>>> pprint(mst(wn.synset('bound.a.01'), lambda s:sorted(s.also_sees())))
609612
[Synset('bound.a.01'),
610613
[Synset('unfree.a.02'),
611614
[Synset('confined.a.02')],

0 commit comments

Comments
 (0)