Advertisement
Guest User

Untitled

a guest
May 23rd, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. import re
  2.  
  3. def find_word(message):
  4.     wordDict = {}
  5.     scoreDict = {}
  6.     # Remove punctuation
  7.     message = re.sub('[^A-Za-z0-9\s+]+', '', message)
  8.     # Lowercase the sentence
  9.     message = message.lower()
  10.     # Put it into a list
  11.     messageList = message.split(" ")
  12.     # Compile my dictionary of pairs
  13.     index1 = 0
  14.     while index1 < (len(messageList)-1):
  15.         for index2 in range(index1+1,len(messageList)):
  16.             wordDict[messageList[index1],messageList[index2]] = 0
  17.         index1 += 1
  18.     # Add up the values
  19.     for pair in wordDict:
  20.         # Check first letters
  21.         if pair[0][0] == pair[1][0]:
  22.             wordDict[pair] += 10
  23.         # Check end letters
  24.         if pair[0][-1] == pair[1][-1]:
  25.             wordDict[pair] += 10
  26.         # Check lengths
  27.         if len(pair[0]) <= len(pair[1]):
  28.             wordDict[pair] += 30 * (len(pair[0]) / len(pair[1]))
  29.         else:
  30.             wordDict[pair] += 30 * (len(pair[1]) / len(pair[0]))
  31.         # Check unique letters
  32.         uniCount = 0
  33.         sameCount = 0
  34.         usedLetters = ""
  35.         for letter in pair[0]:
  36.             if letter not in usedLetters:
  37.                 uniCount += 1
  38.                 usedLetters += letter
  39.         for letter in pair[1]:
  40.             if letter not in pair[0]:
  41.                 uniCount += 1
  42.                 usedLetters += letter
  43.         if len(pair[0]) <= len(pair[1]):
  44.             for letter in pair[0]:
  45.                 if letter in pair[1]:
  46.                     sameCount += 1
  47.         else:
  48.             for letter in pair[1]:
  49.                 if letter in pair[0]:
  50.                     sameCount += 1
  51.         if uniCount > 0:
  52.             wordDict[pair] += 50 * (sameCount / uniCount)
  53.     print("Paired scores:",wordDict,"\n")
  54.    
  55.     # Find the sum score of each word
  56.     for pair in wordDict:
  57.         if pair[0] not in scoreDict:
  58.             scoreDict[pair[0]] = wordDict[pair]
  59.         else:
  60.             scoreDict[pair[0]] += wordDict[pair]
  61.         if pair[1] not in scoreDict:
  62.             scoreDict[pair[1]] = wordDict[pair]
  63.         else:
  64.             scoreDict[pair[1]] += wordDict[pair]
  65.     print ("Sum of scores:",scoreDict,"\n")
  66.  
  67.     # Find the average score of each word
  68.     for pair in scoreDict:
  69.         scoreDict[pair] = scoreDict[pair] / len(scoreDict)
  70.     highestScore = 0
  71.     highestWord = None
  72.     for word in scoreDict:
  73.         if scoreDict[word] > highestScore:
  74.             highestScore = scoreDict[word]
  75.             highestWord = word
  76.     print ("Average scores:",scoreDict,"\n")
  77.     print ("My Answer",highestWord)
  78.     return highestWord
  79.  
  80.  
  81.  
  82. find_word("Friend Fred and friend Ted.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement