Exemplo n.º 1
0
    def resonance(self, chunk):
        self.busy = True
        if self.error: self.error = False
        self._request_count += 1

        # convert chunk to string (if it isn't already a string)
        chunk = self.chunk2str(chunk)
        # assign any unassigned values in chunk string
        chunk = self.assignValues(chunk)

        if '?' in chunk:
            raise Exception(
                "Use the resonanuce function when the chunk has no '?'. If there is a '?' use request instead"
            )

        # keep track of the number of occurrences of a particular value in case of repeats
        occurrences = {}
        # keep a running sum of the cosines and a count of the values in the chunk
        sumOfCosines = 0
        numOfValues = 0
        # perform a query for each value in chunk
        for slotvalue in chunk.split():
            # create a query by removing the value and replacing it with '?'
            query = chunk.split()  # turn chunk into list
            query.pop(numOfValues)  # remove this list item
            # check if chunk has slots by checking for colons (which separate slots from values)
            if ':' in slotvalue:
                slot, value = slotvalue.split(':')
                query.insert(numOfValues, slot + ':?')  # replace value with ?
                query = ' '.join(query)  # convert query to a string
                queryVec, queryStr = self.queryWithSlots(query)
            else:
                value = slotvalue
                query.insert(numOfValues, '?')  # replace value with ?
                query = ' '.join(query)  # convert query to a string
                queryVec, queryStr = self.queryJustValues(query)
            numOfValues = numOfValues + 1

            # find the match between the query vector and the value's memory vector
            match = self.memory[value].compare(queryVec)
            sumOfCosines = sumOfCosines + match
            if self.verbose:
                print 'The query is ' + queryStr
                print 'Match is ' + str(
                    match) + ' for ' + value + ' = ' + self.memStr[value]
        coherence = sumOfCosines / numOfValues
        if self.verbose:
            print 'The coherence is ' + str(coherence)
        if coherence <= self.threshold:
            self.fail(self._request_count)
        else:
            chunkObj = Chunk(chunk)
            chunkObj.activation = coherence
            self.recall(chunkObj,
                        matches=[],
                        request_number=self._request_count)
Exemplo n.º 2
0
  def resonance(self,chunk):
     self.busy=True
     if self.error: self.error=False
     self._request_count+=1


     # convert chunk to string (if it isn't already a string)
     chunk = self.chunk2str(chunk)
     # assign any unassigned values in chunk string
     chunk = self.assignValues(chunk)

     if '?' in chunk:
        raise Exception("Use the resonanuce function when the chunk has no '?'. If there is a '?' use request instead")
          
     # keep track of the number of occurrences of a particular value in case of repeats
     occurrences = {}
     # keep a running sum of the cosines and a count of the values in the chunk
     sumOfCosines = 0;
     numOfValues  = 0; 
     # perform a query for each value in chunk
     for slotvalue in chunk.split():
        # create a query by removing the value and replacing it with '?'
        query = chunk.split() # turn chunk into list
        query.pop(numOfValues) # remove this list item
        # check if chunk has slots by checking for colons (which separate slots from values)
        if ':' in slotvalue:
            slot,value = slotvalue.split(':')
            query.insert(numOfValues, slot+':?') # replace value with ?
            query = ' '.join(query) # convert query to a string
            queryVec, queryStr = self.queryWithSlots(query)
        else:
            value = slotvalue
            query.insert(numOfValues, '?') # replace value with ?
            query = ' '.join(query) # convert query to a string
            queryVec, queryStr = self.queryJustValues(query)
        numOfValues = numOfValues + 1;

        # find the match between the query vector and the value's memory vector
        match = self.memory[value].compare(queryVec)
        sumOfCosines = sumOfCosines + match
        if self.verbose:
            print 'The query is ' + queryStr
            print 'Match is ' + str(match) + ' for ' + value + ' = ' + self.memStr[value]
     coherence = sumOfCosines / numOfValues
     if self.verbose:
        print 'The coherence is ' + str(coherence)
     if coherence <= self.threshold:
        self.fail(self._request_count)
     else:
         chunkObj = Chunk(chunk)
         chunkObj.activation = coherence
         self.recall(chunkObj,matches=[],request_number=self._request_count)     
Exemplo n.º 3
0
 def resonance(self,chunk):
    if '?' in chunk:
       print 'chunk is ' + chunk
       raise Exception("Use the resonance function when the chunk has no '?'. If there is a '?' use request instead")
         
    coherence = self.get_activation(chunk)
    if self.verbose:
       print 'The coherence is ' + str(coherence)
    if coherence <= self.threshold:
       self.fail(self._request_count)
    else:
        chunkObj = Chunk(chunk)
        chunkObj.activation = coherence
        self.recall(chunkObj,matches=[],request_number=self._request_count)     
Exemplo n.º 4
0
    def request(self, chunk):
        self.busy = True
        if self.error: self.error = False
        self._request_count += 1

        # convert chunk to string (if it isn't already a string)
        chunk = self.chunk2str(chunk)
        # assign any unassigned values in chunk string
        chunk = self.assignValues(chunk)

        # check if chunk has slots by checking for colons (which separate slots from values)
        if ':' in chunk:
            queryVec, queryStr = self.queryWithSlots(chunk)
        else:
            queryVec, queryStr = self.queryJustValues(chunk)

        if self.verbose:
            print 'The query is ' + queryStr
        highestCosine = self.threshold
        bestMatch = 'none'
        # find the best match to the query vector in memory
        for mem, memVec in self.memory.items():
            thisCosine = memVec.compare(queryVec)
            if self.verbose:
                print mem, thisCosine
            if thisCosine > highestCosine:
                highestCosine = thisCosine
                bestMatch = mem
        if self.verbose:
            print bestMatch

        if bestMatch == 'none':
            self.fail(self._request_count)
        else:
            # replace the placeholder '?' with the retrieved memory 'bestMatch'
            chunk = chunk.replace('?', bestMatch)
            if self.verbose:
                print 'Best match is ' + bestMatch + ' = ' + self.memStr[
                    bestMatch]
                print 'output chunk = ' + chunk
            chunkObj = Chunk(chunk)
            chunkObj.activation = highestCosine
            self.recall(chunkObj,
                        matches=[],
                        request_number=self._request_count)
Exemplo n.º 5
0
    def resonance(self, chunk):
        if '?' in chunk:
            print('chunk is ' + chunk)
            raise Exception(
                "Use the resonance function when the chunk has no '?'. If there is a '?' use request instead"
            )

        coherence = self.get_activation(chunk)
        if self.verbose:
            print('The coherence is ' + str(coherence))
        if coherence <= self.threshold:
            self.fail(self._request_count)
        else:
            chunkObj = Chunk(chunk)
            chunkObj.activation = coherence
            self.recall(chunkObj,
                        matches=[],
                        request_number=self._request_count)
Exemplo n.º 6
0
    def requestValue(self, chunk, require_new=False):
        # check if chunk has slots by checking for colons (which separate slots from values)
        if ':' in chunk:
            queryVec = self.queryWithSlots(chunk)
        else:
            queryVec = self.queryJustValues(chunk)

        highestCosine = self.threshold
        bestMatch = 'none'
        if self.verbose:
            print('Query is: ' + chunk)
            print('inhibited values: ' + str(self.inhibited))
            print('Finst contains: ' + str(self.finst.obj))
        # find the best match to the query vector in memory
        for mem, memVec in self.mem.items():
            # skip inhibited values
            if mem not in self.inhibited:
                # skip previously reported values if require_new is true
                if (not require_new) or (not self.finst.contains(mem)):
                    thisCosine = memVec.compare(queryVec)
                    if self.verbose:
                        print(mem, thisCosine)
                    if thisCosine > highestCosine:
                        highestCosine = thisCosine
                        bestMatch = mem

        if bestMatch == 'none':
            if self.verbose:
                print('No matches found above threshold of cosine =',
                      self.threshold)
            self.fail(self._request_count)
        else:
            # replace the placeholder '?' with the retrieved memory 'bestMatch'
            chunk = chunk.replace('?', bestMatch)
            if self.verbose:
                print('Best match is ' + bestMatch)
                print('with a cosine of ' + str(highestCosine))
                print('output chunk = ' + chunk)
            chunkObj = Chunk(chunk)
            chunkObj.activation = highestCosine
            self.finst.add(bestMatch)
            self.recall(chunkObj,
                        matches=[],
                        request_number=self._request_count)
