Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def main ():
- run=True
- dict_sum={}
- dict_count={}
- file=get_file ()
- dict_sum, dict_count = make_dicts (file)
- while (run == True):
- action=get_action ( )
- if (action < 1):
- return get_action ()
- elif (action>5):
- return get_action ( )
- elif (action == 1):
- get_sum(dict_sum, dict_count)
- elif (action == 2):
- get_action2 ()
- elif (action == 3):
- get_action3 ()
- elif (action == 4):
- action=get_action ()
- elif (action == 5):
- run=False
- def print_instruction ():
- print("What would you like to do?")
- print("1: Get the score of a word")
- print("2: Get the average score of words in a file")
- print("3: Find the highest / lowest scoring words in a file")
- print("4: Sort the words into positive.txt and negative.txt")
- print("5: Exit the program")
- def get_file ():
- filename=input("Learning data file name?")
- def get_action ():
- action = int(input("Enter a number 1 – 5: "))
- return action
- def make_dicts (file):
- dict_sum ={}
- dict_count ={}
- lines=open("training1.txt").readlines()
- for line in lines:
- parts=line.split()
- score=int(parts[0])
- #sets to where there is no repeat
- repeat= False
- for key in range (1,len(parts)):
- repeat= False
- if (repeat==False):
- if (parts[key] in dict_sum and parts[key] in dict_count ):
- dict_sum[parts[key]] +=score
- dict_count[parts[key]] +=1
- repeat=True
- else:
- dict_sum[parts[key]] = score
- dict_count[parts[key]] = 1
- return dict_sum, dict_count
- def get_sum(dict_sum, dict_count):
- word = input ("which word? ")
- word_sum=0
- word_count=0
- for key in dict_sum:
- if (word in dict_count):
- word_sum=dict_sum[word]
- for key in dict_count:
- if (word in dict_count):
- word_count=dict_count[word]
- if (word_count > 0):
- word_avg=round(word_sum/word_count,1)
- get_rank (word_avg)
- else:
- print()
- def get_action2 ():
- file = input ("file name? ")
- #sets all everytrhing to 0
- sum_all=0
- count_all=0
- avg=0
- #creates a new dict
- dict_sum2={}
- dict_count2={}
- dict_sum2, dict_count2 = make_dicts(file)
- #goes throug the dicts
- for key in dict_sum2:
- sum_all += dict_sum2[key]
- for key in dict_count2:
- count_all+=dict_count2[key]
- #gets score of all the words
- if (count_all > 0):
- avg=round(sum_all/count_all,2)
- get_rank (avg)
- else:
- print()
- def get_action3 ():
- file = input ("file name? ")
- #sets all everytrhing to 0 or emty string
- test_sum=0
- test_count=0
- max_sum=0
- max_name=""
- min_sum=100
- min_name=""
- #creates a new dict
- dict_sum3={}
- dict_count3={}
- dict_sum3, dict_count3 = make_dicts(file)
- for key in range(0,len(dict_sum3)):
- test_sum=dict_sum3[key]
- print(test_sum)
- def get_rank (avg):
- #prints score
- print("score = "+ str(avg))
- #determines what the score is
- if (avg <2):
- print(" is negetive")
- print()
- elif (avg == 2):
- print(" is neutral")
- print()
- else:
- print(" is positive")
- print()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement