from urllib import * import re import string, sys class Decision: def get_list(self, heading, prompt): print heading print print "(enter a blank line to end the list)" ret = [] i = 1 while 1: line = raw_input(prompt % i) if not line: break ret.append(line) i=i+1 print return ret def get_number(self, prompt): res = None while res is None: try: res = float(raw_input(prompt)) except ValueError: pass return res def getOptions(self): self.options = self.get_list("Enter your options", "Option %d: ") def getCriteria(self): self.criteria = self.get_list("Enter your criteria ...", "Criterion %d: ") def getWeights(self): self.weights = {} for c in self.criteria: print print "Enter a relative importance factor, or weight, for each criteria (higher is more important)" print self.weights[c] = self.get_number("Criterion %s: " % c) def getScores(self): self.scores = {} for o in self.options: print print "Scores for option %s" % o print for c in self.criteria: self.scores[o, c] = self.get_number("Criterion %s: " % c) def getResults(self): self.results = {} for o in self.options: value = 0 for c in self.criteria: print o, c, self.weights[c], self.scores[o, c] value = value + self.weights[c] * self.scores[o, c] self.results[o] = value self.results = self.results.items() # A list of tuples (key, value) self.results.sort(lambda x, y: -cmp(x[1], y[1])) def printResults(self): print print "Results, in order from highest to lowest score" print print "%5s %s" % ("Score", "Option") for option, result in self.results: print "%5s %s" % (result, option) def turntheCrank(self): self.getOptions() self.getCriteria() self.getWeights() self.getScores() self.getResults() self.printResults() class YesNo(Decision): def turntheCrank(self): self.options = ["Yes","No"] self.getCriteria() self.getWeights() self.getScores() self.getResults() self.printResults() class Lunch(Decision): def turntheCrank(self): print print "This is a classified, top secret program developed by the CIA. Please keep it confidential" print print "What should we do for lunch? ;-)))" print self.criteria = ["taste", "convenience", "atmosphere", "value"] self.options = self.get_list("Enter your restaurants or other food choices:", "Option %d: ") self.getWeights() self.getScores() self.getResults() self.printResults() class Basketball(Decision): def turntheCrank(self): print "This is a program to help you decide which team will win a basketball game" print print "When prompted, enter a number ranking each team on the prompted team skill" print "on a scale from 1 to 100, with 1 being terrible and 100 being the best imaginable" print team_one = raw_input ("What is the name of team one: ") team_two = raw_input ("What is the name of team two: ") teams = (team_one, team_two) self.options = teams self.weights = {"speed":100, "size":66, "jumping_ability":50, "defense":60, "shooting":75, "ballhandling":50, "rebounding":50} self.criteria = self.weights.keys() self.getScores() self.getResults() self.printResults() class Football(Decision): def turntheCrank(self): print "This is a program to help you decide which team will win a football game" print print "When prompted, enter a number ranking each team on the prompted team skill" print "on a scale from 1 to 100, with 1 being terrible and 100 being the best imaginable" print team_one = raw_input ("What is the name of team one: ") team_two = raw_input ("What is the name of team two: ") teams = (team_one, team_two) self.options = teams self.weights = {"speed and quickness":7, "size and strength":5, "rushing offense":5, "passing offense":7, "defense":1, "coaching":12} self.criteria = self.weights.keys() self.getScores() self.getResults() self.printResults() class Election(Decision): def getOptions(self): self.options = self.get_list("Enter the name of each candidate", "Candidate %d: ") def turntheCrank(self): self.criteria = ["Education", "Social Issues", "Taxing and Spending", "Character", "Intelligence", "Leadership", "Foreign Affairs"] self.getOptions() self.getWeights() self.getScores() self.getResults() self.printResults() class multiChoiceGuesser: def __init__(self, question='', replys=()): self.question = question self.replys = replys def guessedAnswer(self): hits = [] result = [] results = {} for reply in self.replys: x = (self._getGoogleHits(self.question + ' ' + reply)) y = (self._getGoogleHits(reply)) x = float(x) y = float(y) if y == 0: y = y + 1 if x == 0: x = x + 1 dividend = x / y hits.append(dividend) results[reply] = dividend return hits.index(max(hits)) ddef _getGoogleHits(self, query): query = urlencode({'q':query}) urlHandle = urlopen ('http://www.google.com/search?%s' % query) googlePage = urlHandle.read() try: numberAsString = re.search( 'about', googlePage, re.S ).group(1) hits = re.sub(',', '',numberAsString) urlHandle.close() hits = int(hits) except: hits = 0 return hits def _getGoogleHits(self, query): query = urllib.urlencode({'q':query}) urlHandle = urllib.urlopen('http://www.google.com/search?%s' % query) googlePage = urlHandle.read() try: numberAsString = re.search( 'about', googlePage, re.S ).group(1) hits = re.sub (',', '',numberAsString) urlHandle.close() hits = int(hits) except: hits = 0 return hits def guess(self,question, choices): self.mcg = multiChoiceGuesser(question, choices) print ' The question is: ', question print " Please wait for Merlin's answer: ", choices[self.mcg.guessedAnswer()] , " ", "is the answer to your question!" print '' class AskMerlina(Decision, multiChoiceGuesser): def turntheCrank(self): self.question = raw_input ("What is your question? ") self.choices = self.get_list("Enter your options:", "Option %d: ") self.guess(self.question, self.choices) class AskMerlin(Decision, multiChoiceGuesser): def turntheCrank(self): self.question = raw_input ("What is your question? ") self.choices = self.get_list("Enter your options:", "Option %d: ") self.getCriteria() self.guess(self.question, self.choices) if __name__ == "__main__": tidy = 1 while 1: tidy = tidy + 1 if tidy > 2: againornot = raw_input ("Hit 'enter' to ask another question, or type 'quit' to quit this program: ") if againornot == "quit": break print print "Please enter the number for the type of decision you wish to analayze: " print "1. General Decision Analysis, you choose the options, criteria, etc." print "2. What's for Lunch?" print "3. For whom shall I Vote in the upcoming Election?" print "4. Which Basketball Team will win the Game?" print "5. Which Football Team will win the Game?" print "6. Questions which have a Yes or No answer" print "7. Ask Merlin any question, he will help you score each option for each criteria (requires internet access)" print "8. or Ask Merlina any question at all, for when you really don't have a clue about criteria, but you just need a good intuitive answer relatively quickly (requires internet access)" print "9. Type '9' to quit this program" print choice = float(raw_input("Please type in the number of the type of decision-program you wish to run from above and hit enter: ")) print choice if choice == 1: decide = Decision() decide.turntheCrank() elif choice == 2: decide = Lunch() decide.turntheCrank() elif choice == 3: decide = Election() decide.turntheCrank() elif choice == 4: decide = Basketball() decide.turntheCrank() elif choice == 5: decide = Football() decide.turntheCrank() elif choice == 6: decide = YesNo() decide.turntheCrank() elif choice == 7: decide = AskMerlin() decide.turntheCrank() elif choice == 8: decide = AskMerlina() decide.turntheCrank() elif choice == 9: break # break from infinite loop elif choice =="quit": break # exit from infinite loop else: print "Invalid operation"