示例#1
0
    def __init__(self):
        super().__init__(output=True)
        self.vision = Vision(self)
        self.memory = Memory(self)
        self.audition = Audition(self)
        self.typing = Typing(Hands(self))

        def interpreter(words):
            if words[0] == 'read':
                sem = Item(isa='action', type='read', object=words[1])
                pointer = self.vision.find(isa='pointer')
                if pointer is not None:
                    self.vision.encode(pointer)
                    sem.set('x', pointer.x).set('y', pointer.y)
                return sem
            elif words[0] == 'done':
                return Item(isa='done')
            else:
                return Item(isa='action', type=words[0], object=words[1])

        self.language = Language(self)
        self.language.add_interpreter(interpreter)

        def executor(action, context):
            if action.type == 'read':
                query = Query(x=action.x, y=action.y)
                context.set(action.object, self.vision.find_and_encode(query))
            elif action.type == 'type':
                self.typing.type(context.get(action.object))

        self.instruction = Instruction(
            self, self.memory, self.audition, self.language)
        self.instruction.add_executor(executor)
示例#2
0
class ACEUndifferentiatedAgent(Agent):
    def __init__(self):
        """Initializes the agent"""
        super().__init__(output=True)
        self.memory = Memory(self)
        self.vision = Vision(self)
        self.audition = Audition(self)
        self.typing = Typing(Hands(self))

        self.language = Language(self)
        self.language.add_interpreter(self.interpret)

        self.instruction = Instruction(self, self.memory, self.audition,
                                       self.language)
        self.instruction.add_executor(self.execute)

    def _ace_to_owl(self, text):
        """Converts ACE instruction text to OWL XML using the web API"""
        url = 'http://attempto.ifi.uzh.ch/ws/ape/apews.perl'
        params = {
            'text': text,
            'guess': 'on',
            # 'solo': 'owlxml',
            'cdrs': 'on',
            'cowlxml': 'on',
        }
        data = parse.urlencode(params).encode()
        req = request.Request(url, parse.urlencode(params).encode())
        res = request.urlopen(req)
        xml_string = res.read().decode('utf-8')
        print(xml_string)
        xml = ElementTree.fromstring(xml_string)
        print(xml)
        # print(ElementTree.tostring(xml, encoding='utf8', method='xml'))

    def interpret(self, words):
        self._ace_to_owl(' '.join(words))
        if words[0] == 'read':
            sem = Item(isa='action', type='read', object=words[1])
            pointer = self.vision.find(isa='pointer')
            if pointer is not None:
                self.vision.encode(pointer)
                sem.set('x', pointer.x).set('y', pointer.y)
            return sem
        elif words[0] == 'done':
            return Item(isa='done')
        else:
            return Item(isa='action', type=words[0], object=words[1])

    def execute(self, action, context):
        if action.type == 'read':
            query = Query(x=action.x, y=action.y)
            context.set(action.object, self.vision.find_and_encode(query))
        elif action.type == 'type':
            self.typing.type(context.get(action.object))

    def run(self, time=300):
        goal = self.instruction.listen_and_learn()
        self.instruction.execute(goal)
示例#3
0
    def __init__(self):
        """Initializes the agent"""
        super().__init__(output=True)
        self.memory = Memory(self)
        self.vision = Vision(self)
        self.audition = Audition(self)
        self.typing = Typing(Hands(self))

        self.language = Language(self)
        self.language.add_interpreter(self.interpret)

        self.instruction = Instruction(self, self.memory, self.audition,
                                       self.language)
        self.instruction.add_executor(self.execute)
示例#4
0
    def test_instruction_read(self, output=False):
        agent = Agent(output=output)
        memory = Memory(agent)
        env = Environment()
        vision = Vision(agent, env.display)
        audition = Audition(agent, env.speakers)

        def interpreter(words):
            if words[0] == 'read':
                sem = Item(isa='action', type='read', object=words[1])
                pointer = vision.find(isa='pointer')
                if pointer is not None:
                    vision.encode(pointer)
                    sem.set('x', pointer.x).set('y', pointer.y)
                return sem
            elif words[0] == 'done':
                return Item(isa='done')
            else:
                return Item(isa='action', type=words[0], object=words[1])

        language = Language(agent)
        language.add_interpreter(interpreter)

        def executor(action, context):
            query = Query(x=action.x, y=action.y)
            context.set(action.object, vision.find_and_encode(query))

        instruction = Instruction(agent, memory, audition, language)
        instruction.add_executor(executor)

        equation = ['3', 'x', '/', '12', '=', '15', '/', '4']
        for i in range(0, len(equation)):
            env.display.add_text(50 + 50 * i, 50, equation[i])
        pointer = env.display.add(50, 50, 1, 1, 'pointer', 'pointer')

        speech = [
            'to solve', ['read a', (50, 50)], ['read A', (300, 50)], 'done'
        ]

        def thread():
            for line in speech:
                agent.wait(3.0)
                if isinstance(line, str):
                    audition.add(Aural(isa='speech'), line)
                else:
                    audition.add(Aural(isa='speech'), line[0])
                    loc = line[1]
                    pointer.move(loc[0], loc[1])

        agent.run_thread(thread)

        goal = instruction.listen_and_learn()
        self.assertEqual('solve', goal)

        context = instruction.execute(goal)
        self.assertEqual('3', context.a)
        self.assertEqual('15', context.A)

        agent.wait_for_all()
示例#5
0
    def test_instruction_type(self, output=False):
        agent = Agent(output=output)
        env = Environment()
        memory = Memory(agent)
        vision = Vision(agent, env.display)
        audition = Audition(agent, env.speakers)
        motor = Motor(agent, vision, env)

        def interpreter(words):
            if words[0] == 'read':
                sem = Item(isa='action', type='read', object=words[1])
                pointer = vision.find(isa='pointer')
                if pointer is not None:
                    vision.encode(pointer)
                    sem.set('x', pointer.x).set('y', pointer.y)
                return sem
            elif words[0] == 'done':
                return Item(isa='done')
            else:
                return Item(isa='action', type=words[0], object=words[1])

        language = Language(agent)
        language.add_interpreter(interpreter)

        def executor(action, context):
            if action.type == 'read':
                query = Query(x=action.x, y=action.y)
                context.set(action.object, vision.find_and_encode(query))
            elif action.type == 'type':
                motor.type(context.get(action.object))

        instruction = Instruction(agent, memory, audition, language)
        instruction.add_executor(executor)

        typed = []

        def type_handler(key):
            typed.append(key)

        env.keyboard.add_type_fn(type_handler)

        env.display.add_text(50, 50, 'a')
        pointer = env.display.add(50, 50, 1, 1, 'pointer', 'pointer')

        speech = ['to type', ['read letter', (50, 50)], 'type letter', 'done']

        def thread():
            for line in speech:
                agent.wait(3.0)
                if isinstance(line, str):
                    audition.add(Aural(isa='speech'), line)
                else:
                    audition.add(Aural(isa='speech'), line[0])
                    loc = line[1]
                    pointer.move(loc[0], loc[1])

        agent.run_thread(thread)

        goal = instruction.listen_and_learn()
        self.assertEqual('type', goal)

        context = instruction.execute(goal)
        self.assertEqual('a', context.letter)

        agent.wait_for_all()

        self.assertEqual(['a'], typed)