我需要输入一个单词的文本文件。然后使用WordNet找到该单词的lemma_names、definition和synset的示例。我已经阅读了《Python Text Processing with NLTK 2.0 Cookbook》和《Natural Language Processing using NLTK》这两本书来帮助我完成这个任务。虽然我理解了如何在终端中完成这个任务,但我无法在文本编辑器中完成。
例如,如果输入的文本中有单词"flabbergasted",那么输出应该是这样的:
flabbergasted (verb) flabbergast, boggle, bowl over - 充满惊奇;"This boggles the mind!" (adjective) dumbfounded , dumfounded , flabbergasted , stupefied , thunderstruck , dumbstruck , dumbstricken - 像被惊讶和惊喜击中而哑口无言; "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"
同义词集、定义和示例句子直接从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']
有没有一种方法可以获取词性以及词元组成的部分?