예제 #1
0
    def handle_test_variables(self, command, data):
        from content import variable_manager

        dialogueID = data.get("dialogueID", None)

        if dialogueID is not None:
            authToken = dialogue.get_auth_token(dialogueID)

            expr = data.get("expr", "[{{userGender}}=male?shortTerm:longTerm]")

            print(variable_manager.evaluate_value(authToken, expr))
예제 #2
0
    def handle_response(self, command, data):
        dialogue_id = data.get("dialogueID", None)
        session_id = data.get("sessionID", None)

        if dialogue_id is not None and session_id is not None:
            dialogue.set_dialogue_id(session_id, dialogue_id)

            data = Moves().handle_moves(command, data)
            data["clearvars"] = variable_manager.get_clear_vars(
                dialogue.get_auth_token(dialogue_id),
                dialogue.get_topic(dialogue_id))

        return data
예제 #3
0
    def build_theory(self):
        system = pyaspic.ArgumentationSystem()

        all_antecedents = []

        auth_token = dialogue.get_auth_token(self.dialogue_id)

        for rule in self.argument_model.get("rules", []):
            rule = self.create_aspic_rule(
                variable_manager.insert_values(auth_token, rule))
            system.add_rule(rule)
            all_antecedents.extend([
                a[0] for a in term.get_term_specifications(rule.antecedents)
                if "<" not in a[0]
            ])

        all_antecedents = list(set(all_antecedents))

        for contrary in self.argument_model.get("contrariness", []):
            contradiction = False
            if "-" in contrary:
                symbol = "-"
                contradiction = True
            elif "^" in contrary:
                symbol = "^"
            else:
                continue

            c = contrary.split(symbol)
            for i in range(2):
                c[i] = variable_manager.insert_values(auth_token, c[i])

            system.add_contrary((c[0], c[1]), contradiction)

        for pref in self.argument_model.get("rulePreferences", []):
            p = pref.split("<")
            system.add_rule_preference((p[0].strip(), p[1].strip()))

        kb = pyaspic.KnowledgeBase()

        for t in variable_manager.get_terms(auth_token, all_antecedents):
            kb.add_premise(pyaspic.Formula(t))

        theory = pyaspic.ArgumentationTheory(system, kb)

        return theory.evaluate()
예제 #4
0
def get_entry(dialogue_id, entry, parameters=None):
    """
    Get an entry from the dictionary for the given statement
    """

    content_id = dialogue.get_content_id(dialogue_id)

    to_return = []
    if parameters is None:
        parameters = []

    dictionary = {}
    auth_token = dialogue.get_auth_token(dialogue_id)

    result = mongo.get_column("dictionary").find({"contentID": content_id})

    if result:
        for r in result:
            for key, value in r.get("entries", {}).items():
                _term = term.get_term_specification(key)
                t = variable_manager.insert_values(auth_token, _term[0])
                dictionary[t] = {"variables": _term[2], "statements": value}

    if entry in dictionary:
        entry = dictionary[entry]

        variables = entry["variables"]
        mapping = dict(zip(variables, parameters))

        # give default values to parameters not assigned
        mapping.update({a: a for a in variables if a not in mapping})

        for statement in entry.get("statements", []):
            text = statement["text"]
            for key, value in mapping.items():
                text = text.replace("${}".format(key), value)

            text = variable_manager.insert_values(auth_token, text)

            to_return.append({
                "text": text,
                "properties": statement["properties"]
            })

    return to_return
예제 #5
0
    def find_statements(self):
        statement = None
        statements = []

        print(self.descriptors)

        for key, value in self.descriptors.items():
            if key == "default" and statement is None:
                statement = value
            else:
                if ":" in key:
                    condition = key.split(":")
                    if condition[0] in self.condition_handlers:
                        if self.condition_handlers[condition[0]](condition[1]):
                            statement = value
                            break

        if statement is None:
            return []

        print("Found statement: " + statement)

        # check for statement structure
        matches = re.findall(self.statement_regex, statement)

        if matches:
            match = matches[0]
            statement = match[0]

            statement = variable_manager.insert_values(
                dialogue.get_auth_token(self.dialogue_id), statement)

            if len(match) == 2 and match[1] != "":
                parameters = self.populate_parameters(
                    dict(x.split(":") for x in match[1].split(",")))
            else:
                parameters = []

            statements = dictionary.get_entry(self.dialogue_id, statement,
                                              parameters)
        return statements
