我需要输入一个单词的输入文本文件。然后我需要使用wordnet找到lemma_names,定义和单词的synset的例子。我已经阅读了这本书:“使用NLTK 2.0 Cookbook进行Python文本处理”以及“使用NLTK进行自然语言处理”来帮助我实现这一目标。虽然我已经理解如何使用终端完成这项工作,但我无法使用文本编辑器执行相同操作。
例如,如果输入文本中包含单词“flabbergasted”,则输出必须采用这种方式:
大吃一惊 (动词)flabbergast,boggle,bowl over - 惊奇地克服; “这令人难以置信!” (形容词)目瞪口呆,笨拙,大吃一惊,恍恍惚惚,雷鸣般的,笨拙的,笨拙的 - 好像被惊讶和惊讶地打了个傻瓜; “她拒绝看到这起事故,一群警察傻眼了”; “这些惊讶的市议员说不出话来”; “被他晋升的消息震惊了”
直接从WordNet获取同义词,定义和例句!
我有以下代码:
from __future__ import division
import nltk
from nltk.corpus import wordnet as wn
tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
fp = open("inpsyn.txt")
data = fp.read()
#to tokenize input text into sentences
print '\n-----\n'.join(tokenizer.tokenize(data))# splits text into sentences
#to tokenize the tokenized sentences into words
tokens = nltk.wordpunct_tokenize(data)
text = nltk.Text(tokens)
words = [w.lower() for w in text]
print words #to print the tokens
for a in words:
print a
syns = wn.synsets(a)
print "synsets:", syns
for s in syns:
for l in s.lemmas:
print l.name
print s.definition
print s.examples
我得到以下输出:
flabbergasted
['flabbergasted']
flabbergasted
synsets: [Synset('flabbergast.v.01'), Synset('dumbfounded.s.01')]
flabbergast
boggle
bowl_over
overcome with amazement
['This boggles the mind!']
dumbfounded
dumfounded
flabbergasted
stupefied
thunderstruck
dumbstruck
dumbstricken
as if struck dumb with astonishment and surprise
['a circle of policement stood dumbfounded by her denial of having seen the accident', 'the flabbergasted aldermen were speechless', 'was thunderstruck by the news of his promotion']
有没有办法检索词性以及引理名称组?
答案 0 :(得分:22)
def synset(word):
wn.synsets(word)
不会返回任何内容,因此默认情况下会获得None
你应该写
def synset(word):
return wn.synsets(word)
提取引理名称:
from nltk.corpus import wordnet
syns = wordnet.synsets('car')
syns[0].lemmas[0].name
>>> 'car'
[s.lemmas[0].name for s in syns]
>>> ['car', 'car', 'car', 'car', 'cable_car']
[l.name for s in syns for l in s.lemmas]
>>>['car', 'auto', 'automobile', 'machine', 'motorcar', 'car', 'railcar', 'railway_car', 'railroad_car', 'car', 'gondola', 'car', 'elevator_car', 'cable_car', 'car']
答案 1 :(得分:5)
这里我创建了一个可以轻松使用(导入)的模块,并且传递给它的字符串将返回字符串的所有引理词。
模块:
#!/usr/bin/python2.7
''' pass a string to this funciton ( eg 'car') and it will give you a list of
words which is related to cat, called lemma of CAT. '''
from nltk.corpus import wordnet as wn
import sys
#print all the synset element of an element
def lemmalist(str):
syn_set = []
for synset in wn.synsets(str):
for item in synset.lemma_names:
syn_set.append(item)
return syn_set
用法:
注意:模块名称是lemma.py因此“来自引理导入lemmalist”
>>> from lemma import lemmalist
>>> lemmalist('car')
['car', 'auto', 'automobile', 'machine', 'motorcar', 'car', 'railcar', 'railway_car', 'railroad_car', 'car', 'gondola', 'car', 'elevator_car', 'cable_car', 'car']
干杯!
答案 2 :(得分:1)
synonyms = []
for syn in wordnet.synsets("car"):
for l in syn.lemmas():
synonyms.append(l.name())
print synonyms
答案 3 :(得分:0)
在NLTK 3.0
中,lemma_names
已从属性更改为方法。因此,如果您收到错误消息:
TypeError: 'method' object is not iterable
您可以使用以下方法修复它:
>>> from nltk.corpus import wordnet as wn
>>> [item for sysnet in wn.synsets('car') for item in sysnet.lemma_names()]
这将输出:
>>> [
'car', 'auto', 'automobile', 'machine', 'motorcar', 'car',
'railcar', 'railway_car', 'railroad_car', 'car', 'gondola',
'car', 'elevator_car', 'cable_car', 'car'
]