我需要输入一个单词的输入文本文件.然后,我需要使用wordnet来查找词汇的同义词的定义和例子.我经历了这本书:“使用NLTK 2.0 Cookbook的
Python文本处理”以及“使用NLTK的自然语言处理”来帮助我.虽然我已经明白如何使用终端来完成这个工作,但我也不能用文本编辑器来做同样的事情.
例如,如果输入文本的字词“flabbergasted”,输出需要是这样的:
吓坏的
(动词)flabbergast,boggle,碗过度 – 惊奇地克服; “这困扰着头脑!”
(形容词)茫然,沮丧,颤抖,愚蠢,雷霆,浑浊,哑巴 – 仿佛惊讶和惊讶的笨蛋; “一个警察局因为否认这次事故而umb;一声”; “; asted的aldermen是无语的”; “被他的宣传消息吓倒了”
synSets,定义和例句从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']
有没有一种方法来与词组名称一起检索词性?