我正在尝试为每行中的每个单词做pos标记(每行包含几个句子)。
我有这段代码:
import nltk import pos_tag
import nltk.tokenize import word_tokenize
f = open('C:\Users\test_data.txt')
data = f.readlines()
#Parse the text file for NER with POS Tagging
for line in data:
tokens = nltk.word_tokenize(line)
tagged = nltk.pos_tag(tokens)
entities = nltk.chunk.ne_chunk(tagged)
print entities
f.close()
但代码为每一行提供了一个标记,输出如下所示:
[('公寓是全新的,清洁的原始。','NNP'),(''“山上很棒的小地方。”','NNP'),('非常舒适的地方靠近fatima luas停止。我喜欢这个地方。\ njose和vadym非常热情并且非常好地对待我。\ n \ n将会再次留下来。','NNP'),('非常有帮助和沟通主持人。优越的地理位置,与公众联系紧密对于一对夫妇而言,房间有点小,而且缺乏橱柜非常感觉。\ n \ n其他非常干净且维护得很好。','NNP'),('一切都如描述的那样。它是美丽的。' ,'NNP')]
我的代码有'tokenizer',我不知道我的代码有什么问题。我需要每个单词的pos标记而不是每行。但是仍然应该用括号或类似的东西对每一行进行分块(或区分)。
答案 0 :(得分:1)
(从我计算机上运行的纯文本粘贴)
运行代码(注意简单的import语句):
#!/usr/bin/env python3
# encoding: utf-8
import nltk
f = open('/home/matthieu/Téléchargements/testtext.txt')
data = f.readlines()
for line in data:
tokens = nltk.word_tokenize(line)
tagged = nltk.pos_tag(tokens)
entities = nltk.chunk.ne_chunk(tagged)
print(entities)
f.close()
在以下unicode原始文本文件(3行)中:
(this is a first example.)(Another sentence in another parentheses.)
(onlyone in that line)
this is a second one wihtout parenthesis. (Another sentence in another parentheses.)
我得到以下结果:
(S
(/(
this/DT
is/VBZ
a/DT
first/JJ
example/NN
./.
)/)
(/(
Another/DT
sentence/NN
in/IN
another/DT
parentheses/NNS
./.
)/))
(S (/( onlyone/NN in/IN that/DT line/NN )/))
(S
this/DT
...
如您所见,没有特别的问题。 你正确解析你的csv数据?在你的情况下csv有用吗?你尝试使用一个简单的文本文件吗?