Responsive behavior: providing metaknowledge


This is a post that I have been avoiding for awhile because of it’s inherit complexity. The main issue to solve here is, given a query, what is the appropriate response? This area requires using metaknowledge in order to correctly access the right kind of procedural knowledge or the right kind of declarative knowledge. My favorite example of this is from Professor Hofstadter’s example in G√∂del, Escher, Bach: when you are asked how many people live in Chicago you refer to your declarative knowledge – i.e. wikipedia (or google) says 2.175 million. However, when you are asked how many chairs are in your house, you can’t look it up so you will picture your house in your mind and go through each room and count them – a procedure to acquire the knowledge.

So far we have talked about implementing declarative knowledge using WolframApi and the Wikipedia Api. Later on I will get to implementing a kind of procedural knowledge using vision to answer questions based on procedures on surroundings and memory. Now we have to give the AI a sort of metaknowledge about the query to determine which kind of knowledge to use.

Basically we will implement a large case-structure to probe what is in the input. The only trick I have discovered so far is to put “common” things last, so that they aren’t always selected immediately in the case-structure.

Here is the basic code:

def processInput(s):
	s = s.lower()
	print "You entered %s" % (s)
	rsp = ""
	language="en"
	if "spanish" in s:
		language = 'es'
		try:
			rsp = translateText(s,language)
		except:
			language = 'en'
			print "Unexpected error:", sys.exc_info()[0]
			rsp = 'Language services not accessible at this time'
	elif "german" in s:
		language = 'de'
		try:
			rsp = translateText(s,language)
		except:
			language = 'en'
			rsp = 'Language services not accessible at this time'
	elif "italian" in s:
		language = 'it'
		try:
			rsp = translateText(s,language)
		except:
			language = 'en'
			rsp = 'Language services not accessible at this time'
	elif "weather" in s:
		result = pywapi.get_weather_from_noaa('KRDU') # RDU
		rsp = 'It is currently ' + str(int(float(result['temp_f']))) + ' degrees and ' + result['weather']
	elif ("i doing" in s) or (("to do" in s) and ("have" in s or "need" in s)):
		if "tomorrow" in s or "week" in s or "next" in s:
			rsp = getTasksToday(1)
			print "Got response " + rsp
		else:
			rsp = getTasksToday(0)
			print "Got response " + rsp
	elif "play" in s:
		rsp = playMusic(s)
	elif "music" in s:
		if "stop" in s:
			os.system('pkill vlc')
		elif "cancel" in s:
			os.system('pkill vlc')
		elif "kill" in s:
			os.system('pkill vlc')
		elif "close" in s:
			os.system('pkill vlc')
	elif "what is the" in s or "what's the" in s:
		rsp = wolframLookUp(s)
		if "Sorry" in rsp:
			rsp = getAIresponse(s)
	elif "how many" in s:
		rsp = wolframLookUp(s)
		if "Sorry" in rsp:
			rsp = getAIresponse(s)
	elif "tell me" in s:
		if "lot" in s:
			try:
				rsp = wikipediaLookUp(s,2)
			except:
				try:
					rsp = wikipediaLookUp(s,2)
				except:
					rsp = "I am sorry, I can not access that information."
		else:
			try:
				rsp = wikipediaLookUp(s,2)
			except:
				try:
					rsp = wikipediaLookUp(s,2)
				except:
					rsp = "I am sorry, I can not access that information."
	elif "do you know" in s:
		try:
			rsp = wikipediaLookUp(s,2)
		except:
			try:
				rsp = wikipediaLookUp(s,2)
			except:
				rsp = "I am sorry, I can not access that information."
	elif "who am i" in s or "who do you see" in s or "do you recognize" in s or "do you know me" in s:
		config.lookForFaces = 1
		rsp = "Let me see you and think."
		config.gettingStillImages = 0
	elif "off" in s and "light" in s:
		os.system("echo 'rf a1 off' | nc localhost 1099")
		os.system("echo 'rf c1 off' | nc localhost 1099")
		rsp = "Turning off the lights"
	elif "on" in s and "light" in s:
		os.system("echo 'rf a1 on' | nc localhost 1099")
		os.system("echo 'rf c1 on' | nc localhost 1099")
		rsp = "Turning on the lights"
	elif "who" in s:
		try:
			rsp = wikipediaLookUp(s,1)
		except:
			try:
				rsp = wikipediaLookUp(s,1)
			except:
				rsp = "I am sorry, I can not access that information."
	elif "shut" in s and "down" in s:
		saySomething("Shutting the computer down","en")
		os.system("sudo shutdown now &")
	else:
		rsp = getAIresponse(s)
	print rsp

	pattern = re.compile("([^a-zA-Z\d\s:,.']|_)+")
	rsp = re.sub(pattern, '', rsp)
	print rsp + " in " + language
	saySomething(rsp,language)

There is a lot here, but I’m sure it’s mostly self-explanatory. The bits about turning on/off lights and checking the weather and playing music I will get to soon. As you can see I have implemented some bad coding – nesting try/except statements. I found that sometimes the API wouldn’t work (maybe 7% of the time) so this will allow it to retry if it has trouble.

I don’t really like this code because it is long and not so elegant in terms of it’s organization. Please feel free to comment and suggest ways to improve!

Advertisements

2 comments

  1. Have you considered the following options for looking for answers – going through a folder of files, or DB with lots of text, which can be scanned as well? What about adding Q&A teaching element, so for example, i can login as teacher, and teach the AI some basic questions and answers to IT, thus shortening the cases to be looked in or if that information is specifcaly related to a certain area of knowledge, e.g. management skills, presentation, excel skills and etc?

    1. Yes! I’ve thought about this, but in a slightly different context. I have my own personal chat history from Google / Cell phone records / etc., and I was working on a program that would utilize these questions/answers to provide a AI knowledge (about my personal life). This would be a tiny step for putting one owns life into a computer…
      I think your idea may be more practical and also maybe somewhat easier! I’m sure IT centers get the same 10 questions over and over in different variations, which is great for machine learning.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: