Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.70 KB | None | 0 0
  1. import operator, re
  2.  
  3. def get_scrabble_score(word):
  4.     score_dict = {1:['a', 'e', 'i', 'o', 'u', 'l', 'n', 's', 't', 'r'],
  5.                   2:['d', 'g'],
  6.                   3:['b', 'c', 'm', 'p'],
  7.                   4:['f', 'h', 'v', 'w', 'y'],
  8.                   5:['k'],
  9.                   8:['j', 'x'],
  10.                   10:['q', 'z']}
  11.     score = 0
  12.     for letter in word:
  13.         for ascore in score_dict.keys():
  14.             if letter in score_dict[ascore]:
  15.                 score += ascore
  16.                
  17.     return score
  18. def is_playable(word):
  19.     frequency_dict =
  20.  
  21. def file_to_scrabble_dict(afile):
  22.     f = open(afile, 'r')
  23.     words = f.read().split(" ")
  24.     words2 = []
  25.     for word in words:
  26.         if "\n" in word:
  27.             word = word.split("\n")
  28.             for word2 in word:
  29.                 words2.append(word2)
  30.         else:
  31.             words2.append(word)
  32.    
  33.     scrabble_dict = {}
  34.     for aword in words2:
  35.         aword = re.sub(r'\W+', '', aword)
  36.         score = get_scrabble_score(aword)
  37.         if aword in scrabble_dict.keys():
  38.             scrabble_dict[aword][1] += 1
  39.         else:
  40.             scrabble_dict[aword] = [score, 1]
  41.     f.close()
  42.     return scrabble_dict
  43.  
  44. def sort_dict_by_vals(adict):
  45.     sorted_x = sorted(adict.items(), key=operator.itemgetter(1), reverse=True)
  46.     return sorted_x
  47.  
  48.    
  49. def main():
  50.     beemovie_points = file_to_scrabble_dict(r'C:\Temp\beemovie.txt')
  51.     beemovie_sorted = sort_dict_by_vals(beemovie_points)
  52.     sorted_script = ""
  53.     for aword in beemovie_sorted:
  54.         sorted_script = sorted_script + "{}\t\t\tOccurrences: {}\tScore: {}\n".format(aword[0], aword[1][1], aword[1][0])
  55.     print sorted_script
  56. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement