标记nltk.corpus.nps_聊天.xml位置

2024-03-28 16:22:02 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我在和NLTK,nps,聊天语料库合作。在

我知道我可以访问nps聊天语料库,如下所示

posts = nltk.corpus.nps_chat.xml_posts()

我准备了标签姓名列表,如下所示

^{pr2}$

我就这样被跟踪了

>>> 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_聊天.xml_帖子?在


Tags: namechatcorpus标签xmlsystemstatementposts
1条回答
网友
1楼 · 发布于 2024-03-28 16:22:02

nps_chatAPI并没有提供一种同时查看POS标记和post元数据的简单方法,但是导航xml_posts()返回的XML元素并获取这些信息只需要一行代码。下面是一个小演示:

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()

输出:

^{pr2}$

每个xml_post都有一个唯一的元素terminals,包含一系列元素t,每个元素都有word和{}属性。完整路径:Session/Posts/Post/terminals/t。因此,terminals元素是p[0],我们迭代它的子元素以得到它们的单词和POS标记。在

相关问题 更多 >