list - Iterating through a .txt file in an odd way -
what trying write program opens .txt file movie reviews rating number 0-4 followed short review of movie. program prompts user open second text file words matched against reviews , given number value based on review.
for example, these 2 sample reviews how appear in .txt file:
4 comedy-drama of epic proportions rooted in sincere performance title character undergoing midlife crisis . 2 massoud 's story epic , tragedy , record of tenacious , humane fighter prisoner -lrb- , victim -rrb- of history .
so, if looking word "epic", increment count word 2 (which have figured out) since appears twice, , append values 4 , 2 list of ratings word.
how append ints list or dictionary related word? keep in mind need create new list or dicitonary key every word in list of words.
please , thank you. , sorry if poorly worded, programming isn't forte.
all of code:
def menu_validate(prompt, min_val, max_val): """ produces prompt, gets input, validates input , returns value. """ while true: try: menu = int(input(prompt)) if menu >= min_val , menu <= max_val: return menu break elif menu.lower == "quit" or menu.lower == "q": quit() print("you must enter number value {} {}.".format(min_val, max_val)) except valueerror: print("you must enter number value {} {}.".format(min_val, max_val)) def open_file(prompt): """ opens 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 valid file name. make sure file open in programs root folder.") def make_list(file): lst = [] line in file: lst2 = line.split(' ') del lst2[-1] lst.append(lst2) return lst def rating_list(lst): '''iterates through list of lists , appends first value in each list second list''' rating_list = [] list in lst: rating_list.append(list[0]) return rating_list def word_cnt(lst, word : str): cnt = 0 list in lst: word in list: cnt += 1 return cnt def words_list(file): lst = [] word in file: lst.append(word) return lst ##def sort(words, occurrences, avg_scores, std_dev): ## '''sorts , prints output''' ## menu = menu_validate("you must choose 1 of valid choices of 1, 2, 3, 4 \n sort options\n 1. sort avg ascending\n 2. sort avg descending\n 3. sort std deviation ascending\n 4. sort std deviation descending", 1, 4) ## print ("{}{}{}{}\n{}".format("word", "occurence", "avg. score", "std. dev.", "="*51)) ## if menu == 1: ## in range (len(word_list)): ## print ("{}{}{}{}".format(cnt_list.sorted[i],) def make_odict(lst1, lst2): '''makes ordered dictionary of keys/values 2 lists of equal length''' dic = ordereddict() in range (len(word_list)): dic[lst2[i]] = lst2[i] return dic cnt_list = [] while true: menu = menu_validate("1. sentiment words in file? \nq. quit \n", 1, 1) if menu == true: ratings_file = open("sample.txt") ratings_list = make_list(ratings_file) word_file = open_file("enter name of file words score \n") word_list = words_list(word_file) word in word_list: cnt = word_cnt(ratings_list, word) cnt_list.append(word_cnt(ratings_list, word))
sorry, know it's messy , incomplete.
i think mean:
import collections counts = collections.defaultdict(int) word = 'epic' counts[word] += 1
obviously, can more word
have, aren't showing code, ...
edit
okay, looking @ code, i'd suggest make separation between rating , text explicit. take this:
def make_list(file): lst = [] line in file: lst2 = line.split(' ') del lst2[-1] lst.append(lst2) return lst
and convert this:
def parse_ratings(file): """ given file of lines, each numeric rating @ start, parse lines score/text tuples, 1 per line. return list of parsed tuples. """ ratings = [] line in file: text = line.strip().split() if text: score = text[0] ratings.append((score,text[1:])) return ratings
then can compute both values together:
def match_reviews(word, ratings): cnt = 0 scores = [] score,text in ratings: n = text.count(word) if n: cnt += n scores.append(score) return (cnt, scores)
Comments
Post a Comment