A little amusing Python program

Max M maxm at normik.dk
Wed Oct 3 07:30:21 EDT 2001


I have written this little Python program that usually raises a few
eyebrows. It answer multiple choice questions.

It is very simple. ie.

question = 'What is Python'
choices = (
               'a programming language',
               'a tv series',
               'a snake'
               )
It then queries google with:

"What is Python a programming language"
"What is Python a tv series"
"What is Python a snake"

and chooses the result that gets the most hits.

Simple, but far better then random.

Well it's not of much use, but kind of fun and very low budget AI ...

regards Max M

#######################################
from urllib import urlopen, urlencode
import re

class multiChoiceGuesser:

    def __init__(self, question='', replys=()):
        self.question = question
        self.replys   = replys

    def guessedAnswer(self):
        hits = []
        for reply in self.replys:
            hits.append(self._getGoogleHits(self.question + ' ' + reply))
        return hits.index(max(hits))

    def _getGoogleHits(self, query):
        query = urlencode({'q':query})
        urlHandle = urlopen('http://www.google.com/search?%s' % query)
        googlePage = urlHandle.read()
        try:
            numberAsString = re.search(
                'of about <b>(.*?)</b>.', googlePage, re.S
                ).group(1)
            hits = re.sub(',', '',numberAsString)
            urlHandle.close()
            hits = int(hits)
        except:
            hits = 0
        return hits

def guess(question, choices):
    mcg = multiChoiceGuesser(question, choices)
    print 'The question is: ', question
    print 'The most likely answer is: ', choices[mcg.guessedAnswer()]
    print ''

if __name__ == '__main__':

    """
    The test suite.
    """

    question = 'In which part of the world is Denmark located'
    choices = ('Europe',
               'America',
               'Asia',
               'usa'
               )
    guess(question, choices)

    question = 'Who created Linux'
    choices = ('Linus Torvalds',
               'RMS',
               'Steve Jobs',
               'Bill Gates'
               )
    guess(question, choices)

    question = 'What is Python'
    choices = ('a programming language',
               'a tv series',
               'a snake'
               )
    guess(question, choices)

    question = 'Who is norman bates'
    choices = ('a character in Psycho',
               'A hotel owner',
               'the main character in american psycho'
        )
    guess(question, choices)

    question = 'What is Madonna\'s full name?'
    choices = ('Madonna Lewis Ciccone',
               'Madonna lillith Ciccone',
               'Madonna Louise Ciccone'
               )
    guess(question, choices)

#######################################

The question is:  In which part of the world is Denmark located
The most likely answer is:  Europe

The question is:  Who created Linux
The most likely answer is:  Linus Torvalds

The question is:  What is Python
The most likely answer is:  a programming language

The question is:  Who is norman bates
The most likely answer is:  a character in Psycho

The question is:  What is Madonna's full name?
The most likely answer is:  Madonna Louise Ciccone





More information about the Python-list mailing list