Пример #1
0
def interpret_ref_obj_filter(interpreter, speaker, filters_d):
    F = MemoryFilter(interpreter.agent.memory)

    # currently spec intersects all comparators TODO?
    comparator_specs = filters_d.get("comparator")
    if comparator_specs:
        for s in comparator_specs:
            F.append(
                interpret_comparator(interpreter,
                                     speaker,
                                     s,
                                     is_condition=False))

    # FIXME!!! AUTHOR
    # FIXME!!! has_x=FILTERS
    # currently spec intersects all has_x, TODO?
    # FIXME!!! tags_from_dict is crude, use tags/relations appropriately
    #        triples = []
    tags = tags_from_dict(filters_d)
    triples = [{"pred_text": "has_tag", "obj_text": tag} for tag in tags]
    #        for k, v in filters_d.items():
    #            if type(k) is str and "has" in k:
    #                if type(v) is str:
    #                    triples.append({"pred_text": k, "obj_text": v})
    # Warning: BasicFilters will filter out agent's self
    # FIXME !! finer control over this ^
    if triples:
        F.append(BasicFilter(interpreter.agent.memory, {"triples": triples}))

    return F
Пример #2
0
def interpret_task_filter(interpreter, speaker, filters_d, get_all=False):
    F = MemoryFilter(interpreter.agent.memory)

    task_tags = ["currently_running", "running", "paused", "finished"]

    T = filters_d.get("triples")
    task_properties = [
        a.get("obj_text").lower() for a in T
        if a.get("obj_text", "").lower() in task_tags
    ]
    search_data = {}
    search_data["base_table"] = "Tasks"
    search_data["base_exact"] = {}
    if "currently_running" in task_properties:
        search_data["base_exact"]["running"] = 1
    if "paused" in task_properties:
        search_data["base_exact"]["paused"] = 1
    else:
        search_data["base_exact"]["paused"] = 0
    search_data["base_range"] = {}
    if "finished" in task_properties:
        search_data["base_range"]["minfinished"] = 0
    F.append(BasicFilter(interpreter.agent.memory, search_data))

    # currently spec intersects all comparators TODO?
    comparator_specs = filters_d.get("comparator")
    if comparator_specs:
        for s in comparator_specs:
            F.append(
                interpret_comparator(interpreter,
                                     speaker,
                                     s,
                                     is_condition=False))

    return F
Пример #3
0
    def interpret_time(self, interpreter, speaker, d):
        event = None

        if d.get("special_time_event"):
            return TimeCondition(interpreter.agent, d["special_time_event"])
        else:
            if not d.get("comparator"):
                raise ErrorWithResponse(
                    "I don't know how to interpret this time condition")
            dc = d["comparator"]
            dc["input_left"] = {"value_extractor": "NULL"}
            comparator = interpret_comparator(interpreter, speaker, dc)

        if d.get("event"):
            event = self(interpreter, speaker, d["event"])

        return TimeCondition(interpreter.agent, comparator, event=event)
Пример #4
0
def interpret_dance_filter(interpreter, speaker, filters_d, get_all=False):
    F = MemoryFilter(interpreter.agent.memory)
    search_data = {}
    triples = []
    for t in filters_d.get("triples"):
        triples.append(t)
    search_data["base_table"] = "Dances"
    search_data["triples"] = triples
    F.append(BasicFilter(interpreter.agent.memory, search_data))
    # currently spec intersects all comparators TODO?
    comparator_specs = filters_d.get("comparator")
    if comparator_specs:
        for s in comparator_specs:
            F.append(
                interpret_comparator(interpreter,
                                     speaker,
                                     s,
                                     is_condition=False))
    return F
Пример #5
0
    def __call__(self, interpreter, speaker, filters_d, get_all=False):
        """
        This is a subinterpreter to handle FILTERS dictionaries 

        Args:
        interpreter:  root interpreter.
        speaker (str): The name of the player/human/agent who uttered 
            the chat resulting in this interpreter
        filters_d: FILTERS logical form from semantic parser
        get_all (bool): if True, output attributes are set with get_all=True

        Outputs a (chain) of MemoryFilter objects
        """
        agent_memory = interpreter.agent.memory
        self.get_attribute = interpreter.subinterpret.get(
            "attribute", AttributeInterpreter())
        output = filters_d.get("output")
        val_map = None
        if output and type(output) is dict:
            attr_d = output.get("attribute")
            a = self.get_attribute(interpreter,
                                   speaker,
                                   attr_d,
                                   get_all=get_all)
            val_map = ApplyAttribute(agent_memory, a)
        elif output and output == "count":
            val_map = CountTransform(agent_memory)
        # NB (kavyasrinet) output can be string and have value "memory" too here

        # is this a specific memory?
        # ... then return
        mem, _ = maybe_specific_mem(interpreter, speaker,
                                    {"filters": filters_d})
        if mem:
            if val_map:
                val_map.append(FixedMemFilter(agent_memory, mem.memid))
                return val_map
            else:
                return FixedMemFilter(agent_memory, mem.memid)

        F = MemoryFilter(agent_memory)

        # currently spec intersects all comparators TODO?
        comparator_specs = filters_d.get("comparator")
        if comparator_specs:
            for s in comparator_specs:
                F.append(
                    interpret_comparator(interpreter,
                                         speaker,
                                         s,
                                         is_condition=False))

        # FIXME!!! AUTHOR
        # FIXME!!! has_x=FILTERS
        # currently spec intersects all has_x, TODO?
        # FIXME!!! tags_from_dict is crude, use tags/relations appropriately
        #        triples = []
        tags = tags_from_dict(filters_d)
        triples = [{"pred_text": "has_tag", "obj_text": tag} for tag in tags]
        #        for k, v in filters_d.items():
        #            if type(k) is str and "has" in k:
        #                if type(v) is str:
        #                    triples.append({"pred_text": k, "obj_text": v})
        # Warning: BasicFilters will filter out agent's self
        # FIXME !! finer control over this ^
        if triples:
            F.append(BasicFilter(agent_memory, {"triples": triples}))

        selector = None
        location_d = filters_d.get("location")
        if location_d:
            selector = build_linear_extent_selector(interpreter, speaker,
                                                    location_d)
        else:
            argval_d = filters_d.get("argval")
            if argval_d:
                polarity = "arg" + argval_d.get("polarity").lower()
                attribute_d = argval_d.get("quantity").get("attribute")
                selector_attribute = self.get_attribute(
                    interpreter, speaker, attribute_d)
                # FIXME
                ordinal = {
                    "first": 1,
                    "second": 2,
                    "third": 3
                }.get(argval_d.get("ordinal", "first").lower(), 1)
                sa = ApplyAttribute(agent_memory, selector_attribute)
                selector = ExtremeValueMemorySelector(agent_memory,
                                                      polarity=polarity,
                                                      ordinal=ordinal)
                selector.append(sa)

        if val_map:
            if selector:
                selector.append(F)
                val_map.append(selector)
            else:
                val_map.append(F)
            return val_map
        else:
            if selector:
                selector.append(F)
                return selector
            return F