Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 标记了nltk.corpus.nps_chat.xml_post_Python_Tags_Nltk - Fatal编程技术网

Python 标记了nltk.corpus.nps_chat.xml_post

Python 标记了nltk.corpus.nps_chat.xml_post,python,tags,nltk,Python,Tags,Nltk,嗨,我在NLTK,nps_聊天语料库工作 我知道我可以访问nps聊天语料库,如下所示 posts = nltk.corpus.nps_chat.xml_posts() Labeled_names=[(post.text,post.get('class')) for post in posts] 我准备了一份名单,如下所示 posts = nltk.corpus.nps_chat.xml_posts() Labeled_names=[(post.text,post.get('class'))

嗨,我在NLTK,nps_聊天语料库工作

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

posts = nltk.corpus.nps_chat.xml_posts()
Labeled_names=[(post.text,post.get('class')) for post in posts]
我准备了一份名单,如下所示

posts = nltk.corpus.nps_chat.xml_posts()
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\u chat.xml\u post获取标记文本

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

每个
xml\u post
都有一个唯一的元素
terminals
,包含一系列元素
t
,每个元素都有
word
pos
属性。完整路径:
Session/Posts/Post/terminals/t
。因此,
terminals
元素是
p[0]
,我们迭代它的子元素以获取它们的单词和词性标签。

您是指词性标签文本吗?如果是,请使用
nltk.pos_tag()
标记文本。否。NPS聊天语料库已标记。我需要做的是,当我使用post.textOnly
nltk.corpus.nps\u chat.tagged\u posts()获取文本时,有没有一种方法可以获取这些标记。找到了。非常感谢。这就是我想要的know@DYZ事实上,这是可行的,甚至不是很难。(答案如下。)