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

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

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

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

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

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 那么案例里面的数据长什么样子:

for label, sentence in example.to_labeled_lines():
    print("%s has sentiment label %s" % (
        sentence,
        ["very negative", "negative", "neutral", "positive", "very positive"][label]))

具体如下:

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 那么自己咋画呢:

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 其他

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

把结果导出成dict形式

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

把结果导出成json形式

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

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

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

把结果导出成to_labeled_lines形式

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏张善友的专栏

Mix 10 上的asp.net mvc 2的相关Session

Beyond File | New Company: From Cheesy Sample to Social Platform Scott Hansel...

2767
来自专栏芋道源码1024

熔断器 Hystrix 源码解析 —— 断路器 HystrixCircuitBreaker

本文主要基于 Hystrix 1.5.X 版本 1. 概述 2. HystrixCircuitBreaker 3. HystrixCircuitBreaker....

5717
来自专栏一个会写诗的程序员的博客

Spring Reactor 项目核心库Reactor Core

Non-Blocking Reactive Streams Foundation for the JVM both implementing a Reactiv...

2732
来自专栏魂祭心

原 canvas绘制clock

5004
来自专栏hbbliyong

WPF Trigger for IsSelected in a DataTemplate for ListBox items

<DataTemplate DataType="{x:Type vm:HeaderSlugViewModel}"> <vw:HeaderSlug...

4214
来自专栏张善友的专栏

Silverlight + Model-View-ViewModel (MVVM)

     早在2005年,John Gossman写了一篇关于Model-View-ViewModel模式的博文,这种模式被他所在的微软的项目组用来创建Expr...

3268
来自专栏张善友的专栏

Miguel de Icaza 细说 Mix 07大会上的Silverlight和DLR

Mono之父Miguel de Icaza 详细报道微软Mix 07大会上的Silverlight和DLR ,上面还谈到了Mono and Silverligh...

2997
来自专栏转载gongluck的CSDN博客

cocos2dx 打灰机

#include "GamePlane.h" #include "PlaneSprite.h" #include "BulletNode.h" #include...

6906
来自专栏杨龙飞前端

scrollto 到指定位置

2894
来自专栏张善友的专栏

LINQ via C# 系列文章

LINQ via C# Recently I am giving a series of talk on LINQ. the name “LINQ via C...

2965

扫码关注云+社区