我需要用一个单词输入一个输入文本文件.然后我需要使用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: …Run Code Online (Sandbox Code Playgroud) 我需要使用NLTK从英语中找出一个单词中的音节数.这是我到目前为止的代码:
import curses
from curses.ascii import isdigit
import nltk
from nltk.corpus import cmudict
d = cmudict.dict()
def nsyl(word):
return [len(list(y for y in x if isdigit(y[-1]))) for x in d[word.lower()]]
>>> nsyl(arithmetic)
Run Code Online (Sandbox Code Playgroud)
在函数调用之后,我得到一个名称错误,说明没有定义算术.有人可以帮我弄清楚代码中的错误吗?
我编写了以下代码来标记来自文件samp.txt的输入段落.有人可以帮我找出并打印文件中的句子,单词和字符的数量吗?我在python中使用了NLTK.
>>>import nltk.data
>>>import nltk.tokenize
>>>f=open('samp.txt')
>>>raw=f.read()
>>>tokenized_sentences=nltk.sent_tokenize(raw)
>>>for each_sentence in tokenized_sentences:
... words=nltk.tokenize.word_tokenize(each_sentence)
... print each_sentence #prints tokenized sentences from samp.txt
>>>tokenized_words=nltk.word_tokenize(raw)
>>>for each_word in tokenized_words:
... words=nltk.tokenize.word_tokenize(each_word)
... print each_words #prints tokenized words from samp.txt
Run Code Online (Sandbox Code Playgroud)