Exemplo n.º 7
0
  def request(self,chunk):
     self.busy=True
     if self.error: self.error=False
     self._request_count+=1

     
     # convert chunk to string (if it isn't already a string)
     chunk = self.chunk2str(chunk)
     # assign any unassigned values in chunk string
     chunk = self.assignValues(chunk)
     
     # check if chunk has slots by checking for colons (which separate slots from values)
     if ':' in chunk:
        queryVec, queryStr = self.queryWithSlots(chunk)
     else:
        queryVec, queryStr = self.queryJustValues(chunk)
     
     if self.verbose:
        print 'The query is ' + queryStr
     highestCosine = self.threshold
     bestMatch = 'none'
     # find the best match to the query vector in memory
     for mem,memVec in self.memory.items():
        thisCosine = memVec.compare(queryVec)
        if self.verbose:
            print mem, thisCosine
        if thisCosine > highestCosine:
            highestCosine = thisCosine 
            bestMatch = mem
     if self.verbose:
        print bestMatch

     if bestMatch == 'none':
        self.fail(self._request_count)
     else:
         # replace the placeholder '?' with the retrieved memory 'bestMatch'
         chunk = chunk.replace('?',bestMatch)
         if self.verbose:
            print 'Best match is ' + bestMatch + ' = ' + self.memStr[bestMatch]
            print 'output chunk = ' + chunk
         chunkObj = Chunk(chunk)
         chunkObj.activation = highestCosine
         self.recall(chunkObj,matches=[],request_number=self._request_count)
Exemplo n.º 8
0
  def requestValue(self,chunk,require_new=False):
     # check if chunk has slots by checking for colons (which separate slots from values)
     if ':' in chunk:
        queryVec = self.queryWithSlots(chunk)
     else:
        queryVec = self.queryJustValues(chunk)
     
     highestCosine = self.threshold
     bestMatch = 'none'
     if self.verbose:
        print 'Query is: ' + chunk
        print 'inhibited values: ' + str(self.inhibited)
        print 'Finst contains: ' + str(self.finst.obj)
     # find the best match to the query vector in memory
     for mem,memVec in self.mem.items():
        # skip inhibited values
        if mem not in self.inhibited:
            # skip previously reported values if require_new is true
            if (not require_new) or (not self.finst.contains(mem)):
                thisCosine = memVec.compare(queryVec)
                if self.verbose:
                    print mem, thisCosine
                if thisCosine > highestCosine:
                    highestCosine = thisCosine 
                    bestMatch = mem

     if bestMatch == 'none':
        if self.verbose:
            print 'No matches found above threshold of cosine =', self.threshold
        self.fail(self._request_count)
     else:
         # replace the placeholder '?' with the retrieved memory 'bestMatch'
         chunk = chunk.replace('?',bestMatch)
         if self.verbose:
            print 'Best match is ' + bestMatch
            print 'with a cosine of ' + str(highestCosine)
            print 'output chunk = ' + chunk
         chunkObj = Chunk(chunk)
         chunkObj.activation = highestCosine
         self.finst.add(bestMatch)
         self.recall(chunkObj,matches=[],request_number=self._request_count)
Exemplo n.º 9
0
    def recall(self, chunk, matches, request_number):
        keys = list(sorted(chunk.keys()))
        if not hasattr(chunk, '_blend_keys'):
            bk = []
            for k in keys:
                try:
                    v = float(chunk[k])
                    bk.append(k)
                except:
                    pass
            chunk._blend_keys = tuple(bk)
        bk = chunk._blend_keys
        if len(bk) > 0:
            a = chunk.activation
            chunk = Chunk(chunk)
            chunk.activation = a

            for b in bk:
                chunk[b] = 0
            total_activation = 0
            for m in matches:
                m.exp_activation = math.exp(m.activation)
                k = list(sorted(m.keys()))
                if k == keys:
                    blend = {}
                    try:
                        for b in bk:
                            blend[b] = float(m[b])
                    except ValueError:
                        continue
                    for b in bk:
                        chunk[b] += blend[b] * m.exp_activation
                    total_activation += m.exp_activation
            for b in bk:
                chunk[b] /= total_activation
        for m in Memory.recall(self, chunk, matches, request_number):
            yield m
Exemplo n.º 10
0
 def recall(self,chunk,matches,request_number):
   keys=list(sorted(chunk.keys()))
   if not hasattr(chunk,'_blend_keys'):
     bk=[]
     for k in keys:
       try:
         v=float(chunk[k])
         bk.append(k)
       except:
         pass
     chunk._blend_keys=tuple(bk)
   bk=chunk._blend_keys
   if len(bk)>0:
     a=chunk.activation
     chunk=Chunk(chunk)
     chunk.activation=a
     
     for b in bk: chunk[b]=0
     total_activation=0
     for m in matches:
       m.exp_activation=math.exp(m.activation)
       k=list(sorted(m.keys()))
       if k==keys:
         blend={}
         try:            
           for b in bk:
             blend[b]=float(m[b])
         except ValueError:
           continue
         for b in bk:
           chunk[b]+=blend[b]*m.exp_activation
         total_activation+=m.exp_activation
     for b in bk:
       chunk[b]/=total_activation
   for m in Memory.recall(self,chunk,matches,request_number):
     yield m