Skip to content

Commit

Permalink
Adapting code to new RDFLib version (#28)
Browse files Browse the repository at this point in the history
* changing code type from IOMemory/Memory to ConjunctiveGraph
* changing setup to ensure dependencies versions
* changing version
* Update README.md
  • Loading branch information
yfaria authored Jul 28, 2021
1 parent 9276032 commit 42302f8
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 106 deletions.
29 changes: 10 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,28 @@ This subcommand requires the path to the profile to serialize as argument. There

## Python module

To use the function of the transformation as a Python module, it's only needed to import `delphin.rdf`, which exports three main functions: `mrs_to_rdf`, `dmrs_to_rdf` and `eds_to_rdf`. They operate on [IO Memory](https://rdflib.readthedocs.io/en/stable/_modules/rdflib/plugins/memory.html#IOMemory) RDFLib object, creating named graphs for each instance in the context of this optimizd RDFLib store. For example, to serialize a profile to DMRS-RDF, we can do
To use the function of the transformation as a Python module, it's only needed to import `delphin.rdf`, which exports three main functions: `mrs_to_rdf`, `dmrs_to_rdf` and `eds_to_rdf`. They operate on a [ConjunctiveGraph](https://rdflib.readthedocs.io/en/stable/apidocs/rdflib.html#rdflib.graph.ConjunctiveGraph) RDFLib object, creating named graphs for each instance in the context of this conjunctive graph, which is the graph that has all quads of a specific store. For example, to serialize a profile to DMRS-RDF, we can do
```python
import delphin.rdf as drdf
from delphin import itsdb
from delphin import tsql
from delphin import itsdb, tsql
from delphin.dmrs import from_mrs as dmrs_from_mrs
from delphin.codecs.simpledmrs import decode
from rdflib import plugin
from rdflib.graph import Graph, ConjunctiveGraph
from rdflib.store import Store
from rdflib.term import BNode
from rdflib import URIRef
from rdflib.store import Store
from rdflib import RDF, RDFS
from rdflib import Namespace, Literal
from rdflib import URIRef, Namespace, Literal RDF, RDFS

path_to_profile = "./erg/trunk/tsdb/gold/mrs"
ts = itsdb.TestSuite(path_to_profile)
store = plugin.get("IOMemory", Store)()
defaultGraph = ConjunctiveGraph()
ERG = Namespace("http://www.delph-in.net/schema/erg#")
DELPH = Namespace("http://www.delph-in.net/schema/")
POS = Namespace("http://www.delph-in.net/schema/pos#")
DMRS = Namespace("http://www.delph-in.net/schema/dmrs#")
store.bind("erg", ERG)
store.bind("delph", DELPH)
store.bind("pos", POS)
store.bind("dmrs", DMRS)
defaultGraph.bind("erg", ERG)
defaultGraph.bind("delph", DELPH)
defaultGraph.bind("pos", POS)
defaultGraph.bind("dmrs", DMRS)
prefix = "http://example.com"
PROFILE = URIRef(prefix)
defaultGraph = Graph(store, identifier=BNode())
defaultGraph.add((PROFILE, RDF.type, DELPH.Profile))

for (parse_id, result_id, text, mrs_string) in tsql.select('parse-id result-id i-input mrs', ts):
Expand All @@ -69,10 +61,9 @@ for (parse_id, result_id, text, mrs_string) in tsql.select('parse-id result-id i

drdf.dmrs_to_rdf(dmrs_from_mrs(simplemrs.decode(mrs_string)),
DMRSI,
store,
defaultGraph) #inplace, change store and defaultGraph
defaultGraph) #inplace, change defaultGraph

ConjunctiveGraph(store).serialize("./dmrs-erg-gold.nq", format="nquads")
defaultGraph.serialize("./dmrs-erg-gold.nq", format="nquads")
```

## Development
Expand Down
34 changes: 16 additions & 18 deletions delphin/cli/profile_to_rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@

from rdflib.graph import Graph, ConjunctiveGraph
from rdflib.term import _is_valid_uri
from rdflib.store import Store
from rdflib import Namespace
from rdflib import plugin
from rdflib.term import BNode
from rdflib import URIRef
from rdflib import Literal
from rdflib import RDF
from rdflib import RDFS
from rdflib import RDFS

ERG = Namespace("http://www.delph-in.net/schema/erg#")
DELPH = Namespace("http://www.delph-in.net/schema/")
Expand Down Expand Up @@ -78,16 +77,16 @@ def __cli_parse__(args):
# logger.info(f"Converting {len(ts['result'])} analysis of {len(ts['item'])} sentences from {args.profile}")
logger.info(f"Converting {len(ts['result'])} analysis of {len(ts['item'])} sentences from {args.profile}")

# Creating the store and the default graph
store = plugin.get("IOMemory", Store)()
defaultGraph = Graph(store, identifier=BNode())

# Creating the Conjunctive Graph
defaultGraph = ConjunctiveGraph()
PROFILE = URIRef(f"{prefix}") # review later
defaultGraph.add((PROFILE, RDF.type, DELPH.Profile))
semrepURI, prof_semrep_relation = _get_RDF_semrep(semrep, store)
store.bind("erg", ERG)
store.bind("delph", DELPH)
store.bind("pos", POS)
# store.bind("upref", prefix) # may be useful
semrepURI, prof_semrep_relation = _get_RDF_semrep(semrep, defaultGraph)
defaultGraph.bind("erg", ERG)
defaultGraph.bind("delph", DELPH)
defaultGraph.bind("pos", POS)
# defaultGraph.bind("upref", prefix) # may be useful

# The tsql takes some time to be processed:
# logger.info(f"Loading the profile")
Expand Down Expand Up @@ -128,13 +127,12 @@ def __cli_parse__(args):

to_rdf(
obj,
SEMREPI,
store,
SEMREPI,
defaultGraph)

# serializes results
logger.info(f"Serializing results to {args.output}")
ConjunctiveGraph(store).serialize(destination=args.output, format=args.format)
defaultGraph.serialize(destination=args.output, format=args.format)
logger.info(f"DONE")

# except PyDelphinSyntaxError as e:
Expand Down Expand Up @@ -163,22 +161,22 @@ def _get_converters(semrep):

raise PyDelphinException(f"Not a valid format: {semrep}")

def _get_RDF_semrep(semrep, store):
def _get_RDF_semrep(semrep, defaultGraph):
"""
This function binds the prefix of the semantic representation to the RDF store and returns
This function binds the prefix of the semantic representation to the conjunctive graph and returns
RDFLib objects that are relevant for the conversion
"""
if semrep == "mrs":
MRS = Namespace("http://www.delph-in.net/schema/mrs#")
store.bind("mrs", MRS)
defaultGraph.bind("mrs", MRS)
return MRS.MRS, DELPH.hasMRS
elif semrep == "eds":
EDS = Namespace("http://www.delph-in.net/schema/eds#")
store.bind("eds",EDS)
defaultGraph.bind("eds",EDS)
return EDS.EDS, DELPH.hasEDS
elif semrep == "dmrs":
DMRS = Namespace("http://www.delph-in.net/schema/dmrs#")
store.bind("dmrs", DMRS)
defaultGraph.bind("dmrs", DMRS)
return DMRS.DMRS, DELPH.hasDMRS

# sets parser and interface function
Expand Down
2 changes: 1 addition & 1 deletion delphin/rdf/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

__name__ = "Delphin RDF"
__summary__ = "DELPH-IN formats in RDF"
__version__ = "1.0.3"
__version__ = "1.0.4"

__author__ = "foo"
__email__ = "foo"
Expand Down
33 changes: 10 additions & 23 deletions delphin/rdf/_dmrs_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from rdflib.graph import Graph
from rdflib.graph import Graph, ConjunctiveGraph
from rdflib import Literal
from rdflib import RDF
from rdflib import RDFS
Expand All @@ -21,29 +21,22 @@

def dmrs_to_rdf(d:delphin.dmrs._dmrs.DMRS,
DMRSI: rdflib.term.URIRef,
store:rdflib.plugins.memory.IOMemory=plugin.get("IOMemory", Store)(),
defaultGraph:rdflib.graph.Graph=None) -> rdflib.plugins.memory.IOMemory:
defaultGraph:rdflib.graph.ConjunctiveGraph=None) -> rdflib.graph.ConjunctiveGraph:
"""
Takes a PyDelphin DMRS object "d" and serializes it into a named RDF graph inside a store.
Args:
d: a PyDelphin DMRS instance to be converted into RDF format
DMRSI: URI of the DMRS instance being converted
store: RDFLib IOMemory store to add the graphs.
defaultGraph : the default graph of the store. If not given, creates one from the 'store'.
defaultGraph : the conjunctive graph representing the profile graph. If not given, creates one.
Inplace function that alters the store with the serialized DMRS and return the store as well.
"""

