简介
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}]}