NLTK - WordNet:长词列表

NLTK - WordNet: list of long words

我想在 WordNet 中查找长度至少为 18 个字符的单词。我尝试了以下代码:

from nltk.corpus import wordnet as wn
sorted(w for w in wn.synset().name() if len(w)>18)

我收到以下错误消息:

sorted(w for w in wn.synset().name() if len(w)>18)

TypeError: synset() missing 1 required positional argument: 'name'

我正在使用 Python 3.4.3.

如何修复我的代码?

synset 函数需要一个参数。

sorted(w for w in wn.synset('WORD').name() if len(w)>18)

http://www.nltk.org/howto/wordnet.html

使用wn.all_lemma_names() 获取所有引理的列表。我相信这就是您将从 Wordnet 中获得的所有单词,因此无需迭代同义词集(但如果您愿意,可以为每个引理调用同义词集)。

您可能希望按长度对点击进行排序:

longwords = [ n for n in wn.all_lemma_names() if len(n) > 18 ]
longwords.sort(key=len, reverse=True)

在回答之前,你需要知道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.01absorbefacient.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)