# Making the arguments behave well:
if defaultGraph is None:
defaultGraph = Graph(store, identifier=BNode())

if defaultGraph.store != store: # Bad function input
defaultGraph = Graph(store, identifier=BNode())
print("'defaultGraph' argument not consistent with the 'store' argument. The argument was discarded")
defaultGraph = ConjunctiveGraph()

# DMRS graph:
dmrsGraph = Graph(store, identifier=DMRSI)
dmrsGraph = Graph(store=defaultGraph.store, identifier=DMRSI)

# Creating the prefix of the DMRS elements and relevant namespaces
insprefix = Namespace(DMRSI + '#')
Expand All @@ -57,18 +50,12 @@ def dmrs_to_rdf(d:delphin.dmrs._dmrs.DMRS,
# Adding top and index
dmrsGraph.add((DMRSI, DELPH['hasTop'], NODES[d.top]))
dmrsGraph.add((DMRSI, DELPH['hasIndex'], NODES[d.index]))

# creating the prefixes of the output
# graph.bind("dmrs", DMRS)
# graph.bind("delph", DELPH)
# graph.bind("erg", ERG)
# graph.bind("pos", POS)

# Populating the graphs
__nodes_to_rdf__(d, dmrsGraph, defaultGraph, DMRSI, NODES, PREDS, SORTINFO)
__links_to_rdf__(d, dmrsGraph, defaultGraph, DMRSI, LINKS, NODES)

return store
return defaultGraph


def __nodes_to_rdf__(d, dmrsGraph, defaultGraph, DMRSI, NODES, PREDS, SORTINFO):
Expand All @@ -77,8 +64,8 @@ def __nodes_to_rdf__(d, dmrsGraph, defaultGraph, DMRSI, NODES, PREDS, SORTINFO):
Args:
d: a PyDelphin DMRS instance to be converted into RDF format
dmrsGraph: rdflib Graph of a Store of graphs where the DMRS triples will be put.
defaultGraph: the default graph of the Store with the dmrsGraph
dmrsGraph: a rdflib Graph where the DMRS triples will be put.
defaultGraph: the conjunctive graph of the profile
DMRSI: the node of the DMRS instance being converted
NODES: the URI namespace dedicated to DMRS predications
PREDS: the URI namespace dedicated to predicates
Expand Down Expand Up @@ -142,8 +129,8 @@ def __links_to_rdf__(d, dmrsGraph, defaultGraph, DMRSI, LINKS, NODES):
Args:
d: a PyDelphin DMRS instance to be converted into RDF format
dmrsGraph: rdflib Graph of a Store of graphs where the DMRS triples will be put.
defaultGraph: the default graph of the Store with the dmrsGraph
dmrsGraph: a rdflib Graph where the DMRS triples will be put.
defaultGraph: the conjunctive graph of the profile
DMRSI: the node of the DMRS instance being converted
LINKS: the URI namespace dedicated to DMRS links.
NODES: the URI namespace dedicated to DMRS predications
Expand Down
36 changes: 13 additions & 23 deletions delphin/rdf/_eds_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from rdflib.graph import Graph
from rdflib.graph import Graph, ConjunctiveGraph
from rdflib import Literal
from rdflib import RDF
from rdflib import RDFS
Expand All @@ -21,30 +21,26 @@

def eds_to_rdf(e:delphin.eds._eds.EDS,
EDSI: rdflib.term.URIRef,
store:rdflib.plugins.memory.IOMemory=plugin.get("IOMemory", Store)(),
defaultGraph:rdflib.graph.Graph=None) -> rdflib.plugins.memory.IOMemory:
defaultGraph:rdflib.graph.ConjunctiveGraph=None) -> rdflib.graph.ConjunctiveGraph:
"""
Takes a PyDelphin EDS object "e" and serializes it into a named RDF graph inside a store.
Args:
e: a PyDelphin EDS instance to be converted into RDF format
EDSI: URI of the EDS instance being converted
store: RDFLib IOMemory store to add the graphs.
defaultGraph : the default graph of the store. If not given, creates one from the 'store'.
Inplace function that alters the store with the serialized EDS and return the store as well.
defaultGraph : the conjunctive graph representing the profile graph. If not given, creates one.
Inplace function that alters the conjunctive graph with the serialized EDS and return the conjunctive graph as well.
In case of using it without giving the graph, it creates one and returns it.
"""
# Before running this, use delphin.eds.make_ids_unique(e, m) if possible

