예제 #1
0
    def ApplyActions(self, actinstring):
        #self.FailedRuleTokens.clear()
        Actions = actinstring.split()
        #logging.debug("Word:" + self.text)

        if "NEW" in Actions:
            self.features = set()
        if "NEUTRAL" in Actions:
            FeatureOntology.ProcessSentimentTags(self.features)

        HasBartagAction = False
        for Action in Actions:
            # if Action == "NEW":
            #     continue  # already process before.
            # if Action == "NEUTRAL":
            #     continue  # already process before.

            if Action[-1] == "-":
                if Action[0] == "^":  #Remove UpperRelationship
                    if "." in Action:
                        if self.UpperRelationship == Action.split(".",
                                                                  1)[1][-1]:
                            # TODO:  actually break the token. not just delattr
                            delattr(self, "UpperRelationship")
                            logging.warning(
                                " TODO:  actually break the token. not just delattr Remove Relationship:"
                                + Action)
                    else:
                        logging.warning("This Action is not right:" + Action)
                    continue

                FeatureID = FeatureOntology.GetFeatureID(Action.strip("-"))
                if FeatureID in self.features:
                    self.features.remove(FeatureID)
                continue

            if Action[-1] == "+":
                if Action[-2] == "+":
                    if Action[-3] == "+":  #"+++"
                        self.ApplyFeature(utils.FeatureID_0)
                        self.sons = []  #remove the sons of this
                        self.Head0Text = ''  #remove Head0Text.

                    else:  #"X++":
                        #this should be in a chunk, only apply to the new node
                        HasBartagAction = True
                        FeatureID = FeatureOntology.GetFeatureID(
                            Action.strip("++"))
                        self.ApplyFeature(FeatureID)
                else:  #"X+"
                    #MajorPOSFeatures = ["A", "N", "P", "R", "RB", "X", "V"]
                    #Feb 20, 2018: use the BarTagIDs[0] as the MajorPOSFeatures.
                    for bar0id in FeatureOntology.BarTagIDs[0]:
                        if bar0id in self.features:
                            self.features.remove(bar0id)

                    for bar0id in [
                            utils.FeatureID_AC, utils.FeatureID_NC,
                            utils.FeatureID_VC
                    ]:
                        if bar0id in self.features:
                            self.features.remove(bar0id)

                    FeatureID = FeatureOntology.GetFeatureID(Action.strip("+"))
                    self.ApplyFeature(FeatureID)
                continue

            if Action[0] == "^":
                if "." in Action:
                    self.UpperRelationship = Action.split(".")[-1]
                    RelationActionID = FeatureOntology.GetFeatureID(
                        self.UpperRelationship)
                    if RelationActionID != -1:
                        self.ApplyFeature(RelationActionID)
                    else:
                        logging.warning("Wrong Relation Action to apply:" +
                                        self.UpperRelationship +
                                        " in action string: " + actinstring)
                    # apply this "has" to the parent (new) node (chunk)
                    # RelationActionID = FeatureOntology.GetFeatureID("has" + self.UpperRelationship)
                    # if RelationActionID != -1:
                    #     self.ApplyFeature(RelationActionID)
                    # else:
                    #     logging.warning("Wrong Relation Action to apply:" + self.UpperRelationship + " in action string: " + actinstring)

                else:
                    logging.error(
                        "The Action is wrong: It does not have dot to link to proper pointer"
                    )
                    logging.error("  actinstring:" + actinstring)
                    self.UpperRelationship = Action[1:]
                continue

            if Action[0] == '\'':
                #Make the norm of the token to this key
                self.norm = Action[1:-1]
                continue
            if Action[0] == '%':
                #Make the pnorm of the token to this key
                self.pnorm = Action[1:-1]
                continue
            if Action[0] == '/':
                #Make the atom of the token to this key
                self.atom = Action[1:-1]
                continue
            ActionID = FeatureOntology.GetFeatureID(Action)
            if ActionID != -1:
                self.ApplyFeature(ActionID)
            else:
                logging.warning("Wrong Action to apply:" + Action +
                                " in action string: " + actinstring)

                # strtokens[StartPosition + i + GoneInStrTokens].features.add(ActionID)
        if HasBartagAction:  #only process bartags if there is new bar tag, or trunking (in the combine() function)
            FeatureOntology.ProcessBarTags(self.features)
