Beispiel #1
0
    def ready(self):
        if 'UWKGM_STATE' in os.environ and os.environ[
                'UWKGM_STATE'] == 'running':
            verbose.info('Initializing NLTK wordnet...', KnowledgeConfig)

            try:
                _create_unverified_https_context = ssl._create_unverified_context
            except AttributeError:
                pass
            else:
                ssl._create_default_https_context = _create_unverified_https_context

            nltk.download('wordnet')

            verbose.info('Testing MongoDB database connection...')
            pymongo.MongoClient('mongodb://%s:%d/' %
                                (env.resolve('database.mongo.address'),
                                 env.resolve('database.mongo.port')))
            verbose.info('MongoDB connection test pass')
Beispiel #2
0
def openie(text: str) -> List[Tuple[str, str, str]]:
    """Extracts triples using Stanford CoreNLP OpenIE library via CoreNLPConnector in Java REST API

    :param text: A string to be extracted
           :ex: Barack Obama born in Hawaii
    :return: A list of triples
    """

    client = env.resolve('servers.java')
    verbose.info('Extracting triples using OpenIE at: ' + client['address'], caller=openie)
    return requests.get('%s/openie/triples' % client['address'], params={'text': text}).json()
Beispiel #3
0
def dbpedia(triple: Tuple[str, str, str], predicate: str) -> bool:
    """Verifies domain-range agreement using DBpedia's ontology

    :param triple: A triple (subject's entity, predicate, object's entity)
           :ex: ["http://dbpedia.org/resource/Barack_Obama", "bear in", "http://dbpedia.org/resource/Hawaii"]
    :param predicate: Entity of the predicate to be verified
           :ex: http://dbpedia.org/ontology/birthPlace
    :return: True if the predicate does not constitute domain-range violation
    """

    # NOTE: Since DBpedia is expected to be the primary service for ontology look-up in this project,
    #       removing part of the URI helps reduce redundant information showed on a screen.
    #       The following lines of code should be modified when the service endpoint is changed.
    config = env.resolve('database.virtuoso')
    e = '%s:%d/sparql/' % (
        config['address'], config['port']
    ) if 'port' in config else '%s/sparql/' % config['address']

    verbose.info(
        "Verifying domain-range of a predicate '%s' with SPARQL server: %s" %
        (predicate.replace('http://dbpedia.org/ontology/', ''), e), dbpedia)

    sparql = SPARQLWrapper(e)
    sparql.setQuery('SELECT DISTINCT ?vr WHERE {{ '
                    '<{0}> <http://www.w3.org/2000/01/rdf-schema#range> ?rp. '
                    '<{1}> ?vr ?rp. }}'.format(predicate, triple[2]))

    sparql.setReturnFormat(JSON)

    try:
        results = sparql.query().convert()

        for result in results['results']['bindings']:
            if len(result['vr']['value']) > 0:
                return True

    except QueryBadFormed as error:
        verbose.error(str(error), dbpedia)
        return False

    # Domain-range violation concluded
    return False
Beispiel #4
0
"""Virtuoso driver for graph database

The UWKGM project
:copyright: (c) 2020 Ichise Laboratory at NII & AIST
:author: Rungsiman Nararatwong
"""

from dorest import env
from SPARQLWrapper import SPARQLWrapper, JSON

from database.database.graph import default_graph_uri

config = env.resolve('database.virtuoso')

if 'port' in config:
    client = SPARQLWrapper('%s:%s/sparql/' %
                           (config['address'], config['port']))
else:
    client = SPARQLWrapper('%s/sparql/' % config['address'])

client.addDefaultGraph(default_graph_uri)
client.setReturnFormat(JSON)
Beispiel #5
0
"""MongoDB driver for triple-modifier database

The UWKGM project
:copyright: (c) 2020 Ichise Laboratory at NII & AIST
:author: Rungsiman Nararatwong
"""

import os

import pymongo

from dorest import env

mongo_config = env.resolve('database.mongo')
mongo_address = 'mongodb://%s:%d/' % (mongo_config['address'],
                                      mongo_config['port'])

if 'UWKGM_MONGO_USERNAME' not in os.environ or 'UWKGM_MONGO_PASSWORD' not in os.environ \
        or len(os.environ['UWKGM_MONGO_USERNAME']) == 0 or len(os.environ['UWKGM_MONGO_PASSWORD']) == 0:
    mongo_client = pymongo.MongoClient(mongo_address)
else:
    mongo_client = pymongo.MongoClient(
        mongo_address,
        username=os.environ['UWKGM_MONGO_USERNAME'],
        password=os.environ['UWKGM_MONGO_PASSWORD'])

mongo = mongo_client['uwkgm']