# Making the arguments behave well:
# Making the arguments behave well:
if defaultGraph is None:
defaultGraph = Graph(store, identifier=BNode())

if defaultGraph.store != store: # Bad function input
defaultGraph = Graph(store, identifier=BNode())
print("'defaultGraph' argument not consistent with the 'store' argument. The argument was discarded")
defaultGraph = ConjunctiveGraph()

# EDS graph:
edsGraph = Graph(store, identifier=EDSI)
edsGraph = Graph(store=defaultGraph.store, identifier=EDSI)

# Creating the prefix of the EDSI elements and relevant namespaces
insprefix = Namespace(EDSI + '#')
Expand All @@ -55,17 +51,11 @@ def eds_to_rdf(e:delphin.eds._eds.EDS,
# Adding top
edsGraph.add((EDSI, DELPH['hasTop'], NODES[e.top]))

# creating the prefixes of the output
# graph.bind("eds", EDS)
# graph.bind("delph", DELPH)
# graph.bind("erg", ERG)
# graph.bind("pos", POS)

# Populating the graphs
__nodes_to_rdf__(e, edsGraph, defaultGraph, EDSI, NODES, PREDS, SORTINFO)
__edges_to_rdf__(e, edsGraph, NODES)

return store
return defaultGraph


def __nodes_to_rdf__(e, edsGraph, defaultGraph, EDSI, NODES, PREDS, SORTINFO):
Expand All @@ -74,8 +64,8 @@ def __nodes_to_rdf__(e, edsGraph, defaultGraph, EDSI, NODES, PREDS, SORTINFO):
Args:
e: a PyDelphin EDS instance to be converted into RDF format
edsGraph: rdflib Graph of a Store of graphs where the EDS triples will be put.
defaultGraph: the default graph of the Store with the edsGraph
edsGraph: a rdflib Graph where the EDS triples will be put.
defaultGraph : the conjunctive graph representing the profile graph.
EDSI: the node of the EDS instance being converted
NODES: the URI namespace dedicated to EDS predications
PREDS: the URI namespace dedicated to predicates
Expand Down Expand Up @@ -138,7 +128,7 @@ def __edges_to_rdf__(e, edsGraph, NODES):
Args:
e: a PyDelphin EDS instance to be converted into RDF format
edsGraph: rdflib Graph of a Store of graphs where the EDS triples will be put.
edsGraph: a rdflib Graph where the EDS triples will be put.
NODES: the URI namespace dedicated to EDS predications
"""
for edge in e.edges:
Expand Down
Loading

0 comments on commit 42302f8

Please sign in to comment.