enable users to define properties in the input kgtk file
@@ -43,6 +43,16 @@ class TripleGenerator:
):
from etk.wikidata.statement import Rank
self.ignore = ignore
+ self.datatype_mapping = {
+ "item": Item,
+ "time": TimeValue,
+ "globe-coordinate": GlobeCoordinate,
+ "quantity": QuantityValue,
+ "monolingualtext": MonolingualText,
+ "string": StringValue,
+ "external-identifier": ExternalIdentifier,
+ "url": URLValue
+ }
self.prop_types = self.set_properties(prop_file)
self.label_set, self.alias_set, self.description_set = self.set_sets(
label_set, alias_set, description_set
@@ -68,6 +78,7 @@ class TripleGenerator:
self.order_map = {}
self.use_id = use_id
+
def _node_2_entity(self, node: str):
'''
A node can be Qxxx or Pxxx, return the proper entity.
@@ -79,23 +90,13 @@ class TripleGenerator:
return entity
def set_properties(self, prop_file: str):
- datatype_mapping = {
- "item": Item,
- "time": TimeValue,
- "globe-coordinate": GlobeCoordinate,
- "quantity": QuantityValue,
- "monolingualtext": MonolingualText,
- "string": StringValue,
- "external-identifier": ExternalIdentifier,
- "url": URLValue
- }
with open(prop_file, "r") as fp:
props = fp.readlines()
prop_types = {}
for line in props[1:]:
node1, _, node2 = line.split("\t")
try:
- prop_types[node1] = datatype_mapping[node2.strip()]
+ prop_types[node1] = self.datatype_mapping[node2.strip()]
except:
if not self.ignore:
raise KGTKException(
@@ -195,7 +196,11 @@ class TripleGenerator:
return True
def generate_prop_declaration_triple(self, node1: str, label: str, node2: str) -> bool:
- prop = WDProperty(node1, self.prop_types[node1])
+ # update the known prop_types
+ if node1 in self.prop_types:
+ raise KGTKException("Duplicated property definition of {} found!".format(node1))
+ self.prop_types[node1] = node2
+ prop = WDProperty(node1, self.datatype_mapping[node2])
self.doc.kg.add_subject(prop)
return True
@@ -420,7 +425,7 @@ class TripleGenerator:
success = self.generate_description_triple(node1, prop, node2)
elif prop in self.alias_set:
success = self.generate_alias_triple(node1, prop, node2)
- elif prop == "type":
+ elif prop == "data_type":
# special edge of prop declaration
success = self.generate_prop_declaration_triple(
node1, prop, node2)
