我想在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()缺少1个必需的位置参数:'name'
我使用的是Python 3.4.3。
如何修复我的代码?
答案 0 :(得分:3)
使用wn.all_lemma_names()
获取所有引理列表。我相信这是你将从Wordnet中得到的所有单词,因此不需要迭代同义词(但如果你是如此倾向,你可以为每个引理调用同义词)。
您可能希望按长度对点击次数进行排序:
longwords = [ n for n in wn.all_lemma_names() if len(n) > 18 ]
longwords.sort(key=len, reverse=True)
答案 1 :(得分:1)
在回答之前,您需要了解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指数的引理名称列表。此外,当您检查.s.01
时,您将在标记集的索引名称中包含POS标记和索引ID(即absorbefacient.s.01
:len(ss.name()) > 18
。
所以你需要的是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)
答案 2 :(得分:0)
synset
函数需要参数。
sorted(w for w in wn.synset('WORD').name() if len(w)>18)