예제 #1
0
파일: be.py 프로젝트: knighton/babi
class AgentPlaceQuestion(ClauseMeaning):
    def __init__(self):
        self.purpose = Purpose.WH_Q
        self.lemmas = BE_LEMMAS
        self.signatures = [
            [Relation.AGENT, Relation.PLACE],
        ]

    def handle(self, c, memory, (agent_xx, loc_xx)):
        if len(agent_xx) != 1:
            return None

        if len(loc_xx) != 1:
            return None

        agent = memory.ideas[agent_xx[0]]
        place = memory.ideas[loc_xx[0]]

        if agent.query == Query.IDENTITY:
            return None
        elif place.query == Query.IDENTITY:
            x = agent.location_history.get_location()
            if x is None:
                return Response('dunno')
            loc = memory.ideas[x]
            return Response(loc.kind)
        else:
            assert False
예제 #2
0
파일: be.py 프로젝트: knighton/babi
class AgentPlaceBeforeQuestion(ClauseMeaning):
    def __init__(self):
        self.purpose = Purpose.WH_Q
        self.lemmas = BE_LEMMAS
        self.signatures = [
            [Relation.AGENT, Relation.PLACE, Relation.BEFORE],
        ]

    def handle(self, c, memory, (what_xx, where_xx, before_xx)):
        if len(what_xx) != 1:
            return None

        if len(where_xx) != 1:
            return None

        if len(before_xx) != 1:
            return None

        what = memory.ideas[what_xx[0]]
        where = memory.ideas[where_xx[0]]
        before_x, = before_xx

        if what.query == Query.IDENTITY:
            return None
        elif where.query == Query.IDENTITY:
            x = what.location_history.get_location_before_location(before_x)
            if x is None:
                return Response('dunno')
            idea = memory.ideas[x]
            return Response(idea.kind)
        else:
            assert False
예제 #3
0
파일: be.py 프로젝트: knighton/babi
class AgentIn(ClauseMeaning):
    def __init__(self):
        self.purpose = Purpose.INFO
        self.lemmas = BE_LEMMAS
        self.signatures = [
            [Relation.AGENT, Relation.IN],
        ]

    def handle(self, c, memory, (agent_xx, place_xx)):
        if len(agent_xx) != 1:
            return None

        if len(place_xx) != 1:
            return None

        agent = memory.ideas[agent_xx[0]]
        place_x, = place_xx
        if c.adverbs == ['no', 'longer']:
            loc = NotAt(place_x)
            agent.location_history.set_location(loc)
            return Response()

        if c.verb.polarity.tf:
            loc = At(place_x)
        else:
            loc = NotAt(place_x)
        agent.location_history.set_location(loc)
        return Response()
예제 #4
0
파일: be.py 프로젝트: knighton/babi
class AgentIsTo(ClauseMeaning):
    def __init__(self):
        self.purpose = Purpose.INFO
        self.lemmas = BE_LEMMAS
        self.signatures = [
            [Relation.AGENT, Relation.TO],
        ]

    def handle(self, c, memory, (agent_xx, to_xx)):
        if len(agent_xx) != 1:
            return None

        if len(to_xx) != 1:
            return None

        agent_x, = agent_xx
        agent = memory.ideas[agent_x]

        to_x, = to_xx
        to = memory.ideas[to_x]

        if isinstance(agent, Noun) and isinstance(to, Direction):
            direction = get_direction(to.which)
            memory.graph.link(agent_x, direction, to.of_x)
            return Response()
        else:
            return None
예제 #5
0
파일: give.py 프로젝트: knighton/babi
class Give(ClauseMeaning):
    def __init__(self):
        self.purpose = Purpose.INFO
        self.lemmas = GIVE_LEMMAS
        self.signatures = [
            [Relation.AGENT, Relation.TO_RECIPIENT, Relation.TARGET],
        ]

    def handle(self, c, memory, (give_xx, recv_xx, what_xx)):
        if len(recv_xx) != 1:
            return None

        for give_x in give_xx:
            giver = memory.ideas[give_x]
            giver.carrying = filter(lambda x: x not in what_xx, giver.carrying)

        for recv_x in recv_xx:
            receiver = memory.ideas[recv_x]
            receiver.carrying += what_xx

            xx = []
            seen = set()
            for x in receiver.carrying:
                if x in seen:
                    continue
                xx.append(x)
            receiver.carrying = xx

        return Response()
예제 #6
0
파일: be.py 프로젝트: knighton/babi
class AgentIsTargetQuestion(ClauseMeaning):
    def __init__(self):
        self.purpose = Purpose.TF_Q
        self.lemmas = BE_LEMMAS
        self.signatures = [
            [Relation.AGENT, Relation.TARGET],
        ]

    def handle(self, c, memory, (agent_xx, target_xx)):
        if len(agent_xx) != 1:
            return None

        if len(target_xx) != 1:
            return None

        agent_x, = agent_xx
        target_x, = target_xx

        agent = memory.ideas[agent_x]
        target = memory.ideas[target_x]

        if isinstance(agent, Noun) and isinstance(target, Comparative):
            if target.polarity == ComparativePolarity.POS and \
                    target.adjective == 'big':
                s = memory.graph.is_direction(agent_x, 'is_bigger_than',
                                              target.than_x)
                return Response(s)

        return None
예제 #7
0
def go_common(c, memory, agent_xx, to_xx, when_xx=None):
    if len(to_xx) != 1:
        return None

    if when_xx and len(when_xx) != 1:
        return None

    if when_xx:
        when_x, = when_xx
        when = memory.ideas[when_x]
        if isinstance(when, RelativeDay):
            time_span = when.to_time_span()
        else:
            time_span = None
    else:
        time_span = None

    to_x, = to_xx
    for x in agent_xx:
        agent = memory.ideas[x]
        loc = At(to_x)
        agent.location_history.set_location(loc, time_span)
        for x2 in agent.carrying:
            loc = At(to_x)
            memory.ideas[x2].location_history.set_location(loc, time_span)

    return Response()
예제 #8
0
class WhyGoToQuestion(ClauseMeaning):
    def __init__(self):
        self.purpose = Purpose.WH_Q
        self.lemmas = GO_LEMMAS
        self.signatures = [
            [Relation.AGENT, Relation.TO, Relation.REASON],
            [Relation.AGENT, Relation.TO, Relation.BECAUSE],
        ]

    def handle(self, c, memory, (agent_xx, to_xx, why_xx)):
        if len(agent_xx) != 1:
            return None

        if len(to_xx) != 1:
            return None

        if len(why_xx) != 1:
            return None

        agent_x, = agent_xx
        to_x, = to_xx
        why_x, = why_xx
        agent = memory.ideas[agent_x]
        to = memory.ideas[to_x]
        why = memory.ideas[why_x]
        if isinstance(agent, Noun) and isinstance(to, Noun) and \
                isinstance(why, Noun):
            if not agent.query and not to.query and why.query == Query.IDENTITY:
                causes = GO2CAUSES.get(to.kind, 'dunno')
                if not causes:
                    return Response('dunno')
                for cause in causes:
                    if cause in agent.attributes:
                        return Response(cause)
                return Response(causes[0])
            else:
                return None
        else:
            return None
예제 #9
0
파일: be.py 프로젝트: knighton/babi
class AgentIsTarget(ClauseMeaning):
    def __init__(self):
        self.purpose = Purpose.INFO
        self.lemmas = BE_LEMMAS
        self.signatures = [
            [Relation.AGENT, Relation.TARGET],
        ]

    def handle(self, c, memory, (agent_xx, target_xx)):
        if len(agent_xx) != 1:
            return None

        if len(target_xx) != 1:
            return None

        agent_x, = agent_xx
        agent = memory.ideas[agent_x]
        target_x, = target_xx
        target = memory.ideas[target_x]
        if isinstance(agent, Noun) and isinstance(target, Comparative):
            if target.polarity == ComparativePolarity.POS and \
                    target.adjective == 'big':
                memory.graph.link(agent_x, 'is_bigger_than', target.than_x)
                return Response()
            else:
                pass
        elif isinstance(agent, Noun) and isinstance(target, Direction):
            direction = get_direction(target.which)
            memory.graph.link(agent_x, direction, target.of_x)
            return Response()
        elif isinstance(agent, Noun) and isinstance(target, Noun):
            agent.assign(target)
            memory.ideas[agent_x] = agent
            memory.ideas[target_x] = None
            return Response()
        else:
            pass

        return None
