semdsl
项目描述
语义-DSL
用于快速开发语义支持的领域特定语言 (DSL)
安装
pip install semdsl
使用
为了说明semdsl的使用,我们将创建一个简单的 LinkML 架构,用于部分 Clue棋盘游戏,特别是用于表示关于谁犯下了罪行、在哪里以及用什么的假设。
我们将使用 语法提示 注释 该架构,这些提示可用于生成DSL的语法。
```python
>>> schema = """
... id: https://example.org/clue
... name: clue
... imports:
... - https://w3id.org/linkml/types
... classes:
... ClueHypothesis:
... attributes:
... person: # e.g. Colonel Mustard
... annotations:
... grammar.main: "WORD WORD"
... location: # e.g. Kitchen
... annotations:
... grammar.main: "WORD"
... weapon: # e.g. Candlestick
... annotations:
... grammar.main: "WORD"
... annotations:
... grammar.main: >-
... "<" person "in the" location "with the" weapon ">"
... """
我们的想法是能够使用类似 <Colonel Mustard in the Kitchen with the Candlestick>
的字符串来表示假设。
然后我们可以使用 DSLEngine
类来加载架构并生成语法
>>> from semdsl import DSLEngine
>>> engine = DSLEngine()
>>> engine.load_schema(schema)
>>> print(engine.lark_serialization)
from lark import Lark
...
class_clue_hypothesis : "<" person "in the" location "with the" weapon ">"
person : WORD WORD
location : WORD
weapon : WORD
...
默认是Lark语法。
然后您可以使用生成的语法将序列化数据解析成符合架构的pydantic对象
>>> obj = engine.parse_as_object('<Colonel Mustard in the Kitchen with the Candlestick>')
>>> print(obj.location)
Kitchen
>>> print(obj.json())
{"person": "Colonel Mustard", "location": "Kitchen", "weapon": "Candlestick"}
自动分配生成规则
在上一个示例中,我们看到了如何使用语法规则 注释 已存在的架构
然而,我们还可以从架构本身 生成 语法规则。
这是通过在类上使用 grammar.main
注解,然后在该类的属性上使用 grammar.auto
注解来完成的。例如
>>> schema = """
... id: https://example.org/clue
... name: clue
... imports:
... - https://w3id.org/linkml/types
... classes:
... ClueHypothesis:
... attributes:
... person: # e.g. Colonel Mustard
... location: # e.g. Kitchen
... weapon: # e.g. Candlestick
... """
现在我们将创建一个新的引擎并加载架构,并生成一个新的“功能式”语法
>>> engine = DSLEngine() ## create new DSLEngine
>>> engine.load_schema(schema)
>>> print(engine.lark_serialization)
from lark import Lark
...
class_clue_hypothesis : "ClueHypothesis(" slot_clue_hypothesis__person? slot_clue_hypothesis__location? slot_clue_hypothesis__weapon? ")"
slot_clue_hypothesis__person : "person=" TYPE_STRING
slot_clue_hypothesis__location : "location=" TYPE_STRING
slot_clue_hypothesis__weapon : "weapon=" TYPE_STRING
...
然后您可以使用生成的语法将字符串解析成对象
>>> obj = engine.parse_as_object('ClueHypothesis(person="Colonel Mustard" location="Kitchen" weapon="Candlestick")')
>>> print(obj.location)
Kitchen
添加额外的语义
您可以使用以下元模型元素
将URI分配给您方案中的类和槽,这可以在RDF序列化中使用。
在此,我们扩展了我们的Clue方案,为主类的槽范围添加了类
>>> schema = """
... id: https://example.org/clue
... name: clue
... prefixes:
... linkml: https://w3id.org/linkml/
... clue: https://example.org/clue/
... schema: http://schema.org/
... prov: http://www.w3.org/ns/prov#
... dbpedia: http://dbpedia.org/ontology/
... imports:
... - linkml:types
... classes:
... NamedThing:
... class_uri: schema:Thing
... attributes:
... id:
... identifier: true
... range: uriorcurie
... Person:
... class_uri: schema:Person
... is_a: NamedThing
... Location:
... class_uri: schema:Location
... is_a: NamedThing
... Weapon:
... class_uri: dbpedia:Weapon
... is_a: NamedThing
... ClueHypothesis:
... class_uri: prov:Action
... tree_root: true
... attributes:
... person: # e.g. Colonel Mustard
... slot_uri: prov:wasAssociatedWith
... range: Person
... annotations:
... grammar.main: TYPE_URIORCURIE
... location: # e.g. Kitchen
... slot_uri: prov:atLocation
... range: Location
... annotations:
... grammar.main: TYPE_URIORCURIE
... weapon: # e.g. Candlestick
... slot_uri: prov:used
... range: Weapon
... annotations:
... grammar.main: TYPE_URIORCURIE
... annotations:
... grammar.main: >-
... "<" person "in the" location "with the" weapon ">"
... """
现在解析并导出到文件。这次输入字符串使用CURIE表示Clue假设中的不同事物。
>>> engine = DSLEngine()
>>> engine.load_schema(schema)
>>> obj = engine.parse_as_object("< clue:ColonelMustard in the clue:Kitchen with the clue:Candlestick >")
>>> import yaml
>>> with open("tests/output/clue-output.yaml", "w", encoding="utf-8") as f:
... yaml.dump(obj.dict(), f)
从这里我们可以使用LinkML将其转换为RDF序列化
cd clue-output.yaml
linkml-convert clue-output -s clue_model.yaml -t ttl
结果
@prefix clue: <https://example.org/clue/> .
@prefix prov: <http://www.w3.org/ns/prov#> .
[] a prov:Action ;
prov:atLocation clue:Kitchen ;
prov:used clue:Candlestick ;
prov:wasAssociatedWith clue:ColonelMustard .
命令行界面
semdsl --help
限制
限于Lark语法
目前,semdsl仅支持Lark语法。该框架旨在允许可扩展性,例如扩展到ANTLR,但目前不支持。
项目详情
下载文件
下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分发
semdsl-0.0.1rc1.tar.gz (16.8 kB 查看哈希)
构建分发
semdsl-0.0.1rc1-py3-none-any.whl (19.9 kB 查看哈希)
关闭
semdsl-0.0.1rc1.tar.gz 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 632180978d2f3bdf1cfba53952dc52dba935422632b0810f510b4bc60de8fd89 |
|
MD5 | 925c2acfaa5207949e4badae32cd5002 |
|
BLAKE2b-256 | 8a0aaebca01345b205692f695e51ba4beac04c2972f804101b4f5af8f3b4f5ed |
关闭
semdsl-0.0.1rc1-py3-none-any.whl 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | e1c9ddd482a702a7ac54adef15fcfe301825a02fd43d036aed2c96a34141c0e8 |
|
MD5 | 9a9c97738b02e3a4ab178195a22647d1 |
|
BLAKE2b-256 | 31d0d8bbbd4beed259063c729213e02ec053313b36a124c5330941ec0b674955 |