#!/usr/bin/env python #Author: Shriphani Palakodety #Mail: spalakod@cs.purdue.edu #Mail2: shriphani@shriphani.com #Hebb's Rule implementation. #Each neuron has 3 attributes: #Activation = 1 or 0. #Data = A letter of the alphabet in this case #Weight with respect to other cells. #Initiall weights are 0 import string import copy class Neuron: def __init__(self, a, weight_dict): '''A Represents The Cell's Content''' self.content = a #what letter of the alphabet a cell represents self.weights = weight_dict #only contains contents of each neuron. self.state = 0 #activation state initially on. def __str__(self): return "Activation: "+str(self.state)+"\nWeights: "+str(self.weights) class Network: def __init__(self): self.neurons = [] self.flush = {} #weight calculator while updating activities self.possibilities = [] for char in string.uppercase: self.flush[char] = 0 for letter in string.uppercase: weights = copy.copy(self.flush) weights.pop(letter) a = Neuron(letter, weights) self.neurons.append(a) def recvInput(self): a = raw_input("Sensory Interface: ") #Get a word while a != "QUIT": b = list(a) for i in xrange(len(b)): for neuron in self.neurons: char = b[i] if neuron.content == char: neuron.state = 1 reqd_list = b[0:i]+b[i+1:] for char in string.uppercase: if char in reqd_list: neuron.weights[char]+=1 else: if neuron.weights.has_key(char): neuron.weights[char]-=1 else: continue self.checkInfo() self.possibleInfo() print "The possible interpretations of this input might be anagrams of the subsets of: \n" print self.possibilities a = raw_input("Sensory Interface: ") def checkInfo(self): output = [] possibilities = {} for neuron in self.neurons: if neuron.state == 1: for info in neuron.weights.keys(): self.flush[info] += neuron.weights[info] print self.flush for neuron in self.neurons: if self.flush[neuron.content] > 0: neuron.state = 1 else: neuron.state = 0 for char in self.flush.keys(): self.flush[char] = 0 def possibleInfo(self): self.possibilities = [] for neuron in self.neurons: if neuron.state == 1: self.possibilities.append(neuron.content) words = Network() words.recvInput()