以下代码是从这位博主点击打开链接那借鉴过来的用来实现基本的命名实体识别
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf8') #让cmd识别正确的编码
import nltk
newfile = open('news.txt')
text = newfile.read() #读取文件
tokens = nltk.word_tokenize(text) #分词
tagged = nltk.pos_tag(tokens) #词性标注
entities = nltk.chunk.ne_chunk(tagged) #命名实体识别
a1=str(entities) #将文件转换为字符串
file_object = open('out.txt', 'w')
file_object.write(a1) #写入到文件中
file_object.close( )
print entities
但是,在运行时出现了以下错误
UnicodeDecodeError: 'utf8' codec can't decode byte 0xef in position 0: unexpected end of data
现给出以下解决办法
#!