示例#1
0
 def applyConstraints(self):
     # apply role constraints
     for sentence in self.sentences:
         Utils.applyConstraints(sentence)
     # remove invalid roles
     for relation in self.relations:
         updated_roles = []
         for role in relation.roles:
             if not role.invalid:
                 updated_roles.append(role)
         relation.roles = updated_roles
示例#2
0
 def getStocks(self, relation):
     stocks = []
     stocks_phrases = relation.getSecondLevelRoles('<actor_stock:1>')
     # collect all stock phrases from given relation
     for candidate in stocks_phrases:
         if candidate.coreferent != None and Utils.isNamedEntity(candidate.coreferent) and not candidate.coreferent in stocks_phrases:
             # get named entities from given phrase
             for entity_str in Utils.getNamedEntities(candidate.coreferent):
                 if not entity_str.replace('_', ' ') in stocks:
                     stocks.append(entity_str.replace('_', ' '))
     return stocks
示例#3
0
 def getAgencies(self, relation):
     agencies = []
     agencies_phrases = relation.getSecondLevelRoles('<actor_agency:1>')
     # collect all agency phrases from given relation
     for candidate in agencies_phrases:
         if candidate.coreferent != None and Utils.isNamedEntity(candidate.coreferent) and not candidate.coreferent in agencies_phrases:
             # get named entities from given phrase
             for entity_str in Utils.getNamedEntities(candidate.coreferent):
                 # is it relevant agency string? Dfens against bugs and wrong parses
                 if RoleResolver.isRelevantAgencyEntity(entity_str) and not entity_str.replace('_', ' ') in agencies:
                     agencies.append(entity_str.replace('_', ' '))
     return agencies
示例#4
0
    def getCoreferent(self, role):
        coreferent_phrase = None
        # for coord purposes
        coreferent_clause = None

        # get number of phrase - if coreferent is coordination, it will be choosen based on number
        phrase_number = role.phrase.getNumberCategory() if role.phrase != None else 0

        # sequentially add clauses to the list order
        clauses = []
        clause_found = False
        for sentence in self.sentences:
            for clause in sentence.clauses:
                # found containing clause
                if clause == role.getRelation().containing_clause:
                    clause_found = True
                    # if clause was just found and the antecedent is not a pronoun, search also current clause
                    if role.phrase == None or  (role.phrase != None and not 'k3yR' in role.phrase.tokens[0].tag):
                        clauses.insert(0, clause)
                # clause wasn't found yet, add clause at the beginning
                elif not clause_found:
                    clauses.insert(0, clause)
                # clause was found, add clause at the end
                else:
                    clauses.append(clause)

        # find coreferent
        i = 0
        while i < len(clauses) and coreferent_phrase == None:
            j = 0
            while j < len(clauses[i].phrases) and coreferent_phrase == None:
                phrase_role = clauses[i].phrases[j].hasRole(role.second_level_role)
                # newer version - search also base roles
                if not phrase_role:
                    phrase_role = clauses[i].phrases[j].hasBaseRole(role.second_level_role)
                    # update role
                    if phrase_role:
                        phrase_role.second_level_role = role.second_level_role
                if phrase_role and phrase_role.filledWithNE():
                    coreferent_phrase = clauses[i].phrases[j]
                    coreferent_clause = clauses[i]
                j += 1
            i += 1

        # if phrase is coordination and antecedent wants just one entity
        if phrase_number == 1 and isinstance(coreferent_phrase, NPhrase) and coreferent_phrase.is_coordination:
            sub_phrases = coreferent_clause.getDependentPhrases(coreferent_phrase)
            new_coreferent = None
            i = len(sub_phrases) - 1
            # find the latest NE in given coordination
            while i >= 0 and new_coreferent == None:
                if Utils.isNamedEntity(sub_phrases[i]):
                    new_coreferent = sub_phrases[i]
                i = i - 1
            if new_coreferent != None:
                coreferent_phrase = new_coreferent

        return coreferent_phrase
示例#5
0
    def getSameNamedEntity(self, value, keys):
        return_key = None
        # exact match
        if value in keys:
            return_key = value
        # substring match
        elif self.isSubstring(value, keys) != None:
            return_key = self.isSubstring(value, keys)
        # there are just two stock values in whole text, one of them is full name (and added) and the second is abreviation
        else:
            # the new value is a abbreviation
            if Utils.isStockAbbreviation(value):

                # get all stock roles from text
                stock_strs = []
                for relation in self.text_wrapper.relations:
                    for role in relation.roles:
                        if role.second_level_role == '<actor_stock:1>' and role.filledWithNE() and role.coreferent != None and not Utils.getNamedEntityString(role.coreferent) in stock_strs:
                            stock_strs.append(Utils.getNamedEntityString(role.coreferent))
                # primitive check
                if len(stock_strs) == 2 and len(keys) == 1 and not Utils.isStockAbbreviation(keys[0]):
                    return_key = keys[0]

        return return_key
示例#6
0
 def filledWithNE(self):
     return Utils.isNamedEntity(self.phrase) if self.phrase != None else False
