def getInterestingLabel(self, treeNode): """ Finds 'interesting things' such as inversions, shifters, etc. """ if len(treeNode) < 2: return None if self.granularity == ma_util.GRANULARITY_COARSE: raise NotImplementedError( 'getInterestingLabels does not handle coarse labels!') catP = ma_util.sen(treeNode.node, self.granularity) catL = ma_util.sen(treeNode[0].node, self.granularity) catR = ma_util.sen(treeNode[1].node, self.granularity) if catP == catL and catP == catR: return "ID" cat_max = max(catL, catR) cat_min = min(catL, catR) cat_avg = (catL + catR) / 2.0 # cat_miv is low if max sentiment is very POS cat_miv = ma_util.VERY_POS - cat_max # cat_mav is low if min sentiment is very POS cat_mav = ma_util.VERY_POS - cat_min if catP <= cat_max and catP >= cat_min: r_label = 'AVG' # is parent less positive than most positive child # and more positive than least positive? elif catP <= cat_mav and catP >= cat_miv: r_label = 'INV' elif catL == ma_util.FINE_NEU and ((catP - ma_util.FINE_NEU) * (catR - ma_util.FINE_NEU) < 0): #r_label = 'INV/L' r_label = 'INV' elif catR == ma_util.FINE_NEU and ((catP - ma_util.FINE_NEU) * (catL - ma_util.FINE_NEU) < 0): #r_label = 'INV/R' r_label = 'INV' elif (catP - ma_util.FINE_NEU) * (cat_avg - ma_util.FINE_NEU) > 0: # (3 (4 wundervollen) (2 Szenen)) would match # here, but other rules fire first # Also not sure if there is intensification happening there? r_label = 'INT' else: #r_label = '???' r_label = "MWE" return r_label
def getProduction(self, treeNode): """ >>> pe = ProdEvaluator() >>> from nltk.tree import Tree >>> t = Tree(3, [Tree(3, "good"), Tree(2, "movie")]) >>> pe.getProduction(t) (3,3,2) >>> t = Tree(3, "good") >>> pe.getProduction(t) None """ # If the tree is binary, then its children are other tree instances # unary nodes only have for terminals and associated sentiment label if len(treeNode) < 2: return None top = ma_util.sen(treeNode.node, self.granularity) c0 = ma_util.sen(treeNode[0].node, self.granularity) c1 = ma_util.sen(treeNode[1].node, self.granularity) return (top, c0, c1)
def extract_projection_sentiment(self, tigerObj, tigerSentence): """Extracts projection feature. @param tigerObj {TigerXML} Full parse file @param tigerSentence {some XML Element} XML node of current sentence @returns {list} Sentiment Values for each Span NEG, NEU, POS """ data = [] for node in tigerObj.preOrder(tigerSentence, forceSentiment=True): data.append(ma_util.sen(node[1], self.granularity)) return data
def extract_gold_sentiment(self, goldSentence, extractLength=False): """Extracts gold label. @param goldSentence {PTB tree} Parse tree with sentiment annotation @returns {list} List of labels """ data = [] for goldNode in ma_util.walkTree(goldSentence): label = ma_util.sen(goldNode.node, self.granularity) if extractLength: data.append((label, len(goldNode.leaves()))) else: data.append(label) return data