def applyMappingToTarget(mapping, alignment, tigerHelper, stripTargetIDPrefix): """Applies sentiment values to target. Given a mapping from source node IDs to sentiment values and an alignment between source and target node IDs, the sentiment values are applied to the target tree. @param mapping map from source node IDs to sentiment values @param alignment map from source node IDs to target node IDs @param target etree parse tree - will be modified """ global countSourceNotInAlignment global countMappingApplied global countTargetNotFound for (sourceID, sentiment) in mapping.iteritems(): if not sourceID in alignment: logger.warn("Source node ID %s not found in alignment.", sourceID) countSourceNotInAlignment += 1 continue targetID = alignment[sourceID] origTargetID = targetID if stripTargetIDPrefix: targetID = ma_util.stripPrefixFromString(targetID) node = tigerHelper.getNode(targetID) if node is None: logger.warn("Could not find target node with ID %s", targetID) countTargetNotFound += 1 continue if node.tag == "s": continue countMappingApplied += 1 th.setSentiment(node, sentiment, th.SEN_MAPPED) node.set("x-source-id", sourceID) node.set("x-original-id", origTargetID)
def stripPrefixFromID(self): """Removes alphabetic prefix from ID attributes in TigerXML tree. @param tree {etree} TigerXML tree """ logger.info("Stripping prefixes from node IDs") expression = ".//*[@id]" nodes = self.tree.iterfind(expression) for node in nodes: nodeID = node.get("id") node.set("id", ma_util.stripPrefixFromString(nodeID)) expression = ".//graph[@root]" nodes = self.tree.iterfind(expression) for node in nodes: nodeID = node.get("root") node.set("root", ma_util.stripPrefixFromString(nodeID)) expression = ".//edge[@idref]" nodes = self.tree.iterfind(expression) for node in nodes: nodeID = node.get("idref") node.set("idref", ma_util.stripPrefixFromString(nodeID))