2016-04-25 19 views
1

私はキーとして単語の辞書を持ち、値としてintを持っています。標準偏差関数を書く

print (word_ratings_dict) 
{'hate': [1, 2, 2, 1, 1, 3, 0, 2, 3, 2, 0, 4, 1, 1], 'joy': [3, 4, 3, 3, 2, 4, 1]} 

辞書の各キーワードについて、統計モジュールを使わずに標準偏差を計算する必要があります。

相続人は、私がこれまで持っているもの:

def menu_validate(prompt, min_val, max_val): 
    """ produces a prompt, gets input, validates the input and returns a value. """ 
    while True: 
     try: 
      menu = int(input(prompt)) 
      if menu >= min_val and menu <= max_val: 
       return menu 
       break 
      elif menu.lower == "quit" or menu.lower == "q": 
       quit() 
      print("You must enter a number value from {} to {}.".format(min_val, max_val)) 
     except ValueError: 
      print("You must enter a number value from {} to {}.".format(min_val, max_val)) 

def open_file(prompt): 
    """ opens a file """ 
    while True: 
     try: 
      file_name = str(input(prompt)) 
      if ".txt" in file_name: 
       input_file = open(file_name, 'r') 
       return input_file 
      else: 
       input_file = open(file_name+".txt", 'r') 
       return input_file 
     except FileNotFoundError: 
      print("You must enter a valid file name. Make sure the file you would like to open is in this programs root folder.") 

def make_list(file): 
    lst = [] 
    for line in file: 
     lst2 = line.split(' ') 
     del lst2[-1] 
     lst.append(lst2) 
    return lst 

def rating_list(lst): 
    '''iterates through a list of lists and appends the first value in each list to a second list''' 
    rating_list = [] 
    for list in lst: 
     rating_list.append(list[0]) 
    return rating_list 

def word_cnt(lst, word : str): 
    cnt = 0 
    for list in lst: 
     for word in list: 
      cnt += 1 
    return cnt 

def words_list(file): 
    lst = [] 
    for word in file: 
     lst.append(word) 
    return lst 

def word_rating(word, ratings_lst): 
    '''finds ratings for a word and appends them to a dictionary of words''' 
    lst = [] 
    for line in ratings_lst: 
     line = line.split() 
     if word in line: 
      rating = line[0] 
      lst.append(int(rating)) 
    return lst 
cnt_list = [] 
while True: 
    menu = menu_validate("1. Get sentiment for all words in a file? \nQ. Quit \n", 1, 1) 
    if menu == True: 
     ratings_file = open("sample.txt") 
     ratings_list = make_list(ratings_file) 
     word_ratings_dict = {} 
     word_avg_dict = {} 
     std_dev_dict = {} 
     word_file = open_file("Enter the name of the file with words to score \n") 
     word_list = words_list(word_file) 


     for word in word_list: 
      #counts the words 
      cnt = word_cnt(ratings_list, word) 

      cnt_dict[word] = cnt 

      word_ratings_dict[word] = word_rating(word, ratings_list) 

      total_rating = 0 

      for i in range (0, cnt): 
       total_rating += word_ratings_dict[word][i] 

      word_avg_dict[word] = total_rating/cnt 

      std_dev_dict[word] = 
+0

をあなたは '輸入statistics'をしたくない場合でも、[モジュールコード](HTTPS ://hg.python.org/cpython/file/3.5/Lib/statistics.py)はオープンソースです。あなたはいつもそのモジュールがどのようにそれをするのかを読むことができます。とにかく '統計情報 'の何が間違っていますか? – ChrisP

+1

これは割り当てのためであり、講師は明示的に統計モジュールを使用できないと述べました。 –

答えて

2

これらがきちんと仕事をする:

def mean(data): 
    return float(sum(data)/len(data)) 

def variance(data): 
    mu = mean(data) 
    return mean([(x - mu) ** 2 for x in data]) 

def stddev(data): 
    return sqrt(variance(data)) 
+0

ありがとうございました! –