示例#7
0
    def createOutputObjects(self):

        # for each relevant relation
        for relation in self.text_wrapper.relations:

            # relevancy of output
            if relation.containsMainInformation() and relation.containsSpecificInformation():

                # for each stock in relation
                for stock_str in self.getStocks(relation):

                    # try to match this stock_str on some already used
                    stock_key = stock_str
                    same_named = self.getSameNamedEntity(stock_str, self.output_objects.keys())
                    if same_named != None:
                        stock_key = same_named

                    # temp holder object
                    current = {}
                    in_hashmap = False
                    if stock_key in self.output_objects.keys():
                        in_hashmap = True
                        current = self.output_objects[stock_key]

                    # # add name or abbreviation title
                    # if Utils.isStockAbbreviation(stock_str):
                    #     if not 'stock abbreviation' in current.keys():
                    #         current['stock abbreviation'] = stock_str
                    # elif not 'stock name' in current.keys():
                    #     current['stock name'] = stock_str
                    #
                    # # add price change
                    # price_change = Utils.extractPriceChange(stock_str)
                    # if price_change != None:
                    #     current['price change'] = price_change
                    entity_parts = Utils.getEntityParts(stock_str)
                    if 'name' in entity_parts.keys() and not 'stock name' in current.keys():
                        current['stock name'] = entity_parts['name']
                    if 'abbreviation' in entity_parts.keys() and not 'stock abbreviation' in current.keys():
                        current['stock abbreviation'] = entity_parts['abbreviation']
                    if 'price change' in entity_parts.keys() and not 'price change' in current.keys():
                        current['price change'] = entity_parts['price change']

                    # resolve agencies
                    agencies = self.getAgencies(relation)

                    # if there is no agency mentioned, just add to main object
                    if len(agencies) == 0:

                        # recommendation attributes
                        for recommendation in relation.getRolesWithBase('state'):
                            if recommendation.second_level_role == '<state_past:1>' and not 'past recommendation' in current.keys():
                                current['past recommendation'] = Utils.getRecommendationString(recommendation.phrase)
                            elif recommendation.second_level_role == '<state_current:1>' and not 'current recommendation' in current.keys():
                                current['current recommendation'] = Utils.getRecommendationString(recommendation.phrase)

                    # agency mentioned
                    else:

                        # create agencies attribute array
                        if not 'agencies' in current.keys():
                            current['agencies'] = {}

                        # for each agency
                        for agency_key in agencies:

                            # add agency attr.
                            if not agency_key in current['agencies'].keys():
                                current['agencies'][agency_key] = {}
                                current['agencies'][agency_key]['agency name'] = agency_key

                            # agency recommendations
                            for recommendation in relation.getRolesWithBase('state'):
                                if recommendation.second_level_role == '<state_past:1>': #and not 'past recommendation' in current['agencies'][agency_key].keys():
                                    current['agencies'][agency_key]['past recommendation'] = Utils.getRecommendationString(recommendation.phrase)
                                elif recommendation.second_level_role == '<state_current:1>': #and not 'current recommendation' in current['agencies'][agency_key].keys():
                                    current['agencies'][agency_key]['current recommendation'] = Utils.getRecommendationString(recommendation.phrase)

                            # agency prices
                            for price in relation.getRolesWithBase('price'):
                                if price.second_level_role == '<price_past:1>' and not 'past price' in current['agencies'][agency_key].keys():
                                    current['agencies'][agency_key]['past price'] = Utils.getNumberEntityString(price.phrase)
                                elif price.second_level_role == '<price_current:1>' and not 'current price' in current['agencies'][agency_key].keys():
                                    current['agencies'][agency_key]['current price'] = Utils.getNumberEntityString(price.phrase)
                                elif price.second_level_role == '<price_change:1>' and not 'price change' in current['agencies'][agency_key].keys():
                                    current['agencies'][agency_key]['price change'] = Utils.getNumberEntityString(price.phrase)

                    # prices in general
                    for price in relation.getRolesWithBase('price'):
                        if price.second_level_role == '<price_past:1>' and not 'past price' in current.keys() and len(agencies) == 0:
                            current['past price'] = Utils.getNumberEntityString(price.phrase)
                        elif price.second_level_role == '<price_current:1>' and not 'current price' in current.keys() and len(agencies) == 0:
                            current['current price'] = Utils.getNumberEntityString(price.phrase)
                        elif price.second_level_role == '<price_change:1>' and not 'price change' in current.keys() and len(agencies) == 0:
                            current['price change'] = Utils.getNumberEntityString(price.phrase)

                    # add created new object to hashmap, if contains recommendation value
                    if not in_hashmap: #and self.addedRecommendation(current):
                        self.output_objects[stock_key] = current

        # filter output objects
        self.filterOutputObjects()
示例#8
0
 def matchesPhrase(self, phrase):
     return Utils.isRecommendationValue(phrase)
示例#9
0
 def matchesPhrase(self, phrase):
     return Utils.isPriceEntity(phrase)
示例#10
0
 def matchesPhrase(self, phrase):
     return Utils.isNamedEntity(phrase)