예제 #2
0
    def ApplyDagActions(self, OpenNode, node, actinstring, rule):
        iepairmatch = re.search("(#.*#)", actinstring)
        if iepairmatch:
            ieaction = iepairmatch.group(1)
            actinstring = actinstring.replace(iepairmatch.group(1), '')
            self.ApplyDagActions_IEPair(OpenNode, node, rule, ieaction)

        Actions = actinstring.split()

        for Action in copy.copy(Actions):
            if "---" in Action:
                ParentPointer = Action[:Action.rfind(
                    '.')]  #find pointer up the the last dot "."
                parentnodeid = self.FindPointerNode(OpenNode.ID, ParentPointer,
                                                    rule, node.ID)
                if not parentnodeid:
                    return

                if "~---" in Action:
                    self.graph = set([
                        edge for edge in self.graph
                        if edge[0] != parentnodeid or edge[2] != node.ID
                    ])
                    logging.debug(
                        "Dag Action {}: Removed all edge from {} to {}".format(
                            Action, parentnodeid, node.ID))
                else:
                    self.graph = set([
                        edge for edge in self.graph
                        if edge[0] != node.ID or edge[2] != parentnodeid
                    ])
                    logging.debug(
                        "Dag Action {}: Removed all edge from {} to {}".format(
                            Action, parentnodeid, node.ID))
                Actions.pop(Actions.index(Action))

        for Action in sorted(Actions, key=lambda d: (d[-1])):
            if Action[0] == '^':
                ParentPointer = Action[:Action.rfind(
                    '.')]  #find pointer up the the last dot "."
                parentnodeid = self.FindPointerNode(OpenNode.ID, ParentPointer,
                                                    rule, node.ID)
                if not parentnodeid:
                    return

                #logging.warning("DAG Action: This action {} to apply, parent id={}".format(Action, parentnodeid))
                if Action[-1] == "-":  # remove
                    relation = Action[Action.rfind('.') + 1:-1]
                    self._RemoveEdge(node.ID, relation, parentnodeid)
                else:
                    relation = Action[Action.rfind('.') + 1:]
                    newedge = [node.ID, relation, parentnodeid]
                    if logging.root.isEnabledFor(logging.DEBUG):
                        logging.debug(
                            "DAG Action:Adding new edge: {}".format(newedge))

                    self._AddEdge(node.ID, relation, parentnodeid)

                    RelationActionID = FeatureOntology.GetFeatureID(relation)
                    if RelationActionID != -1:
                        node.ApplyFeature(RelationActionID)
                    else:
                        logging.warning(
                            "Wrong Relation Action to apply: {} in action string: {}"
                            .format(relation, actinstring))
                continue

            if Action[-1] == "-":
                FeatureID = FeatureOntology.GetFeatureID(Action.strip("-"))
                if FeatureID in node.features:
                    node.features.remove(FeatureID)
                continue

            if Action[-1] == "+":
                if Action[-2] == "+":
                    if Action[-3] == "+":  #"+++"
                        logging.error(
                            "There should be no +++ operation in DAG.")
                    else:  #"X++":
                        FeatureID = FeatureOntology.GetFeatureID(
                            Action.strip("++"))
                        node.ApplyFeature(FeatureID)
                else:  #"X+"
                    for bar0id in FeatureOntology.BarTagIDs[0]:
                        if bar0id in node.features:
                            node.features.remove(bar0id)

                    for bar0id in [
                            utils.FeatureID_AC, utils.FeatureID_NC,
                            utils.FeatureID_VC
                    ]:
                        if bar0id in node.features:
                            node.features.remove(bar0id)

                    FeatureID = FeatureOntology.GetFeatureID(Action.strip("+"))
                    node.ApplyFeature(FeatureID)
                continue

            if Action[0] == '\'':
                #Make the norm of the token to this key
                node.norm = Action[1:-1]
                continue
            if Action[0] == '%':
                # Make the pnorm of the token to this key
                node.pnorm = Action[1:-1]
                continue
            if Action[0] == '/':
                #Make the atom of the token to this key
                node.atom = Action[1:-1]
                continue
            ActionID = FeatureOntology.GetFeatureID(Action)
            if ActionID != -1:
                node.ApplyFeature(ActionID)
            else:
                logging.warning("Wrong Action to apply:" + Action +
                                " in action string: " + actinstring)

            if Action == "NEUTRAL":
                FeatureOntology.ProcessSentimentTags(node.features)