使用SceneGraphParser解析语句

简介

Scene Graph Parser受到Stanford Scene Graph Parser的启发创建。

Stanford Scene Graph Parser,斯坦福场景图解析器,场景图是一种基于图的图像内容语义表示。它们对图像中的对象、它们的属性和对象之间的关系进行编码。

Scene Graph Parser,场景图解析器,将语句(自然语言)解析为场景图(实体及其关系)。

安装

> conda create -n SceneGraphParser python=3.7
> conda activate SceneGraphParser
# 当时安装版本为0.1.0
> pip install SceneGraphParser 

> python -m spacy download en
# 运行失败尝试网址:
> pip install https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.0.0/en_core_web_sm-3.0.0.tar.gz
# 运行失败尝试本地:
> pip install en_core_web_sm-3.0.0.tar.gz

使用

> python
>>> import sng_parser
>>> graph = sng_parser.parse('A woman is playing the piano in the room.')
>>> sng_parser.tprint(graph) 
# 实体列表(实体头 拓展 修饰词)
Entities:
+--------+-----------+-------------+
| Head   | Span      | Modifiers   |
|--------+-----------+-------------|
| woman  | a woman   | a           |
| piano  | the piano | the         |
| room   | the room  | the         |
+--------+-----------+-------------+
# 关系列表(实体头 关系 实体头)
Relations:
+-----------+------------+----------+
| Subject   | Relation   | Object   |
|-----------+------------+----------|
| woman     | playing    | piano    |
| woman     | in         | room     |
+-----------+------------+----------+

# lemma表示词元
# 主要使用entities:lemma_head, entities:lemma_span, entities:modifiers:lemma_span
#        relations:object, relations:lemma_relation, relations:subject
>>> from pprint import pprint
>>> pprint(graph)
{'entities': [{'head': 'woman',
               'lemma_head': 'woman',
               'lemma_span': 'a woman',
               'modifiers': [{'dep': 'det', 'lemma_span': 'a', 'span': 'A'}],
               'span': 'A woman',
               'span_bounds': (0, 2),
               'type': 'unknown'},
              {'head': 'piano',
               'lemma_head': 'piano',
               'lemma_span': 'the piano',
               'modifiers': [{'dep': 'det',
                              'lemma_span': 'the',
                              'span': 'the'}],
               'span': 'the piano',
               'span_bounds': (4, 6),
               'type': 'unknown'},
              {'head': 'room',
               'lemma_head': 'room',
               'lemma_span': 'the room',
               'modifiers': [{'dep': 'det',
                              'lemma_span': 'the',
                              'span': 'the'}],
               'span': 'the room',
               'span_bounds': (7, 9),
               'type': 'scene'}],
 'relations': [{'lemma_relation': 'play',
                'object': 1,
                'relation': 'playing',
                'subject': 0},
               {'lemma_relation': 'in',
                'object': 2,
                'relation': 'in',
                'subject': 0}]}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MallocLu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值