大家好,我是nps_chat语料库的工作人员。
我知道我可以像下面这样访问nps聊天语料库
posts = nltk.corpus.nps_chat.xml_posts()我准备了Labeled_names列表,如下所示
Labeled_names=[(post.text,post.get('class')) for post in posts]我的粉丝是这样的,
>>> Labeled_names[:10]
[('now im left with this gay name', 'Statement'), (':P', 'Emotion'), ('PART', 'System'), ('hey everyone ', 'Greet'), ('ah well', 'Statement'), ('NICK :10-19-20sUser7', 'System'), ('10-19-20sUser7 is a gay name.', 'Accept'), ('.ACTION gives 10-19-20sUser121 a golf clap.', 'System'), (':)', 'Emotion'), ('JOIN', 'System')]我需要知道的是,除了文本之外,有没有一种方法可以使用nltk.corpus.nps_chat.xml_post获取标记文本?
发布于 2017-09-19 21:04:29
XML并没有提供一种同时查看POS元数据和post元数据的简单方法,但是导航xml_posts()返回的nps_chat元素并获取这些信息只是一个简单的方法。下面是一个小演示:
from nltk.corpus import nps_chat
for p in nps_chat.xml_posts()[3:5]:
print(p.get("class"), p.get("user"))
print(p.text)
tagged_words = list((t.get("word"), t.get("pos")) for t in p[0]) # <-- here it is
print(tagged_words)
print()输出:
Greet 10-19-20sUser59
hey everyone
[('hey', 'UH'), ('everyone', 'NN')]
Statement 10-19-20sUser115
ah well
[('ah', 'UH'), ('well', 'UH')]每个xml_post都有一个惟一的元素terminals,其中包含一系列元素t,每个元素都有word和pos属性。完整路径:Session/Posts/Post/terminals/t。因此,terminals元素是p[0],我们遍历它的子元素以获得它们的单词和POS子标签。
https://stackoverflow.com/questions/41529750
复制相似问题