示例#1
0
 def act(self, seq):
     # logging.info(
     #    "appending machines {0} and {1} to new binary {2}".format(
     #        seq[self.first_pos], seq[self.second_pos], self.bin_rel))
     rel_machine = Machine(self.bin_rel)
     rel_machine.append(seq[self.first_pos], 1)
     rel_machine.append(seq[self.second_pos], 2)
     return [rel_machine]
示例#2
0
 def act(self, seq):
     # logging.info(
     #    "appending machines {0} and {1} to new binary {2}".format(
     #        seq[self.first_pos], seq[self.second_pos], self.bin_rel))
     rel_machine = Machine(self.bin_rel)
     rel_machine.append(seq[self.first_pos], 1)
     rel_machine.append(seq[self.second_pos], 2)
     return [rel_machine]
示例#3
0
def test():
    #a = Machine("the", PosControl("DET"))
    #kek = Machine("kek", PosControl("ADJ"))
    #kockat = Machine("kockat", PosControl("NOUN<CAS<ACC>>"))
    m = Machine("vonat")
    m2 = Machine("tb")
    m.append(m2)
    m2.append(m)
    m3 = copy(m)
    assert m3
示例#4
0
def test():
    #a = Machine("the", PosControl("DET"))
    #kek = Machine("kek", PosControl("ADJ"))
    #kockat = Machine("kockat", PosControl("NOUN<CAS<ACC>>"))
    m = Machine("vonat")
    m2 = Machine("tb")
    m.append(m2)
    m2.append(m)
    m3 = copy(m)
    assert m3
示例#5
0
    def create_machine(self, name, partitions):
        # lists are accepted because of ["=", "AGT"]
        if type(name) is list:
            name = "".join(name)

        # HACK until we find a good solution for defaults
        name = name.strip('<>')

        is_plur = name in self.plur_dict
        if is_plur:
            name = self.plur_dict[name]

        m = Machine(decode_from_proszeky(name),
                    ConceptControl(), partitions)
        if is_plur:
            m.append(self.create_machine('more', 1), 0)

        return m
示例#6
0
    def create_machine(self, name, partitions):
        # lists are accepted because of ["=", "AGT"]
        if type(name) is list:
            name = "".join(name)

        # HACK until we find a good solution for defaults
        name = name.strip('<>')

        is_plur = name in self.plur_dict
        if is_plur:
            name = self.plur_dict[name]

        m = Machine(decode_from_proszeky(name),
                    ConceptControl(), partitions)
        if is_plur:
            m.append(self.create_machine('more', 1), 0)

        return m
示例#7
0
    def unify_recursively(self, static_machine, zeros_only, first=False,
                          stop=None):
        """Returns the active machine that corresponds to @p static_machine. It
        recursively unifies all machines in all partitions of @p static_machine
        with machines in the active set. @p static_machine may be either a
        machine or a string.
        @param stop the set of machines already unified."""
        if stop is None:
            stop = set()

        if unicode(static_machine) == u'IS_A':
            return None
        # If we have already unified this machine: just return
        if (not isinstance(static_machine, str) and
                not isinstance(static_machine, unicode)):
            static_printname = static_machine.printname()
        else:
            static_printname = static_machine
        if static_printname in stop:
            #logging.debug('ur stops')
            return self.active[static_printname].keys()[0]
        #If static_machine is a string, we don't have much to do
        #logging.debug('ur static_machine {0}, type: {1}'.format(
        #   str(static_machine), str(type(static_machine))))
        if isinstance(static_machine, str):
            if static_machine in self.active:
                # FIXME: [0] is a hack, fix it
                #logging.debug('ur str in active')
                return self.active[static_machine].keys()[0]
            else:
                if static_machine.startswith('#'):
                    #logging.debug('ur waking up')
                    self.wake_avm_construction(static_machine)
                    return None
                #logging.debug('ur activating str')
                active_machine = Machine(static_machine, ConceptControl())
                self.__add_active_machine(active_machine)
                return active_machine
        # If it's a machine, we create the corresponding active one
        elif isinstance(static_machine, Machine):
            static_name = static_machine.printname()
            #logging.debug('Does {0} start with #? {1}'.format(
            #   static_name, static_name.startswith('#')))

            if static_name in self.active:
                #logging.debug('ur machine in active')
                active_machine = self.active[static_name].keys()[0]
            else:
                #logging.debug('Not in active')
                if static_name.startswith('#'):
                    #logging.debug('ur waking up')
                    self.wake_avm_construction(static_name)
                    return None
                #logging.debug('ur activating machine')
                active_machine = Machine(static_name)
                active_control = copy.copy(static_machine.control)
                #active_control = copy.deepcopy(static_machine.control)
                #deepcopy causes infinite recursion, I hope shallow copy
                #works, since the active machine will update the control's
                #machine attribute (and we don't know of anything else)
                active_machine.set_control(active_control)
                self.__add_active_machine(active_machine)

            stop.add(static_name)

            # Now we have to walk through the tree recursively
            for i, part in enumerate(static_machine.partitions):
                for ss_machine in part:
                    as_machine = self.unify_recursively(
                        ss_machine, zeros_only, first=False, stop=stop)
                    if as_machine is not None:
                        #logging.info('adding {} to part {} of {}'.format(
                        #    as_machine, i, active_machine))
                        active_machine.append(as_machine, i)
            return active_machine
        else:
            raise TypeError('static_machine must be a Machine or a str')
