def mapRootSentiment(source, tigerHelper, force=False): """Maps top-level sentiment between source and target sentences. If the target root node is not aligned and only has the default sentiment, we apply the sentiment value of the source root node to the target root node. We assume that root nodes always are implicitly aligned. The optional force parameter specifies whether the root sentiment is always mapped. This will override any previous mapping based on node alignments. @param source {Iterable{nltk.trees.Tree}} Source PTB trees @param target {etree} Target TigerXML tree, will be modified @param force {boolean} If True, always map sentiment between root nodes @returns Modified TigerXML tree """ for (sourceSentence, targetSentence) in itertools.izip_longest( source, th.getSentences(tigerHelper.tree), fillvalue="LIST_LENGTH_NOT_EQUAL"): rootNode = tigerHelper.getSentenceRoot(targetSentence) metaS = rootNode.get("x-sentiment") # we will typically get here before default sentiment values # have been applied, so metaS might be None. #assert metaS is not None if (not force and metaS == th.SEN_MAPPED): continue else: logger.debug("Mapping root sentiment %s for target %s", sourceSentence.node, th.getNodeID(rootNode)) th.setSentiment(rootNode, sourceSentence.node, th.SEN_MAPPED_ROOT)
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)