예제 #10
0
class Fear(ClauseMeaning):
    def __init__(self):
        self.purpose = Purpose.INFO
        self.lemmas = FEAR_LEMMAS
        self.signatures = [[Relation.AGENT, Relation.TARGET]]

    def handle(self, c, memory, (agent_xx, target_xx)):
        if len(agent_xx) != 1:
            return None

        if len(target_xx) != 1:
            return None

        return Response()
예제 #11
0
class GoWhereQuestion(ClauseMeaning):
    def __init__(self):
        self.purpose = Purpose.WH_Q
        self.lemmas = GO_LEMMAS
        self.signatures = [
            [Relation.AGENT, Relation.PLACE],
        ]

    def handle(self, c, memory, (agent_xx, to_xx)):
        if len(agent_xx) != 1:
            return None

        if len(to_xx) != 1:
            return None

        agent_x, = agent_xx
        to_x, = to_xx
        agent = memory.ideas[agent_x]
        to = memory.ideas[to_x]
        if isinstance(agent, Noun) and isinstance(to, Noun):
            if not agent.query and to.query == Query.IDENTITY and \
                    to.kind == 'place':
                if c.verb.tense == Tense.FUTURE:
                    for adj in agent.attributes:
                        effects = CAUSE2EFFECTS.get(adj)
                        if not effects:
                            continue
                        for verb, target in effects:
                            if verb == 'go':
                                return Response(target)
                    return Response('dunno')
                else:
                    return None
            else:
                return None
        else:
            return None
예제 #12
0
파일: be.py 프로젝트: knighton/babi
class AgentInQuestion(ClauseMeaning):
    def __init__(self):
        self.purpose = Purpose.TF_Q
        self.lemmas = BE_LEMMAS
        self.signatures = [
            [Relation.AGENT, Relation.IN],
        ]

    def handle(self, c, memory, (agent_xx, place_xx)):
        if len(agent_xx) != 1:
            return None

        if len(place_xx) != 1:
            return None

        agent = memory.ideas[agent_xx[0]]
        place_x, = place_xx
        r = agent.location_history.is_at_location(place_x)
        if r is None:
            return Response('maybe')
        elif r:
            return Response('yes')
        else:
            return Response('no')
예제 #13
0
파일: be.py 프로젝트: knighton/babi
def a_direction_b(memory, agent_xx, relation, what_xx):
    agents = map(lambda x: memory.ideas[x], agent_xx)
    for n in agents:
        if not isinstance(n, Noun):
            return None

    whats = map(lambda x: memory.ideas[x], what_xx)
    for n in whats:
        if not isinstance(n, Noun):
            return None

    for agent_x in agent_xx:
        for what_x in what_xx:
            memory.graph.link(agent_x, relation, what_x)

    return Response()
예제 #14
0
class HowGoFromToQuestion(ClauseMeaning):
    def __init__(self):
        self.purpose = Purpose.WH_Q
        self.lemmas = GO_LEMMAS
        self.signatures = [
            [
                Relation.AGENT, Relation.FROM_SOURCE, Relation.TO_LOCATION,
                Relation.WAY
            ],
        ]

    def handle(self, c, memory, (agent_xx, source_xx, to_xx, how_xx)):
        if len(source_xx) != 1:
            return None

        if len(to_xx) != 1:
            return None

        if len(how_xx) != 1:
            return None

        how = memory.ideas[how_xx[0]]
        if how.query != Query.IDENTITY:
            return None

        for x in agent_xx:
            agent = memory.ideas[x]
            if agent.query:
                return None

        source_x, = source_xx
        source = memory.ideas[source_x]
        if source.query:
            return None

        to_x, = to_xx
        to = memory.ideas[to_x]
        if to.query:
            return None

        ss = memory.graph.shortest_path(to_x, source_x)
        ss = map(lambda s: s.split('_')[1][0], ss)
        ss.reverse()
        s = ','.join(ss)
        return Response(s)
예제 #15
0
파일: be.py 프로젝트: knighton/babi
def is_a_direction_b(memory, a_xx, relation, b_xx):
    if len(a_xx) != 1:
        return None

    if len(b_xx) != 1:
        return None

    a_x, = a_xx
    a = memory.ideas[a_x]
    if not isinstance(a, Noun):
        return None

    b_x, = b_xx
    b = memory.ideas[b_x]
    if not isinstance(b, Noun):
        return None

    s = memory.graph.is_direction(a_x, relation, b_x)
    return Response(s)
예제 #16
0
class DoesItFitInside(ClauseMeaning):
    def __init__(self):
        self.purpose = Purpose.TF_Q
        self.lemmas = ['fit']
        self.signatures = [
            [Relation.AGENT, Relation.INSIDE],
            [Relation.AGENT, Relation.IN],
        ]

    def handle(self, c, memory, (agent_xx, inside_xx)):
        if len(agent_xx) != 1:
            return None

        if len(inside_xx) != 1:
            return None

        agent_x, = agent_xx
        inside_x, = inside_xx
        s = memory.graph.is_direction(agent_x, 'is_smaller_than', inside_x)
        return Response(s)
예제 #17
0
class BefearQuestion(ClauseMeaning):
    def __init__(self):
        self.purpose = Purpose.WH_Q
        self.lemmas = BEFEAR_LEMMAS
        self.signatures = [
            [Relation.AGENT, Relation.TARGET],
        ]

    def handle(self, c, memory, (agent_xx, target_xx)):
        if len(agent_xx) != 1:
            return None

        agent_x, = agent_xx
        agent = memory.ideas[agent_x]

        if len(target_xx) != 1:
            return None

        target_x, = target_xx
        target = memory.ideas[target_x]

        if isinstance(agent, Noun) and isinstance(target, Noun):
            if agent.query == Query.IDENTITY and not target.query:
                rel2xx = {
                    Relation.TARGET: target_xx,
                }
                features = ClauseFeatures(possible_lemmas=BEFEAR_LEMMAS,
                                          rel2xx=rel2xx)
                c = memory.resolve_one_clause(features)
                if c is None:
                    if not target.kind:
                        return Response('dunno')

                    features = NounFeatures(query=Query.GENERIC,
                                            kind=target.kind)
                    target_kind_xx = memory.resolve_one_noun(features)
                    if len(target_kind_xx) != 1:
                        return Response('todo')

                    rel2xx = {
                        Relation.TARGET: target_kind_xx,
                    }
                    features = ClauseFeatures(possible_lemmas=BEFEAR_LEMMAS,
                                              rel2xx=rel2xx)
                    c = memory.resolve_one_clause(features)

                if c is None:
                    return Response('dunno')

                c = memory.ideas[c]
                got_xxx = c.rel2xxx.get(Relation.AGENT)
                if not got_xxx:
                    return Reponse('dunno')

                if len(got_xxx) != 1:
                    return Response('todo')
                got_xx, = got_xxx

                rr = []
                for x in got_xx:
                    n = memory.ideas[x]
                    if not isinstance(n, Noun):
                        rr.append('not_a_noun')
                    elif n.name:
                        rr.append(' '.join(n.name))
                    elif n.kind:
                        rr.append(n.kind)
                    else:
                        assert False
                return Response(','.join(rr))
            else:
                return None
        else:
            return None
