项目文件夹

文件
2026-07-13 12:37:18 +08:00

81 行
2.4 KiB
Python

# coding:utf-8
# MIT License
#
# Copyright (c) 2022 xfbai
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from penman import load as load_, Graph, Triple
from penman import loads as loads_
from penman import encode as encode_
from penman.model import Model
from penman.models.noop import NoOpModel
from penman.models import amr
op_model = Model()
noop_model = NoOpModel()
amr_model = amr.model
DEFAULT = op_model
def _get_model(dereify):
if dereify is None:
return DEFAULT
elif dereify:
return op_model
else:
return noop_model
def _remove_wiki(graph):
metadata = graph.metadata
triples = []
for t in graph.triples:
v1, rel, v2 = t
if rel == ":wiki":
t = Triple(v1, rel, "+")
triples.append(t)
graph = Graph(triples)
graph.metadata = metadata
return graph
def load(source, dereify=None, remove_wiki=False):
model = _get_model(dereify)
out = load_(source=source, model=model)
if remove_wiki:
for i in range(len(out)):
out[i] = _remove_wiki(out[i])
return out
def loads(string, dereify=None, remove_wiki=False):
model = _get_model(dereify)
out = loads_(string=string, model=model)
if remove_wiki:
for i in range(len(out)):
out[i] = _remove_wiki(out[i])
return out
def encode(g, top=None, indent=-1, compact=False):
model = amr_model
return encode_(g=g, top=top, indent=indent, compact=compact, model=model)