Skip to content

Commit

Permalink
Final code for paper
Browse files Browse the repository at this point in the history
  • Loading branch information
GullyAPCBurns committed Dec 18, 2015
1 parent 37049c8 commit b877e0b
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 26 deletions.
2 changes: 2 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,5 @@
See the License for the specific language governing permissions and
limitations under the License.



3 changes: 3 additions & 0 deletions amr-core-patterns.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
op(\d+)
(\w+)-quantity
([\w\-]+)-entity
52 changes: 52 additions & 0 deletions amr-core.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
accompanier, age
beneficiary
compared-to, concession, condition, consist-of
degree, destination, direction, domain, duration
example, extent
frequency
instrument
location
manner, medium, mod, mode
ord
part, path, polarity, poss, purpose
quant
scale, source, subevent
time, topic, unit
value
wiki
calendar, century, day, dayperiod, decade, era, month, quarter, season, timezone, weekday, year, year2
prep-against, prep-along-with, prep-amid, prep-among, prep-as, prep-at
prep-by
prep-for, prep-from
prep-in, prep-in-addition-to, prep-into
prep-on, prep-on-behalf-of, prep-out-of
prep-to, prep-toward
prep-under
prep-with, prep-without
conj-as-if
location-of
polarity
degree
mode
amr-unknown
interrogative
imperative
expressive
and
or
either
neither
after
near
between
all
no
that
more
too
most
subset, subset-of
wiki, xref
product-of, sum-of
statistical-test
date-entity, date-interval
File renamed without changes.
135 changes: 135 additions & 0 deletions amr_rdf2dot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
"""
A commandline tool for drawing RDF graphs in Graphviz DOT format
You can draw the graph of an RDF file directly:
.. code-block: bash
rdf2dot my_rdf_file.rdf | dot -Tpng | display
"""

import rdflib
import rdflib.extras.cmdlineutils
from rdflib.namespace import Namespace, NamespaceManager

import sys
import cgi
import collections

from rdflib import XSD

LABEL_PROPERTIES = [rdflib.RDFS.label,
rdflib.URIRef("http://purl.org/dc/elements/1.1/title"),
rdflib.URIRef("http://xmlns.com/foaf/0.1/name"),
rdflib.URIRef("http://www.w3.org/2006/vcard/ns#fn"),
rdflib.URIRef("http://www.w3.org/2006/vcard/ns#org")
]

XSDTERMS = [
XSD[x] for x in (
"anyURI", "base64Binary", "boolean", "byte", "date",
"dateTime", "decimal", "double", "duration", "float", "gDay", "gMonth",
"gMonthDay", "gYear", "gYearMonth", "hexBinary", "ID", "IDREF",
"IDREFS", "int", "integer", "language", "long", "Name", "NCName",
"negativeInteger", "NMTOKEN", "NMTOKENS", "nonNegativeInteger",
"nonPositiveInteger", "normalizedString", "positiveInteger", "QName",
"short", "string", "time", "token", "unsignedByte", "unsignedInt",
"unsignedLong", "unsignedShort")]

EDGECOLOR = "blue"
NODECOLOR = "black"
ISACOLOR = "black"


def rdf2dot(g, stream, opts={}):
"""
Convert the RDF graph to DOT
writes the dot output to the stream
"""

fields = collections.defaultdict(set)
nodes = {}

def node(x):

if x not in nodes:
nodes[x] = "node%d" % len(nodes)
return nodes[x]

def label(x, g):

for labelProp in LABEL_PROPERTIES:
l = g.value(x, labelProp)
if l:
return l

try:
return g.namespace_manager.compute_qname(x)[2]
except:
return x

def formatliteral(l, g):
v = cgi.escape(l)
if l.datatype:
return u'"%s"^^%s' % (v, qname(l.datatype, g))
elif l.language:
return u'"%s"@%s' % (v, l.language)
return u'"%s"' % v

def qname(x, g):
try:
q = g.compute_qname(x)
return q[0] + ":" + q[2]
except:
return x

def color(p):
return "BLACK"

stream.write(u"digraph { \n node [ fontname=\"DejaVu Sans\" ] ; \n")

for s, p, o in g:
sn = node(s)
if p == rdflib.RDFS.label:
continue
if isinstance(o, (rdflib.URIRef, rdflib.BNode)):
on = node(o)
opstr = u"\t%s -> %s [ color=%s, label=< <font point-size='14' " + \
u"color='#6666ff'>%s</font> > ] ;\n"
stream.write(opstr % (sn, on, color(p), qname(p, g)))
else:
fields[sn].add((qname(p, g), formatliteral(o, g)))

for u, n in nodes.items():
stream.write(u"# %s %s\n" % (u, n))
f = []
#f = [u"<tr><td align='left' width='40px'>%s</td><td align='left'>%s</td></tr>" %
# x for x in sorted(fields[n])]
nn = g.compute_qname(u)
uu = nn[0] + u":" + nn[2]
opstr = u"%s [ shape=none, color=%s label=< <table color='#666666'" + \
u" cellborder='0' cellspacing='0' border='1'><tr>" + \
u"<td href='%s' bgcolor='#ffffff' colspan='2'>" + \
u"<font point-size='14' color='#000000'>%s</font></td>" + \
u"</tr>%s</table> > ] \n"
stream.write(opstr % (n, NODECOLOR, label(u, g), uu, u"".join(f)))

stream.write("}\n")


def _help():
sys.stderr.write("""
rdf2dot.py [-f <format>] files...
Read RDF files given on STDOUT, writes a graph of the RDFS schema in DOT
language to stdout
-f specifies parser to use, if not given,
""")


def main():
rdflib.extras.cmdlineutils.main(rdf2dot, _help)

if __name__ == '__main__':
main()
Loading

0 comments on commit b877e0b

Please sign in to comment.