From 214f30d51ae7e6bd4f32f8a878ce701f2a76e8cf Mon Sep 17 00:00:00 2001 From: Ivan Herman Date: Fri, 5 Dec 2014 15:32:55 +0100 Subject: [PATCH] First step for itemprop-reverse: slight reorganization of the triple generation part --- pyMicrodata/microdata.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/pyMicrodata/microdata.py b/pyMicrodata/microdata.py index dcf9d65..c8adb9c 100644 --- a/pyMicrodata/microdata.py +++ b/pyMicrodata/microdata.py @@ -361,22 +361,26 @@ def generate_triples(self, item, context) : # todo: what follows has to be duplicated with the get_item_inverse_properties and the corresponding changes # but... what if the target/object is a literal? That must be filtered out... + def get_predicate_object(prop, name) : + # 9.1.1. set a new context + new_context = context.new_copy(itype) + # 9.1.2, generate the URI for the property name, that will be the predicate + # Also update the context + # Note that the method also checks, and stores, the possible superproperty/equivalent property values + new_context.current_name = predicate = self.generate_predicate_URI(name, new_context) + # 9.1.3, generate the property value. The extra flag signals that the value is a new item + # Note that 9.1.4 step is done in the method itself, ie, a recursion may occur there + # if a new item is hit (in which case the return value is a RDF resource chaining to a subject) + # Note that the value may be None (e.g, for an element without a @src), in which case nothing + # is generated + value = self.get_property_value(prop, new_context) + return (predicate, value) + # Step 9: Get the item properties and run a cycle on those # each entry in the dictionary is an array of RDF objects for prop in self.get_item_properties(item) : for name in prop.getAttribute("itemprop").strip().split() : - # 9.1.1. set a new context - new_context = context.new_copy(itype) - # 9.1.2, generate the URI for the property name, that will be the predicate - # Also update the context - # Note that the method also checks, and stores, the possible superproperty/equivalent property values - new_context.current_name = predicate = self.generate_predicate_URI(name, new_context) - # 9.1.3, generate the property value. The extra flag signals that the value is a new item - # Note that 9.1.4 step is done in the method itself, ie, a recursion may occur there - # if a new item is hit (in which case the return value is a RDF resource chaining to a subject) - # Note that the value may be None (e.g, for an element without a @src), in which case nothing - # is generated - value = self.get_property_value(prop, new_context) + (predicate, value) = get_predicate_object(prop, name) if value is None : continue # 9.1.5, generate the triple self.graph.add((subject, URIRef(predicate), value))