示例#8
0
    def unify_recursively(self,
                          static_machine,
                          zeros_only,
                          first=False,
                          stop=None):
        """Returns the active machine that corresponds to @p static_machine. It
        recursively unifies all machines in all partitions of @p static_machine
        with machines in the active set. @p static_machine may be either a
        machine or a string.
        @param stop the set of machines already unified."""
        if stop is None:
            stop = set()

        if unicode(static_machine) == u'IS_A':
            return None
        # If we have already unified this machine: just return
        if (not isinstance(static_machine, str)
                and not isinstance(static_machine, unicode)):
            static_printname = static_machine.printname()
        else:
            static_printname = static_machine
        if static_printname in stop:
            #logging.debug('ur stops')
            return self.active[static_printname].keys()[0]
        #If static_machine is a string, we don't have much to do
        #logging.debug('ur static_machine {0}, type: {1}'.format(
        #   str(static_machine), str(type(static_machine))))
        if isinstance(static_machine, str):
            if static_machine in self.active:
                # FIXME: [0] is a hack, fix it
                #logging.debug('ur str in active')
                return self.active[static_machine].keys()[0]
            else:
                if static_machine.startswith('#'):
                    #logging.debug('ur waking up')
                    self.wake_avm_construction(static_machine)
                    return None
                #logging.debug('ur activating str')
                active_machine = Machine(static_machine, ConceptControl())
                self.__add_active_machine(active_machine)
                return active_machine
        # If it's a machine, we create the corresponding active one
        elif isinstance(static_machine, Machine):
            static_name = static_machine.printname()
            #logging.debug('Does {0} start with #? {1}'.format(
            #   static_name, static_name.startswith('#')))

            if static_name in self.active:
                #logging.debug('ur machine in active')
                active_machine = self.active[static_name].keys()[0]
            else:
                #logging.debug('Not in active')
                if static_name.startswith('#'):
                    #logging.debug('ur waking up')
                    self.wake_avm_construction(static_name)
                    return None
                #logging.debug('ur activating machine')
                active_machine = Machine(static_name)
                active_control = copy.copy(static_machine.control)
                #active_control = copy.deepcopy(static_machine.control)
                #deepcopy causes infinite recursion, I hope shallow copy
                #works, since the active machine will update the control's
                #machine attribute (and we don't know of anything else)
                active_machine.set_control(active_control)
                self.__add_active_machine(active_machine)

            stop.add(static_name)

            # Now we have to walk through the tree recursively
            for i, part in enumerate(static_machine.partitions):
                for ss_machine in part:
                    as_machine = self.unify_recursively(ss_machine,
                                                        zeros_only,
                                                        first=False,
                                                        stop=stop)
                    if as_machine is not None:
                        #logging.info('adding {} to part {} of {}'.format(
                        #    as_machine, i, active_machine))
                        active_machine.append(as_machine, i)
            return active_machine
        else:
            raise TypeError('static_machine must be a Machine or a str')