嗨,我正在使用NLTK,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 获取标记的文本?
nps_chat
API 不提供同时查看 POS 标记和发布元数据的简单方法,但只需一行即可导航 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()
输出:
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 标签。