Skip to content

Commit

Permalink
Merge pull request #317 from fatestigma/development
Browse files Browse the repository at this point in the history
Remove warning for no ontology input
  • Loading branch information
saggu committed Jul 13, 2018
2 parents 2babf32 + 10f3709 commit e74b055
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 0 additions & 2 deletions etk/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ def __init__(self, etk, cdr_document: Dict, mime_type, url, doc_id=None) -> None
self.kg = None
if not self.etk.kg_schema:
self.etk.log("Schema not found.", "warning", self.doc_id, self.url)
if not self.etk.ontology:
self.etk.log("Ontology not found.", "warning", self.doc_id, self.url)
self._provenance_id_index = 0
self._provenances = dict()
self._jsonpath_provenances = dict()
Expand Down
2 changes: 1 addition & 1 deletion etk/ontology_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ def __is_valid_uri_resolve(self, field_name: str, context: Optional[dict]) -> UR

def rdf_generation(kg_object) -> str:
"""
Covert input knowledge graph object into n-triples RDF
Convert input knowledge graph object into n-triples RDF
:param kg_object: str, dict, or json object
:return: n-triples RDF in str
Expand Down
21 changes: 15 additions & 6 deletions examples/ontology_api/create_kg_with_ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from etk.etk import ETK
from etk.knowledge_graph import KGSchema
from etk.etk_module import ETKModule
from etk.ontology_api import Ontology
from etk.ontology_api import Ontology, rdf_generation
from rdflib.namespace import RDF


Expand All @@ -13,23 +13,29 @@ def __init__(self, etk):
ETKModule.__init__(self, etk)

def process_document(self, doc):
return [self.__create_document(data) for data in doc.cdr_document.get('data', [])]
docs = list()
for data in doc.cdr_document.get('data', []):
docs.extend(self.__create_document(data))
return docs

def __create_document(self, data):
type_ = data.get(RDF.type.toPython(), list())
type_.extend(data.get('@type', list()))
doc = self.etk.create_document(dict(), doc_id=data['@id'], type_=type_)
docs = [doc]
for key in data:
if key in {'@id', '@type', RDF.type.toPython()}:
continue
field_name = doc.kg.context_resolve(key)
for value in data[key]:
if isinstance(value, dict):
# at most nest 1 level, no need to worry what it returns
subdoc = self.__create_document(value)
doc.kg.add_value(field_name, subdoc.kg._kg)
docs.append(subdoc[0])
doc.kg.add_value(field_name, subdoc[0].doc_id)
else:
doc.kg.add_value(field_name, value)
return doc
return docs


if __name__ == "__main__":
Expand Down Expand Up @@ -114,6 +120,9 @@ def __create_document(self, data):
input_data = {'doc_id': '1', 'data': json.loads(sample_input)}
doc = etk.create_document(input_data)
docs = etk.process_ems(doc)
for doc in docs[1:]:
print(json.dumps(doc.kg.value, indent=2))
kgs = [json.dumps(doc.kg.value) for doc in docs[1:]]
with open('output.jsonl', 'w') as f:
f.write('\n'.join(kgs))
with open('output.nt', 'w') as f:
f.writelines(map(rdf_generation, kgs))

0 comments on commit e74b055

Please sign in to comment.