我正在尝试将文本文件读入Python,然后执行句子分段器,单词标记器和词性标记器。
这是我的代码:
file=open('C:/temp/1.txt','r')
sentences = nltk.sent_tokenize(file)
sentences = [nltk.word_tokenize(sent) for sent in sentences]
sentences = [nltk.pos_tag(sent) for sent in sentences]
当我尝试第二条命令时,它显示错误:
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
sentences = nltk.sent_tokenize(file)
File "D:\Python\lib\site-packages\nltk\tokenize\__init__.py", line 76, in sent_tokenize
return tokenizer.tokenize(text)
File "D:\Python\lib\site-packages\nltk\tokenize\punkt.py", line 1217, in tokenize
return list(self.sentences_from_text(text, realign_boundaries))
File "D:\Python\lib\site-packages\nltk\tokenize\punkt.py", line 1262, in sentences_from_text
sents = [text[sl] for sl in self._slices_from_text(text)]
File "D:\Python\lib\site-packages\nltk\tokenize\punkt.py", line 1269, in _slices_from_text
for match in self._lang_vars.period_context_re().finditer(text):
TypeError: expected string or buffer
另一种尝试:
当我尝试一个句子时,例如“一条黄色的狗在猫身上咆哮”
前三个命令有效,但最后一行却出现此错误:(我想知道我是否没有完全下载软件包吗?)
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
sentences = [nltk.pos_tag(sent) for sent in sentences]
File "D:\Python\lib\site-packages\nltk\tag\__init__.py", line 99, in pos_tag
tagger = load(_POS_TAGGER)
File "D:\Python\lib\site-packages\nltk\data.py", line 605, in load
resource_val = pickle.load(_open(resource_url))
ImportError: No module named numpy.core.multiarray
最佳答案
嗯...您确定错误在第二行吗?
您似乎正在使用单引号和逗号字符,而不是标准ASCII '
和,
字符:
file=open(‘C:/temp/1.txt’,‘r’) # your version (WRONG)
file=open('C:/temp/1.txt', 'r') # right
Python甚至不能编译它。确实,当我尝试使用它时,由于语法错误而无法使用。
更新:您发布了具有正确语法的更正版本。追溯的错误消息非常简单:您正在调用的函数似乎期望将一大块文本作为其参数,而不是文件对象。尽管我对NLTK并不了解,但在Google confirms this上花了五秒钟。
尝试这样的事情:
file = open('C:/temp/1.txt','r')
text = file.read() # read the contents of the text file into a variable
result1 = nltk.sent_tokenize(text)
result2 = [nltk.word_tokenize(sent) for sent in result1]
result3 = [nltk.pos_tag(sent) for sent in result2]
更新:我将
sentences
重命名为result
1/2/3,因为由于反复覆盖同一变量,导致代码实际执行混乱。这不会影响语义,只是说明第二行实际上对最后的result3
有影响。