예제 #18
0
파일: be.py 프로젝트: knighton/babi
class AgentTargetQuestion(ClauseMeaning):
    def __init__(self):
        self.purpose = Purpose.WH_Q
        self.lemmas = BE_LEMMAS
        self.signatures = [
            [Relation.AGENT, Relation.TARGET],
        ]

    def handle(self, c, memory, (agent_xx, target_xx)):
        if len(agent_xx) != 1:
            return None

        if len(target_xx) != 1:
            return None

        agent_x, = agent_xx
        agent = memory.ideas[agent_x]
        target_x, = target_xx
        target = memory.ideas[target_x]
        if isinstance(agent, Direction) and isinstance(target, Noun):
            if target.query == Query.IDENTITY:
                direction = get_direction(agent.which)
                xx = memory.graph.look_toward_direction(agent.of_x, direction)
                rr = []
                for x in xx:
                    idea = memory.ideas[x]
                    if idea.name:
                        r = ' '.join(idea.name)
                    elif idea.kind:
                        r = idea.kind
                    else:
                        return None
                    rr.append(r)

                if not rr:
                    return Response('nothing')

                r = ','.join(rr)
                return Response(r)
            else:
                return None
        elif isinstance(agent, Noun) and isinstance(target, Direction):
            of = memory.ideas[target.of_x]
            if of.query == Query.IDENTITY:
                direction = get_direction(target.which)
                xx = memory.graph.look_from_direction(agent_x, direction)
                rr = []
                for x in xx:
                    idea = memory.ideas[x]
                    if idea.name:
                        r = ' '.join(idea.name)
                    elif idea.kind:
                        r = idea.kind
                    else:
                        return None
                    rr.append(r)

                if not rr:
                    return Response('nothing')

                r = ','.join(rr)
                return Response(r)
            else:
                return None
        elif isinstance(agent, Noun) and isinstance(target, Noun):
            if not agent.query and target.query == Query.IDENTITY:
                if target.kind == 'color':
                    for s in agent.attributes:
                        if is_color(s):
                            return Response(s)

                    rr = []
                    features = NounFeatures(kind=agent.kind)
                    for n in memory.resolve_each_noun(features):
                        for s in n.attributes:
                            if is_color(s):
                                rr.append(s)

                    if rr:
                        s = majority_vote(rr)
                        return Response(s)

                    return Response('dunno')
                else:
                    return None
            else:
                return None
        else:
            return None
예제 #19
0
파일: give.py 프로젝트: knighton/babi
class ReceiveQuestion(ClauseMeaning):
    def __init__(self):
        self.purpose = Purpose.WH_Q
        self.lemmas = RECEIVE_LEMMAS
        self.signatures = [
            [Relation.AGENT, Relation.TARGET, Relation.FROM_SOURCE],
            [Relation.AGENT, Relation.TARGET, None],
        ]

    def handle(self, c, memory, (recv_xx, what_xx, source_xx)):
        if len(recv_xx) != 1:
            return None

        if source_xx and len(source_xx) != 1:
            return None

        receiver = memory.ideas[recv_xx[0]]
        what = memory.ideas[what_xx[0]]
        source = memory.ideas[source_xx[0]] if source_xx else None

        q_count = 0
        if receiver.query:
            q_count += 1
        if what.query:
            q_count += 1
        if source and source.query:
            q_count += 1

        if q_count != 1:
            return None

        # give_agent_xx = source_xx
        # give_target_xx = what_xx
        # give_to_recip_xx = recv_xx
        if receiver.query == Query.IDENTITY:
            give_rel2xx = {
                Relation.TARGET: what_xx,
            }
            if source:
                give_rel2xx[Relation.AGENT] = source_xx
            give_want_rel = Relation.TO_RECIPIENT
        elif what.query == Query.IDENTITY:
            give_rel2xx = {
                Relation.TO_RECIPIENT: recv_xx,
            }
            if source:
                give_rel2xx[Relation.AGENT] = source_xx
            give_want_rel = Relation.TARGET
        elif source and source.query == Query.IDENTITY:
            give_rel2xx = {
                Relation.TARGET: what_xx,
                Relation.TO_RECIPIENT: recv_xx,
            }
            give_want_rel = Relation.AGENT
        else:
            assert False

        features = ClauseFeatures(
            possible_lemmas=GIVE_LEMMAS, rel2xx=give_rel2xx)
        x = memory.resolve_one_clause(features)
        if x is None:
            return Response('dunno')

        c = memory.ideas[x]
        xxx = c.rel2xxx[give_want_rel]
        if len(xxx) != 1:
            return None
        xx, = xxx
        if len(xx) != 1:
            return None

        x, = xx
        n = memory.ideas[x]
        if n.name:
            return Response(' '.join(n.name))
        elif n.kind:
            return Response(n.kind)
        else:
            return Response('cat got my tongue')
