Пример #1
0
    def costFunction(self, params):
        """Returns Error as a function of Lambda Params.

		Args:
			params: List of doubles, represents different lambda values,
					lambda_phi is the first param; lambda_pi's are the next
					6 parameters

		Returns:
			Cost - Integer representing number of misclassified results in the 
				   training set.

		"""

        # Set Lambda Phi & Pi based upon params
        # 1st Param - Lambda Phi
        # 2nd Param - Lambda Pi
        self.lambda_phi = params[0]
        self.lambda_pi = map(lambda x: x / sum(params[1:]), params[1:])

        # Train the classifier
        self.train()

        # Calculate Error as
        # Number of Training Errors
        error = 0
        for (ques, ans) in self.training_set:

            res = self.get_classification(ques)
            pred_ans = ut.clean(ut.key_max_val_dict(res))

            if pred_ans != ans:
                error += 1

        return error
Пример #2
0
	def get_reply (self, string):
		
		self.trn = self.trn_1
		self.threshold = self.threshold_1
		self.tmap = self.training_map_1
		
		#print self.lambda_pi_1
	
		# Get the classification for 'string'
		res = self.trn.get_classification(string)
		
		# Get the value, reply of highest ranked response
		value = res[ut.key_max_val_dict(res)]
		reply = ut.key_max_val_dict(res)
		response_prior = ''

		# Check if it crosses minimum threshold
		if value >= self.threshold:
			# Check if it has been repeated in history
			# and respond appropriately
			"""
			if reply in self.history:
				response_prior = 'I\'ll repeat myself, '
				#reply = 'I\'ll repeat myself, '+ choice(self.tmap.get(reply, [reply]))
				if response_prior + reply in self.history:
					response_prior = 'I am tired of this conversation, but ' + response_prior
					if response_prior + reply in self.history:
						response_prior = ''
			"""
			# Put the string in history
			self.history.append (response_prior + reply)
			reply = response_prior + choice(self.tmap.get(reply, [reply]))
			return reply

		# In case responses are below the threshold
		else:
			reply = ' I don\'t know anything about this'
			if reply in self.history:
				reply = 'Ask me something else'

			self.history.append(reply)
			return reply
Пример #3
0
    def get_reply(self, string):

        self.trn = self.trn_1
        self.threshold = self.threshold_1
        self.tmap = self.training_map_1

        #print self.lambda_pi_1

        # Get the classification for 'string'
        res = self.trn.get_classification(string)

        # Get the value, reply of highest ranked response
        value = res[ut.key_max_val_dict(res)]
        reply = ut.key_max_val_dict(res)
        response_prior = ''

        # Check if it crosses minimum threshold
        if value >= self.threshold:
            # Check if it has been repeated in history
            # and respond appropriately
            """
			if reply in self.history:
				response_prior = 'I\'ll repeat myself, '
				#reply = 'I\'ll repeat myself, '+ choice(self.tmap.get(reply, [reply]))
				if response_prior + reply in self.history:
					response_prior = 'I am tired of this conversation, but ' + response_prior
					if response_prior + reply in self.history:
						response_prior = ''
			"""
            # Put the string in history
            self.history.append(response_prior + reply)
            reply = response_prior + choice(self.tmap.get(reply, [reply]))
            return reply

        # In case responses are below the threshold
        else:
            reply = ' I don\'t know anything about this'
            if reply in self.history:
                reply = 'Ask me something else'

            self.history.append(reply)
            return reply
Пример #4
0
	def costFunction (self, params):
		self.lambda_phi = params[0]
		self.lambda_pi = map(lambda x: x/sum(params[1:]), params[1:] )
		#print self.lambda_pi

		# Train the classifier
		self.train()

		# Calculate Error
		error = 0.0
		for (ques,ans) in self.training_set:
			res = self.get_classification(ques)
			pred_ans = ut.clean(ut.key_max_val_dict (res))
			# Add one for every misclassified result
			if(pred_ans != ans):
#				print pred_ans, ans
#				raw_input()
				error += 1

		return error
Пример #5
0
    def costFunction(self, params):

        self.lambda_pi = map(lambda x: x / sum(params), params)
        #print self.lambda_pi

        # Train the classifier
        self.train()

        # Calculate Error
        error = 0.0
        for (ques, ans) in self.training_set:
            res = self.get_classification(ques)
            pred_ans = ut.clean(ut.key_max_val_dict(res))
            # Add one for every misclassified result
            if (pred_ans != ans):
                #				print pred_ans, ans
                #				raw_input()
                error += 1

        return error
Пример #6
0
    def get_reply(self, string, annotation):
        """Performs classification on the given text, based upon annotations, history and relevance and
		   gives an appropriate response.

		Args:
			string : The string to be classified
			annotation : Chooses from two different datasets based upon this value

		Returns:
			A Final Response String to be supplied to the user.

		"""

        # If Annotation is 1; Then limit conversation length to 25 interactions;
        # Specific to this situation
        self.conversation_count += 1
        if self.conversation_count > 25 and int(annotation) == 1:
            return "I am irritated now, Talk to me Later"

        if int(annotation) == 1:
            self.trn = self.trn_1
            self.threshold = self.threshold_1
            self.tmap = self.training_map_1
        else:
            self.trn = self.trn_2
            self.threshold = self.threshold_2
            self.tmap = self.training_map_2

        if self.trn is not self.trn_1:
            print annotation

            # Get the classification for 'string'
        res = self.trn.get_classification(string)

        # Get the value, reply of highest ranked response
        value = res[ut.key_max_val_dict(res)]
        reply = ut.key_max_val_dict(res)

        response_prior = ""

        # Check if it crosses minimum threshold
        if value >= self.threshold:

            # Check if it has been repeated in history
            # and respond appropriately
            if reply in self.history:
                response_prior = "I'll repeat myself, "

                if response_prior + reply in self.history:
                    response_prior = "I am tired of this conversation, but " + response_prior
                    if response_prior + reply in self.history:
                        response_prior = ""

                        # Put the string in history
            self.history.append(response_prior + reply)
            reply = response_prior + choice(self.tmap.get(reply, [reply]))
            return reply

            # In case responses are below the threshold
        else:
            reply = " I don't know anything about this"
            if reply in self.history:
                reply = "Ask me something else or leave me alone"

            self.history.append(reply)
            return reply