这个地方真的好少有人写到,踩了好久的坑都不知道怎么解决。
首先,在用brat自带的转换序列标注的文件时,运行程序
1、python2 anntoconll.py ../data/data_new/corpoa.txt
报错:
File "anntoconll.py", line 154, in text_to_conll return StringIO('\n'.join(('\t'.join(l) for l in lines))) TypeError: initial_value must be unicode or None, not str
问了chatGPT,修改:
在文件开头加了
try:
from StringIO import StringIO # Python 2
except ImportError:
from io import StringIO # Python 3
又将报错的代码改为:return StringIO('\n'.join(('\t'.join(l)+'\n' for l in lines)))
2、可以运行了,但是发现生成的文件中,中文是乱码的。
问了chatGPT,他想让我将乱码的文件进行修改,没看懂怎么操作的。
3、然后我将系统中的示例文件也进行了同样的操作,发现英文的文件序列标注后并不会报错。于是再次问了chatGPT,他先提出的解决方案是:
python2 anntoconll.py -e utf-8 ../data/data_new/corpoa.txt
但是anntoconll.py并没有-e的选项,也没有可以添加操作的地方。
4、又提出修改代码:
return io.BytesIO('\n'.join(['\t'.join(l) for l in lines if l]).encode('utf-8'))
但是报错:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 6: ordinal not in range(128)
5、再次修改代码:
with open(filename, 'r', encoding='utf-8') as f:
再次报错:
TypeError: 'encoding' is an invalid keyword argument for this function
这个错误是因为使用的是 Python 2.x 版本,而在 Python 2.x 版本中,open()
函数不支持 encoding
参数来指定文件编码。
6、再次修改:
import codecs
with codecs.open(fn, 'r', encoding='utf-8') as f:
# 处理文件内容
错误解决,可以正常运行了。
最后的文件长这样:
我真的无语凝噎