예제 #20
0
파일: give.py 프로젝트: knighton/babi
class GiveQuestion(ClauseMeaning):
    def __init__(self):
        self.purpose = Purpose.WH_Q
        self.lemmas = GIVE_LEMMAS
        self.signatures = [
            [Relation.AGENT, Relation.TO_RECIPIENT, Relation.TARGET],
            [Relation.AGENT, None,                  Relation.TARGET],
        ]

    def handle(self, c, memory, (give_xx, recv_xx, what_xx)):
        if len(give_xx) != 1:
            return None

        if recv_xx and len(recv_xx) != 1:
            return None

        if len(what_xx) != 1:
            return None

        giver = memory.ideas[give_xx[0]]
        receiver = memory.ideas[recv_xx[0]] if recv_xx else None
        what = memory.ideas[what_xx[0]]

        q_count = int(bool(giver.query))
        if receiver:
            q_count += bool(receiver.query)
        q_count += bool(what.query)

        if q_count != 1:
            return None

        if giver.query == Query.IDENTITY:
            rel2xx = {
                Relation.TARGET: what_xx,
            }
            if receiver:
                rel2xx[Relation.TO_RECIPIENT] = recv_xx
            want_rel = Relation.AGENT
        elif receiver and receiver.query == Query.IDENTITY:
            rel2xx = {
                Relation.AGENT: give_xx,
                Relation.TARGET: what_xx,
            }
            want_rel = Relation.TO_RECIPIENT
        elif what.query == Query.IDENTITY:
            rel2xx = {
                Relation.AGENT: give_xx,
            }
            if receiver:
                rel2xx[Relation.TO_RECIPIENT] = recv_xx
            want_rel = Relation.TARGET
        else:
            assert False

        features = ClauseFeatures(possible_lemmas=self.lemmas, rel2xx=rel2xx)
        x = memory.resolve_one_clause(features)
        if x is None:
            return Response('dunno')

        c = memory.ideas[x]
        xxx = c.rel2xxx[want_rel]
        if len(xxx) != 1:
            return None
        xx, = xxx
        if len(xx) != 1:
            return None

        x, = xx
        n = memory.ideas[x]
        if n.name:
            return Response(' '.join(n.name))
        elif n.kind:
            return Response(n.kind)
        else:
            return Response('cat got my tongue')
예제 #21
0
파일: be.py 프로젝트: knighton/babi
    def handle_disjunctions(self, c, memory, (agent_xxx, place_xxx)):
        if len(agent_xxx) != 1:
            return None
        agent_xx, = agent_xxx

        for place_xx in place_xxx:
            if len(place_xx) != 1:
                return None
        place_xx = map(lambda xx: xx[0], place_xxx)

        for agent_x in agent_xx:
            agent = memory.ideas[agent_x]
            loc = AtOneOf(place_xx)
            agent.location_history.set_location(loc)

        return Response()


class AgentInQuestion(ClauseMeaning):
    def __init__(self):
        self.purpose = Purpose.TF_Q
        self.lemmas = BE_LEMMAS
        self.signatures = [
            [Relation.AGENT, Relation.IN],
        ]

    def handle(self, c, memory, (agent_xx, place_xx)):
        if len(agent_xx) != 1:
            return None

        if len(place_xx) != 1: