Пример #1
0
 def test_morphs(self):
     context = WNRDF.WNRDFContext("wordnet_3.1+.db")
     graph = WNRDF.entry(context, "abhor","v")
     serialization = graph.serialize()
     assert("abhorred" in serialization)
     assert("abhorring" in serialization)
Пример #2
0
 def test_entry(self):
     context = WNRDF.WNRDFContext("wordnet_3.1+.db")
     graph = WNRDF.entry(context, "cat","n")
     print graph.serialize()
     assert(graph)
Пример #3
0
 def test_adjposition(self):
     context = WNRDF.WNRDFContext("wordnet_3.1+.db")
     graph = WNRDF.entry(context, "inclined","a")
     serialization = graph.serialize()
     assert("adjposition" in serialization)
Пример #4
0
 def test_morphs(self):
     context = WNRDF.WNRDFContext("wordnet_3.1+.db")
     graph = WNRDF.entry(context, "abhor", "v")
     serialization = graph.serialize()
     assert ("abhorred" in serialization)
     assert ("abhorring" in serialization)
Пример #5
0
 def test_adjposition(self):
     context = WNRDF.WNRDFContext("wordnet_3.1+.db")
     graph = WNRDF.entry(context, "inclined", "a")
     serialization = graph.serialize()
     assert ("adjposition" in serialization)
Пример #6
0
 def test_entry(self):
     context = WNRDF.WNRDFContext("wordnet_3.1+.db")
     graph = WNRDF.entry(context, "cat", "n")
     print graph.serialize()
     assert (graph)
Пример #7
0
    def application(self, environ, start_response):
        uri = environ['PATH_INFO']
        if re.match(".*\.html", uri):
            mime = "html"
        elif re.match(".*\.rdf", uri):
            mime = "pretty-xml"
        elif re.match(".*\.ttl", uri):
            mime = "turtle"
        elif re.match(".*\.nt", uri):
            mime = "nt"
        elif re.match(".*\.json", uri):
            mime = "json-ld"
        elif 'HTTP_ACCEPT' in environ:
            if uri.startswith("/sparql"):
                mime = self.best_mime_type(environ['HTTP_ACCEPT'], "sparql")
            else:
                mime = self.best_mime_type(environ['HTTP_ACCEPT'], "html")
        else:
            mime = "html"

        if uri == "/" or uri == "/index.html":
            start_response('200 OK', [('Content-type', 'text/html')])
            return [
                self.render_html("WordNet RDF",
                                 open(resolve("index.html")).read())
            ]
        if uri == "/license.html":
            start_response('200 OK', [('Content-type', 'text/html')])
            return [
                self.render_html("WordNet RDF",
                                 open(resolve("license.html")).read())
            ]
        elif uri == "/wnrdf.css" or uri == "/sparql/wnrdf.css":
            start_response('200 OK', [('Content-type', 'text/css')])
            return [open(resolve("wnrdf.css")).read()]
        elif re.match(
                "/%s/(\d+)\-[nvarspNVARSP](|\.nt|\.html|\.rdf|\.ttl|\.json)$" %
                WNRDF.wn_version, uri):
            synset_id = re.findall(
                "/%s/(\d+)\-[nvarspNVARSP]" % WNRDF.wn_version, uri)[0]
            if len(synset_id) == 8:
                synset_id = str(WNRDF.pos2number(uri[-1])) + synset_id
                return self.send302(
                    start_response,
                    "/%s/%s-%s" % (WNRDF.wn_version, synset_id, uri[-1]))
            translate = True
            if uri[-1].isupper():
                translate = False
            graph = WNRDF.synset(self.wordnet_context,
                                 int(synset_id),
                                 extras=mime == "html",
                                 translate=translate)
            if graph is None:
                return self.send404(start_response)
            title = ', '.join(
                sorted([
                    str(o)
                    for _, _, o in graph.triples((None, RDFS.label, None))
                ]))
            if mime == "html":
                content = self.rdfxml_to_html(graph, title)
            else:
                try:
                    content = graph.serialize(
                        format=mime,
                        context=self.wordnet_context.jsonld_context)
                except Exception:
                    return self.send501(start_response)
            start_response('200 OK', [('Content-type', self.mime_types[mime]),
                                      ('Vary', 'Accept'),
                                      ('Content-length', str(len(content)))])
            return [content]


#        elif re.match("/title/%s/(\d+)\-[nvarsp](|\.nt|\.html|\.rdf|\.ttl|\.json)$" % WNRDF.wn_version, uri):
#            synset_id = re.findall("/%s\-(\d+)\-[nvarsp]" % WNRDF.wn_version, uri)[0]
#            graph = WNRDF.synset(self.wordnet_context, int(synset_id))
#            if graph is None:
#                return self.send404(start_response)
#            title = ', '.join(sorted([str(o) for _, _, o in graph.triples((None, RDFS.label, None))]))
#            start_response('200 OK', [('Content-type', 'text/plain')])
#            return [title]
        elif re.match(
                "/%s/(.*)\-[nvarsp](|\.nt|\.html|\.rdf|\.ttl|\.json)$" %
                WNRDF.wn_version, uri):
            lemma_pos, = re.findall("^/%s/(.*)\-([nvarsp])" % WNRDF.wn_version,
                                    uri)
            lemma, pos = lemma_pos
            graph = WNRDF.entry(self.wordnet_context, unquote_plus(lemma), pos)
            if graph is None:
                return self.send404(start_response)
            can_form = graph.value(WNRDF.entry_name(unquote_plus(lemma), pos),
                                   WNRDF.lemon.canonicalForm)
            title = graph.value(can_form, WNRDF.lemon.writtenRep)
            if mime == "html":
                content = self.rdfxml_to_html(graph, str(title))
            else:
                try:
                    content = graph.serialize(
                        format=mime,
                        context=self.wordnet_context.jsonld_context,
                        base=WNRDF.entry_name(unquote_plus(lemma), pos))
                except Exception as x:
                    return self.send501(start_response)
            start_response('200 OK', [('Content-type', self.mime_types[mime]),
                                      ('Vary', 'Accept'),
                                      ('Content-length', str(len(content)))])
            return [content]
        elif re.match(
                "/wn30/(\d{8}\-[nvarsp])(|\.nt|\.html|\.rdf|\.ttl|\.json)$",
                uri):
            (synset_id, ext), = re.findall(
                "/wn30/(\d{8}\-[nvarsp])(|\.nt|\.html|\.rdf|\.ttl|\.json)$",
                uri)
            with sqlite3.connect(resolve('mapping/mapping.db')) as conn:
                c = conn.cursor()
                c.execute("select wn31 from wn30 where wn30=?", (synset_id, ))
                row = c.fetchone()
                if row:
                    wn31, = row
                    return self.send302(start_response,
                                        "/wn31/%s-%s" % (wn31, synset_id[-1]))
                else:
                    return self.send404(start_response)
        elif re.match(
                "/wn20/(\d{8}\-[nvarsp])(|\.nt|\.html|\.rdf|\.ttl|\.json)$",
                uri):
            (synset_id, ext), = re.findall(
                "/wn20/(\d{8}\-[nvarsp])(|\.nt|\.html|\.rdf|\.ttl|\.json)$",
                uri)
            print(synset_id)
            with sqlite3.connect(resolve('mapping/mapping.db')) as conn:
                c = conn.cursor()
                c.execute(
                    "select wn31 from wn30 join wn20 on wn30.wn30 = wn20.wn30 where wn20=?",
                    (synset_id, ))
                row = c.fetchone()
                if row:
                    wn31, = row
                    return self.send302(start_response,
                                        "/wn31/%s-%s" % (wn31, synset_id[-1]))
                else:
                    return self.send404(start_response)
        elif uri == "/search" or uri == "/search/":
            start_response('200 OK', [('Content-type', 'text/html')])
            if 'QUERY_STRING' in environ:
                qs_parsed = parse_qs(environ['QUERY_STRING'])
                if 'query' in qs_parsed:
                    lemma = qs_parsed['query'][0]
                    result = self.search(self.wordnet_context, lemma)
                    return [result]
                else:
                    return ["No query"]
            else:
                return ["No query string"]
        elif re.match("/ontology(|.nt|.html|.ttl|.rdf)", uri):
            start_response('200 OK', [
                ('Content-type', self.mime_types[mime]),
                ('Content-length',
                 str(
                     os.stat(
                         resolve("ontology" + self.mime_ext[mime])).st_size))
            ])
            return [open(resolve("ontology" + self.mime_ext[mime])).read()]
        elif uri == ("/%s.nt.gz" % WNRDF.wn_version):
            start_response(
                '200 OK',
                [('Content-type', 'appliction/x-gzip'),
                 ('Content-length', str(os.stat("wordnet.nt.gz").st_size))])
            return open(resolve("wordnet.nt.gz"), "rb").read()
        elif uri.startswith("/flag/") and exists(resolve(uri[1:])):
            start_response(
                '200 OK',
                [('Content-type', 'image/gif'),
                 ('Content-length', str(os.stat(resolve(uri[1:])).st_size))])
            return open(resolve(uri[1:]), "rb").read()
        elif uri == "/sparql/":
            if 'QUERY_STRING' in environ:
                qs = parse_qs(environ['QUERY_STRING'])
                if 'query' in qs:
                    return self.sparql_query(
                        qs['query'][0], mime,
                        qs.get('default-graph-uri', [None])[0], start_response)
                else:
                    start_response('200 OK', [('Content-type', 'text/html')])
                    return [
                        self.render_html("WordNet RDF",
                                         open(resolve("sparql.html")).read())
                    ]
            else:
                start_response('200 OK', [('Content-type', 'text/html')])
                return [
                    self.render_html("WordNet RDF",
                                     open(resolve("sparql.html")).read())
                ]
        else:
            return self.send404(start_response)
Пример #8
0
    def application(self, environ, start_response):
        uri = environ['PATH_INFO']
        if re.match(".*\.html", uri):
            mime = "html"
        elif re.match(".*\.rdf", uri):
            mime = "pretty-xml"
        elif re.match(".*\.ttl", uri):
            mime = "turtle"
        elif re.match(".*\.nt", uri):
            mime = "nt"
        elif re.match(".*\.json", uri):
            mime = "json-ld"
        elif 'HTTP_ACCEPT' in environ:
            if uri.startswith("/sparql"):
                mime = self.best_mime_type(environ['HTTP_ACCEPT'], "sparql")
            else:
                mime = self.best_mime_type(environ['HTTP_ACCEPT'], "html")
        else:
            mime = "html"

        if uri == "/" or uri == "/index.html":
            start_response('200 OK', [('Content-type', 'text/html')])
            return [self.render_html("WordNet RDF",open(resolve("index.html")).read())]
        if uri == "/license.html":
            start_response('200 OK', [('Content-type', 'text/html')])
            return [self.render_html("WordNet RDF",open(resolve("license.html")).read())]
        elif uri == "/wnrdf.css" or uri == "/sparql/wnrdf.css":
            start_response('200 OK', [('Content-type', 'text/css')])
            return [open(resolve("wnrdf.css")).read()]
        elif re.match("/%s/(\d+)\-[nvarspNVARSP](|\.nt|\.html|\.rdf|\.ttl|\.json)$" % WNRDF.wn_version, uri):
            synset_id = re.findall("/%s/(\d+)\-[nvarspNVARSP]" % WNRDF.wn_version, uri)[0]
            if len(synset_id) == 8:
                synset_id = str(WNRDF.pos2number(uri[-1])) + synset_id
                return self.send302(start_response,"/%s/%s-%s" % (WNRDF.wn_version, synset_id, uri[-1]))
            translate = True
            if uri[-1].isupper():
                translate = False
            graph = WNRDF.synset(self.wordnet_context, int(synset_id), extras=mime == "html", translate=translate)
            if graph is None:
                return self.send404(start_response)
            title = ', '.join(sorted([str(o) for _, _, o in graph.triples((None, RDFS.label, None))]))
            if mime == "html":
                content = self.rdfxml_to_html(graph, title)
            else:
                try:
                    content = graph.serialize(format=mime, context=self.wordnet_context.jsonld_context)
                except Exception:
                    return self.send501(start_response)
            start_response('200 OK', [('Content-type', self.mime_types[mime]),('Vary','Accept'), ('Content-length', str(len(content)))])
            return [content]
#        elif re.match("/title/%s/(\d+)\-[nvarsp](|\.nt|\.html|\.rdf|\.ttl|\.json)$" % WNRDF.wn_version, uri):
#            synset_id = re.findall("/%s\-(\d+)\-[nvarsp]" % WNRDF.wn_version, uri)[0]
#            graph = WNRDF.synset(self.wordnet_context, int(synset_id))
#            if graph is None:
#                return self.send404(start_response)
#            title = ', '.join(sorted([str(o) for _, _, o in graph.triples((None, RDFS.label, None))])) 
#            start_response('200 OK', [('Content-type', 'text/plain')])
#            return [title]
        elif re.match("/%s/(.*)\-[nvarsp](|\.nt|\.html|\.rdf|\.ttl|\.json)$" % WNRDF.wn_version, uri):
            lemma_pos, = re.findall("^/%s/(.*)\-([nvarsp])" % WNRDF.wn_version, uri)
            lemma, pos = lemma_pos
            graph = WNRDF.entry(self.wordnet_context, unquote_plus(lemma), pos)
            if graph is None:
                return self.send404(start_response)
            can_form = graph.value(WNRDF.entry_name(unquote_plus(lemma), pos), WNRDF.lemon.canonicalForm)
            title = graph.value(can_form, WNRDF.lemon.writtenRep)
            if mime == "html":
                content = self.rdfxml_to_html(graph, str(title))
            else:
                try:
                    content = graph.serialize(format=mime, context=self.wordnet_context.jsonld_context,
                                              base=WNRDF.entry_name(unquote_plus(lemma), pos))
                except Exception as x:
                    return self.send501(start_response)
            start_response('200 OK', [('Content-type', self.mime_types[mime]),('Vary','Accept'), ('Content-length', str(len(content)))])
            return [content]
        elif re.match("/wn30/(\d{8}\-[nvarsp])(|\.nt|\.html|\.rdf|\.ttl|\.json)$", uri):
            (synset_id, ext), = re.findall("/wn30/(\d{8}\-[nvarsp])(|\.nt|\.html|\.rdf|\.ttl|\.json)$", uri)
            with sqlite3.connect(resolve('mapping/mapping.db')) as conn:
                c = conn.cursor()
                c.execute("select wn31 from wn30 where wn30=?", (synset_id,))
                row = c.fetchone()
                if row:
                    wn31, = row
                    return self.send302(start_response, "/wn31/%s-%s" % (wn31, synset_id[-1]))
                elif synset_id.endswith("s"):
                    synset_id = synset_id[:-1] + "a"
                    c.execute("select wn31 from wn30 where wn30=?", (synset_id,))
                    row = c.fetchone()
                    if row:
                        wn31, = row
                        return self.send302(start_response, "/wn31/%s-%s" % (wn31, synset_id[-1]))
                    else:
                        return self.send404(start_response)
                else:
                    return self.send404(start_response)
        elif re.match("/wn20/(\d{8}\-[nvarsp])(|\.nt|\.html|\.rdf|\.ttl|\.json)$", uri):
            (synset_id, ext), = re.findall("/wn20/(\d{8}\-[nvarsp])(|\.nt|\.html|\.rdf|\.ttl|\.json)$", uri)
            print(synset_id)
            with sqlite3.connect(resolve('mapping/mapping.db')) as conn:
                c = conn.cursor()
                c.execute("select wn31 from wn30 join wn20 on wn30.wn30 = wn20.wn30 where wn20=?", (synset_id,))
                row = c.fetchone()
                if row:
                    wn31, = row
                    return self.send302(start_response, "/wn31/%s-%s" % (wn31, synset_id[-1]))
                else:
                    return self.send404(start_response)
        elif uri == "/search" or uri == "/search/":
            start_response('200 OK', [('Content-type', 'text/html')])
            if 'QUERY_STRING' in environ:
                qs_parsed = parse_qs(environ['QUERY_STRING'])
                if 'query' in qs_parsed:
                    lemma = qs_parsed['query'][0]
                    result = self.search(self.wordnet_context, lemma)
                    return [result]
                else:
                    return ["No query"]
            else:
                return ["No query string"]
        elif re.match("/ontology(|.nt|.html|.ttl|.rdf)",uri):
            start_response('200 OK', [('Content-type', self.mime_types[mime]),
                                      ('Content-length', str(os.stat(resolve("ontology" + self.mime_ext[mime])).st_size))])
            return [open(resolve("ontology" + self.mime_ext[mime])).read()]
        elif uri == ("/%s.nt.gz" % WNRDF.wn_version):
            start_response('200 OK', [('Content-type', 'appliction/x-gzip'),
                                      ('Content-length', str(os.stat("wordnet.nt.gz").st_size))])
            return open(resolve("wordnet.nt.gz"), "rb").read()
        elif uri.startswith("/flag/") and exists(resolve(uri[1:])):
            start_response('200 OK', [('Content-type', 'image/gif'),
                ('Content-length', str(os.stat(resolve(uri[1:])).st_size))])
            return open(resolve(uri[1:]), "rb").read() 
        elif uri == "/sparql/":
            if 'QUERY_STRING' in environ:
                qs = parse_qs(environ['QUERY_STRING'])
                if 'query' in qs:
                    return self.sparql_query(qs['query'][0], mime, qs.get('default-graph-uri',[None])[0], start_response)
                else:
                    start_response('200 OK', [('Content-type', 'text/html')])
                    return [self.render_html("WordNet RDF",open(resolve("sparql.html")).read())]
            else:
                start_response('200 OK', [('Content-type', 'text/html')])
                return [self.render_html("WordNet RDF",open(resolve("sparql.html")).read())]
        else:
            return self.send404(start_response)