Permalink
Browse files

Merge pull request #465 from pquentin/master

Sort lowest common hypernyms by Synset name
  • Loading branch information...
stevenbird committed Sep 19, 2013
2 parents 1d4adda + e10de6a commit d274e272caf87546e81b29ef7359c0ec5961a455
Showing with 8 additions and 7 deletions.
  1. +4 −3 nltk/corpus/reader/wordnet.py
  2. +4 −4 nltk/test/wordnet_lch.doctest
@@ -12,7 +12,7 @@
import math
import re
from itertools import islice, chain
from operator import itemgetter
from operator import itemgetter, attrgetter
from collections import defaultdict
from nltk.corpus.reader import CorpusReader
@@ -508,10 +508,11 @@ def lowest_common_hypernyms(self, other, simulate_root=False, use_min_depth=Fals
try:
if use_min_depth:
max_depth = max(s.min_depth() for s in synsets)
return [s for s in synsets if s.min_depth() == max_depth]
unsorted_lch = [s for s in synsets if s.min_depth() == max_depth]
else:
max_depth = max(s.max_depth() for s in synsets)
return [s for s in synsets if s.max_depth() == max_depth]
unsorted_lch = [s for s in synsets if s.max_depth() == max_depth]
return sorted(unsorted_lch, key=attrgetter('name'))
except ValueError:
return []
@@ -19,7 +19,7 @@ This method generally returns a single result, but in some cases, more than one
valid LCH is possible:
>>> wn.synset('body.n.09').lowest_common_hypernyms(wn.synset('sidereal_day.n.01'))
[Synset('measure.n.02'), Synset('attribute.n.02')]
[Synset('attribute.n.02'), Synset('measure.n.02')]
In some cases, lowest_common_hypernyms() can return one of the synsets which was
passed to it as an argument:
@@ -43,11 +43,11 @@ behavior:
>>> wn.synset('woman.n.01').lowest_common_hypernyms(wn.synset('girlfriend.n.02'))
[Synset('woman.n.01')]
>>> wn.synset('woman.n.01').lowest_common_hypernyms(wn.synset('girlfriend.n.02'), use_min_depth=True)
[Synset('woman.n.01'), Synset('organism.n.01')]
[Synset('organism.n.01'), Synset('woman.n.01')]
In the general case, however, they tend to return the same results:
>>> wn.synset('body.n.09').lowest_common_hypernyms(wn.synset('sidereal_day.n.01'))
[Synset('measure.n.02'), Synset('attribute.n.02')]
[Synset('attribute.n.02'), Synset('measure.n.02')]
>>> wn.synset('body.n.09').lowest_common_hypernyms(wn.synset('sidereal_day.n.01'), use_min_depth=True)
[Synset('measure.n.02'), Synset('attribute.n.02')]
[Synset('attribute.n.02'), Synset('measure.n.02')]

0 comments on commit d274e27

Please sign in to comment.