Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1.  
  2.  
  3. def main ():
  4. run=True
  5. dict_sum={}
  6. dict_count={}
  7.  
  8. file=get_file ()
  9. dict_sum, dict_count = make_dicts (file)
  10. while (run == True):
  11. action=get_action ( )
  12. if (action < 1):
  13. return get_action ()
  14. elif (action>5):
  15. return get_action ( )
  16. elif (action == 1):
  17. get_sum(dict_sum, dict_count)
  18. elif (action == 2):
  19. get_action2 ()
  20. elif (action == 3):
  21. get_action3 ()
  22. elif (action == 4):
  23. action=get_action ()
  24. elif (action == 5):
  25. run=False
  26.  
  27.  
  28.  
  29. def print_instruction ():
  30. print("What would you like to do?")
  31. print("1: Get the score of a word")
  32. print("2: Get the average score of words in a file")
  33. print("3: Find the highest / lowest scoring words in a file")
  34. print("4: Sort the words into positive.txt and negative.txt")
  35. print("5: Exit the program")
  36.  
  37.  
  38. def get_file ():
  39. filename=input("Learning data file name?")
  40.  
  41. def get_action ():
  42. action = int(input("Enter a number 1 – 5: "))
  43. return action
  44.  
  45.  
  46. def make_dicts (file):
  47. dict_sum ={}
  48. dict_count ={}
  49. lines=open("training1.txt").readlines()
  50. for line in lines:
  51. parts=line.split()
  52. score=int(parts[0])
  53. #sets to where there is no repeat
  54. repeat= False
  55. for key in range (1,len(parts)):
  56. repeat= False
  57. if (repeat==False):
  58. if (parts[key] in dict_sum and parts[key] in dict_count ):
  59. dict_sum[parts[key]] +=score
  60. dict_count[parts[key]] +=1
  61. repeat=True
  62. else:
  63. dict_sum[parts[key]] = score
  64. dict_count[parts[key]] = 1
  65.  
  66. return dict_sum, dict_count
  67.  
  68.  
  69.  
  70. def get_sum(dict_sum, dict_count):
  71. word = input ("which word? ")
  72. word_sum=0
  73. word_count=0
  74. for key in dict_sum:
  75. if (word in dict_count):
  76. word_sum=dict_sum[word]
  77.  
  78. for key in dict_count:
  79. if (word in dict_count):
  80. word_count=dict_count[word]
  81.  
  82. if (word_count > 0):
  83. word_avg=round(word_sum/word_count,1)
  84. get_rank (word_avg)
  85. else:
  86. print()
  87.  
  88.  
  89.  
  90.  
  91.  
  92. def get_action2 ():
  93. file = input ("file name? ")
  94. #sets all everytrhing to 0
  95. sum_all=0
  96. count_all=0
  97. avg=0
  98.  
  99. #creates a new dict
  100. dict_sum2={}
  101. dict_count2={}
  102. dict_sum2, dict_count2 = make_dicts(file)
  103.  
  104. #goes throug the dicts
  105. for key in dict_sum2:
  106. sum_all += dict_sum2[key]
  107.  
  108. for key in dict_count2:
  109. count_all+=dict_count2[key]
  110. #gets score of all the words
  111. if (count_all > 0):
  112. avg=round(sum_all/count_all,2)
  113. get_rank (avg)
  114.  
  115. else:
  116. print()
  117.  
  118. def get_action3 ():
  119. file = input ("file name? ")
  120. #sets all everytrhing to 0 or emty string
  121. test_sum=0
  122. test_count=0
  123.  
  124. max_sum=0
  125. max_name=""
  126.  
  127. min_sum=100
  128. min_name=""
  129.  
  130. #creates a new dict
  131. dict_sum3={}
  132. dict_count3={}
  133. dict_sum3, dict_count3 = make_dicts(file)
  134.  
  135. for key in range(0,len(dict_sum3)):
  136. test_sum=dict_sum3[key]
  137. print(test_sum)
  138.  
  139.  
  140.  
  141.  
  142. def get_rank (avg):
  143. #prints score
  144. print("score = "+ str(avg))
  145. #determines what the score is
  146. if (avg <2):
  147. print(" is negetive")
  148. print()
  149. elif (avg == 2):
  150. print(" is neutral")
  151. print()
  152. else:
  153. print(" is positive")
  154. print()
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement