前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pytreebank︱情感分析可视化——情感结构树

pytreebank︱情感分析可视化——情感结构树

作者头像
悟乙己
发布2018-01-02 16:57:17
1.3K0
发布2018-01-02 16:57:17
举报
文章被收录于专栏:素质云笔记
这里写图片描述
这里写图片描述

效果如上,一句话用树状来表达,具体案例可查询stanford Sentiment Analysis网站

如何用py来画,主要参考自pytreebank-github

1 直接上案例,官网案例数据下载:

http://nlp.stanford.edu/sentiment/trainDevTestTrees_PTB.zip

代码语言:javascript
复制
import pytreebank
dataset = pytreebank.load_sst("/path/to/sentiment/")
pytreebank.LabeledTree.inject_visualization_javascript()
# select and example to visualize
example = dataset["train"][0]
# display it in the page
example.display()

load_sst时候需要写入官网下载数据的链接具体在哪。

这里写图片描述
这里写图片描述

2 那么案例里面的数据长什么样子:

代码语言:javascript
复制
for label, sentence in example.to_labeled_lines():
    print("%s has sentiment label %s" % (
        sentence,
        ["very negative", "negative", "neutral", "positive", "very positive"][label]))

具体如下:

代码语言:javascript
复制
The Rock is destined to be the 21st Century 's new `` Conan '' and that he 's going to make a splash even greater than Arnold Schwarzenegger , Jean-Claud Van Damme or Steven Segal . has sentiment label positive
The Rock has sentiment label neutral
The has sentiment label neutral
Rock has sentiment label neutral
is destined to be the 21st Century 's new `` Conan '' and that he 's going to make a splash even greater than Arnold Schwarzenegger , Jean-Claud Van Damme or Steven Segal . has sentiment label very positive
is destined to be the 21st Century 's new `` Conan '' and that he 's going to make a splash even greater than Arnold Schwarzenegger , Jean-Claud Van Damme or Steven Segal has sentiment label positive
is has sentiment label neutral
destined to be the 21st Century 's new `` Conan '' and that he 's going to make a splash even greater than Arnold Schwarzenegger , Jean-Claud Van Damme or Steven Segal has sentiment label very positive
destined has sentiment label neutral
to be the 21st Century 's new `` Conan '' and that he 's going to make a splash even greater than Arnold Schwarzenegger , Jean-Claud Van Damme or Steven Segal has sentiment label neutral
to be the 21st Century 's new `` Conan '' and has sentiment label neutral
to be the 21st Century 's new `` Conan '' has sentiment label neutral
to be the 21st Century 's new `` Conan has sentiment label neutral
to has sentiment label neutral

3 那么自己咋画呢:

代码语言:javascript
复制
pytreebank.LabeledTree.inject_visualization_javascript()

pytreebank.create_tree_from_string('(4 (0 你) (3 (2 是) (3 (3 (3 谁) (2 的)) (2 谁))))').display()

4代表总共有四个节点, (0 你)代表,‘你’这个字的情感点; 编号的意义为: 0–>”very negative”, 1–> “negative” 2–>, “neutral” 3–>, “positive” 4–>, “very positive”

这里写图片描述
这里写图片描述

4 其他

代码语言:javascript
复制
pytreebank.create_tree_from_string('(4 (0 你) (3 (2 是) (3 (3 (3 谁) (2 的)) (2 谁))))').to_dict()

把结果导出成dict形式

代码语言:javascript
复制
pytreebank.create_tree_from_string('(4 (0 你) (3 (2 是) (3 (3 (3 谁) (2 的)) (2 谁))))').to_json()

把结果导出成json形式

代码语言:javascript
复制
pytreebank.create_tree_from_string('(4 (0 你) (3 (2 是) (3 (3 (3 谁) (2 的)) (2 谁))))').to_lines()

把结果导出成一列文字形式

代码语言:javascript
复制
pytreebank.create_tree_from_string('(4 (0 你) (3 (2 是) (3 (3 (3 谁) (2 的)) (2 谁))))').to_lines()

把结果导出成to_labeled_lines形式

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 直接上案例,官网案例数据下载:
  • 2 那么案例里面的数据长什么样子:
  • 3 那么自己咋画呢:
  • 4 其他
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档