使用NLTK库获取短期数据

时间:2016-09-19 12:39:08

标签: python nltk

我们正在使用NLTK库。 我们正在尝试使用不合适的英语句子的句子。 如果句子是正确的'名词动词名词/代词',它是完美的分块,但如果句子只有'动词名词'那么分块没有正确发生。

下面显示的一些示例工作正常:我正在为我的同事提高重置密码的门票

不起作用的示例:重置密码(两行都在文件“test.txt”中)

import nltk
from nltk.corpus import webtext
from nltk.tokenize import PunktSentenceTokenizer

train_text = webtext.raw("train.txt")
sample_text = webtext.raw("test.txt")
custom_sent_tokenizer = PunktSentenceTokenizer(train_text)

tokenized = custom_sent_tokenizer.tokenize(sample_text)

def process_content():
    try:
        for i in tokenized:
           words = nltk.word_tokenize(i)
           tagged = nltk.pos_tag(words)
           chunkGram = r"""Chunk: {<RB.?>*<VB.?>*<NNP>+<NN>?}"""
           chunkParser = nltk.RegexpParser(chunkGram)
           chunked = chunkParser.parse(tagged)
           chunked.draw()     

   except Exception as e:
       print(str(e))

process_content()

我们正在从文本文件中分块数据,而不是来自某些正在运行的文本。

0 个答案:

没有答案
相关问题