def add(self, key, v): # Perform checks if (isinstance(v, HRR)): self.hrr[key] = v self.keys.append(key) if self.vectors is None: self.vectors = numeric.array([self.hrr[key].v]) else: self.vectors = numeric.resize( self.vectors, (len(self.keys), self.dimensions)) self.vectors[-1, :] = self.hrr[key].v # Generate vector pairs if (self.include_pairs or self.vector_pairs is not None): for k in self.keys[:-1]: self.key_pairs.append('%s*%s' % (k, key)) v = (self.hrr[k] * self.hrr[key]).v if self.vector_pairs is None: self.vector_pairs = numeric.array([v]) else: self.vector_pairs = numeric.resize( self.vector_pairs, (len(self.key_pairs), self.dimensions)) self.vector_pairs[-1, :] = v else: raise TypeError( 'hrr.Vocabulary.add() Type error: Argument provided not of HRR type' )
def add(self,key,v): # Perform checks if(isinstance(v,HRR)): self.hrr[key] = v self.keys.append(key) if self.vectors is None: self.vectors=numeric.array([self.hrr[key].v]) else: self.vectors=numeric.resize(self.vectors,(len(self.keys),self.dimensions)) self.vectors[-1,:]=self.hrr[key].v # Generate vector pairs if(self.include_pairs or self.vector_pairs is not None): for k in self.keys[:-1]: self.key_pairs.append('%s*%s'%(k,key)) v=(self.hrr[k]*self.hrr[key]).v if self.vector_pairs is None: self.vector_pairs=numeric.array([v]) else: self.vector_pairs=numeric.resize(self.vector_pairs,(len(self.key_pairs),self.dimensions)) self.vector_pairs[-1,:]=v else: raise TypeError('hrr.Vocabulary.add() Type error: Argument provided not of HRR type')
def generate_pairs(self): """ This function is intended to be used in situations where a vocabulary has already been created without including pairs, but it becomes necessary to have the pairs (for graphing in interactive plots, for example). This is essentially identical to the add function above, except that it makes all the pairs in one pass (and without adding new vectors). """ self.key_pairs = [] self.vector_pairs = None for i in range(1, len(self.keys)): for k in self.keys[:i]: key = self.keys[i] self.key_pairs.append('%s*%s'%(k,key)) v=(self.hrr[k]*self.hrr[key]).v if self.vector_pairs is None: self.vector_pairs=numeric.array([v]) else: self.vector_pairs=numeric.resize(self.vector_pairs,(len(self.key_pairs),self.dimensions)) self.vector_pairs[-1,:]=v