在回答之前,你需要知道NLTK中wordnet接口是如何工作的,见http://www.nltk.org/howto/wordnet.html
Wordnet 由可以表示的概念索引,不同的词包含有关的语义信息。 NLTK 中的 Wordnet 接口让你搜索一个词可以表示的概念,例如:
>>> from nltk.corpus import wordnet as wn
>>> wn.synsets('dog')
[Synset('dog.n.01'), Synset('frump.n.01'), Synset('dog.n.03'), Synset('cad.n.01'), Synset('frank.n.02'), Synset('pawl.n.01'), Synset('andiron.n.01'), Synset('chase.v.01')]
>>> for ss in wn.synsets('dog'):
... print ss, ss.definition()
...
Synset('dog.n.01') a member of the genus Canis (probably descended from the common wolf) that has been domesticated by man since prehistoric times; occurs in many breeds
Synset('frump.n.01') a dull unattractive unpleasant girl or woman
Synset('dog.n.03') informal term for a man
Synset('cad.n.01') someone who is morally reprehensible
Synset('frank.n.02') a smooth-textured sausage of minced beef or pork usually smoked; often served on a bread roll
Synset('pawl.n.01') a hinged catch that fits into a notch of a ratchet to move a wheel forward or prevent it from moving backward
Synset('andiron.n.01') metal supports for logs in a fireplace
Synset('chase.v.01') go after with the intent to catch
要访问 wordnet 中的所有同义词集:
wn.all_synsets()
对于每个同义词集,您可以查找有关同义词集的不同功能,例如
>>> ss = wn.synsets('dog')[0] # First synset for the word 'dog'
>>> ss.definition()
u'a member of the genus Canis (probably descended from the common wolf) that has been domesticated by man since prehistoric times; occurs in many breeds'
>>> ss.hypernyms()
[Synset('canine.n.02'), Synset('domestic_animal.n.01')]
>>> ss.hyponyms()
[Synset('basenji.n.01'), Synset('corgi.n.01'), Synset('cur.n.01'), Synset('dalmatian.n.02'), Synset('great_pyrenees.n.01'), Synset('griffon.n.02'), Synset('hunting_dog.n.01'), Synset('lapdog.n.01'), Synset('leonberg.n.01'), Synset('mexican_hairless.n.01'), Synset('newfoundland.n.01'), Synset('pooch.n.01'), Synset('poodle.n.01'), Synset('pug.n.01'), Synset('puppy.n.01'), Synset('spitz.n.01'), Synset('toy_dog.n.01'), Synset('working_dog.n.01')]
>>> ss.name()
u'dog.n.01'
>>> ss.lemma_names() # Other words that can represent this concept.
[u'dog', u'domestic_dog', u'Canis_familiaris']
所以你可以用一个衬里来做,它不是那么可读:
sorted(ss.name() for ss in wn.all_synsets() if len(ss.name())>18)
但请注意,这只会为您提供作为 Synsets 索引的引理名称列表。此外,当您检查 len(ss.name()) > 18 时,您还包括 POS 标签和索引 ID(即同义词集的索引名称中的 .s.01:absorbefacient.s.01)。
所以你需要的是lemma_names() 而不是name()。
>>> from itertools import chain
>>> sorted(lemma for lemma in chain(*(ss.lemma_names() for ss in wn.all_synsets())) if len(lemma) > 18)
或者,您可以在收集引理时检查长度,然后对它们进行链接和排序:
>>> sorted(chain(*([lemma for lemma in ss.lemma_names() if len(lemma)>18] for ss in wn.all_synsets())))
注意:通过遍历同义词集并获得 lemma_names(),您将获得重复项以及 lemma_names() 首字母大写与非首字母名称。
当然,你不需要循环所有的麻烦,因为有一个内置函数
>>> sorted(lemma for lemma in wn.all_lemma_names() if len(lemma) > 18)