예제 #6
0
    def find_content(self, query, existing_content=None):
        """
        Use the argumentation theory to find content that matches the given query
        """
        to_return = []
        auth_token = dialogue.get_auth_token(self.dialogue_id)

        if existing_content is None:
            existing_content = {}

        vars_to_fill = []

        if query is not None:
            for key, value in existing_content.items():
                if value[0] != "$":
                    query = query.replace("${}".format(key), value)
                else:
                    vars_to_fill.append(key)

        query = variable_manager.insert_values(auth_token, query)

        if "=>" in query:
            content = []
            elements = query.split("=>")
            conclusion = elements[1].strip()
            premises = elements[0].strip().split(",")

            if conclusion == "[?]":
                pass
            else:
                content = []
                arguments = self.get_arguments_matching_conclusion(conclusion)

                arguments2 = self.get_arguments_with_conclusion(
                    conclusion, True)

                arguments = self.get_arguments_with_conclusion(
                    conclusion, True)
                support = []

                for label, arg in arguments.items():
                    for sub_arg in arg["last_sub_arguments"]:
                        support.append(
                            self.theory["arguments"][sub_arg]["conclusion"])

                if len(support) == len(vars_to_fill):
                    var_map = dict(zip(vars_to_fill, support))
                    statements = []
                    for s in support:
                        spec = term.get_term_specification(s)
                        entry = dictionary.get_entry(self.dialogue_id, spec[0],
                                                     spec[2])
                        statements.extend(entry)

                    if statements:
                        however = []
                        similar = self.get_similar_arguments(conclusion)

                        for label, arg in similar.items():
                            for sub_arg in arg["last_sub_arguments"]:
                                conclusion = self.theory["arguments"][sub_arg][
                                    "conclusion"]
                                spec = term.get_term_specification(conclusion)
                                entry = dictionary.get_entry(
                                    self.dialogue_id, spec[0], spec[2])

                                for e in entry:
                                    however.append(e["text"])

                        if however:
                            new_statements = []

                            for s in statements:
                                text = s["text"]
                                properties = s["properties"]
                                for h in however:
                                    new_statement = {
                                        "text":
                                        "{}. However, {}".format(h, text),
                                        "properties": properties
                                    }
                                    new_statements.append(new_statement)

                            statements = new_statements

                        to_return.append({
                            "content": {
                                vars_to_fill[0]: support[0]
                            },
                            "statements": statements
                        })

                        # now look for "however" components

                # arguments = self.get_arguments_matching_conclusion(conclusion)
                #
                # acceptable = [a for a in arguments if a in self.theory["extensions"][0]]
                # unacceptable = [a for a in arguments if a not in acceptable]
                #
                # however = []
                # support = []
                #
                #
                #
                # for a in unacceptable:
                #     s = [self.theory["arguments"][b]["conclusion"] for b in self.theory["arguments"][a]["last_sub_arguments"]]
                #
                #     if len(s) == len(premises):
                #         support.append(s)
                #
                #
                # for i in range(len(support)):
                #     if len(support[i]) == len(vars_to_fill):
                #         for c in support[i]:
                #             content_spec = term.get_term_specification(c)
                #             entry = dictionary.get_entry(self.dialogue_id, content_spec[0],content_spec[2])
                #             however.extend(entry)
                #
                # # need to find non-atomic args that conclude this
                # for a in acceptable:
                #     acceptable_check = [b for b in self.theory["arguments"][a]["last_sub_arguments"] if b in self.theory["extensions"][0]]
                #     if len(acceptable_check) == len(premises):
                #         content.append([self.theory["arguments"][a]["conclusion"] for a in acceptable_check])
                #
                # for i in range(len(content)):
                #     if len(content[i]) == len(vars_to_fill):
                #         var_map = dict(zip(vars_to_fill, content[i]))
                #         s = []
                #
                #         for c in content[i]:
                #             content_spec = term.get_term_specification(c)
                #             entry = dictionary.get_entry(self.dialogue_id, content_spec[0],content_spec[2])
                #             s.extend(entry)
                #
                #         statements = []
                #
                #         if however:
                #             for statement in s:
                #                 statement["text"] = however[0]["text"] + " however " + statement["text"]
                #                 statements.append(statement)
                #         else:
                #             statements = s
                #
                #         to_return.append({
                #             "content": var_map,
                #             "statements": statements
                #         })
        elif query[0] == "[" and query[-1] == "]" and len(vars_to_fill) == 1:
            query = query[1:-1]
            query_spec = term.get_term_specification(query)

            for conclusion in self.theory["acceptableConclusions"][0]:
                conclusion_spec = term.get_term_specification(conclusion)

                if conclusion_spec[0] == query_spec[0] and conclusion_spec[
                        1] == query_spec[1]:
                    entry = dictionary.get_entry(self.dialogue_id,
                                                 conclusion_spec[0],
                                                 conclusion_spec[2])

                    to_return.append({
                        "content": {
                            vars_to_fill[0]: conclusion
                        },
                        "statements": entry
                    })

        return to_return