gpt4 book ai didi

python - NLTK(python)中如何处理中文?

转载 作者:太空宇宙 更新时间:2023-11-04 00:42:42 25 4
gpt4 key购买 nike

输入文件的代码为“utf8 without BOM”,每一行如下:

( IP ( NP ( NP ( NR 上海 ) ( NR 浦东 ) ) ( NP ( NN 开发 ) ( NP ( CC 与 ) ( NP ( NN 法制 ) ( NN 建设 ) ) ) ) ) ( VP ( VV 同步 ) ) )

我想使用 NLTK 通过使用

从这个字符串构建一棵树
nltk.tree.Tree.fromstring

我的输出是“\u4e0a\u6d77”的形式。

如何将输出转换为 utf8?

我不明白为什么a的输出是utf8的形式?

# -*- coding: utf-8 -*-
import nltk
tparse = nltk.tree.Tree.fromstring
import sys
reload(sys)
sys.setdefaultencoding('utf8')
class cal_prob:
def __init__(self):
pass
def input_dataset(self, path="CTB-auto-pos/"):
trainfile = open(path+"train.txt", "r+")
datas = trainfile.read().split("\n")
for data in datas:
data = unicode(data) # change them to unicode
print data
tree = tparse(data)
print tree
print unicode(str(tree)).decode("utf8")
print unicode(str(tree)).encode("utf8")
break
#
a = u"(IP \n (NP (NP (NR \u4e0a\u6d77) (NR \u6d66\u4e1c)) (NP (NN \u5f00\u53d1) (NP (CC \u4e0e) (NP (NN \u6cd5\u5236) (NN \u5efa\u8bbe))))) (VP (VV \u540c\u6b65)))"
print a
print a.decode("utf8")
trainfile.close()
a = cal_prob()
a.input_dataset()

最佳答案

下面是一个正确打开编码文件的例子。不需要 reload(sys) 技巧(参见 https://anonbadger.wordpress.com/2015/06/16/why-sys-setdefaultencoding-will-break-code/ )或其他编码/解码。

tree.pformat() 按您的意愿显示树:

import nltk
import io

with io.open('train.txt', encoding='utf8') as trainfile:
for line in trainfile:
print tree
print
print tree.pformat()

输出:

(IP
(NP
(NP (NR \u4e0a\u6d77) (NR \u6d66\u4e1c))
(NP (NN \u5f00\u53d1) (NP (CC \u4e0e) (NP (NN \u6cd5\u5236) (NN \u5efa\u8bbe)))))
(VP (VV \u540c\u6b65)))

(IP
(NP
(NP (NR 上海) (NR 浦东))
(NP (NN 开发) (NP (CC 与) (NP (NN 法制) (NN 建设)))))
(VP (VV 同步)))

关于python - NLTK(python)中如何处理中文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41199907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com