私がしようとしているのは、ムービーレビューで.txtファイルを開き、評価が0-4の数字で、その後に映画の短いレビュー。次いで、プログラムは、レビューと照合され、レビューに基づいて数値が与えられた単語を含む第2のテキストファイルを開くようにユーザに促す。中年の危機を受けたタイトル文字で誠実なパフォーマンスに根ざしほぼ壮大なスケールの奇妙な方法で.txtファイルを繰り返します。
4コメディ・ドラマ:彼らは.txtファイルにどのように表示されるか、これらの2つのサンプルのレビューで例えば
、。マサウドの物語は叙事詩であるが、悲劇でもあり、囚人でもあり、究極的には犠牲者であった - 人類の歴史の犠牲者でもあった強靭で人道的な戦闘機の記録である。
「epic」という言葉を探していた場合、2回現れてからその単語の数が2つ増えてしまいました。その単語の評価のリスト。
これらのintをその単語に関連するリストまたは辞書に追加するにはどうすればよいですか?単語リストのすべての単語に対して新しいリストまたはdicitonaryキーを作成する必要があることに注意してください。
お願いします。ありがとうございます。そして、申し訳ありませんが、これはあまり言葉を言わなかった場合、プログラミングは私の特権ではありません。
私のコードのすべて:
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 sort(words, occurrences, avg_scores, std_dev):
## '''sorts and prints the output'''
## menu = menu_validate("You must choose one of the valid choices of 1, 2, 3, 4 \n Sort Options\n 1. Sort by Avg Ascending\n 2. Sort by Avg Descending\n 3. Sort by Std Deviation Ascending\n 4. Sort by Std Deviation Descending", 1, 4)
## print ("{}{}{}{}\n{}".format("Word", "Occurence", "Avg. Score", "Std. Dev.", "="*51))
## if menu == 1:
## for i in range (len(word_list)):
## print ("{}{}{}{}".format(cnt_list.sorted[i],)
def make_odict(lst1, lst2):
'''makes an ordered dictionary of keys/values from 2 lists of equal length'''
dic = OrderedDict()
for i in range (len(word_list)):
dic[lst2[i]] = lst2[i]
return dic
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_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:
cnt = word_cnt(ratings_list, word)
cnt_list.append(word_cnt(ratings_list, word))
申し訳ありませんが、私はそれが汚いと非常に不完全であることを知っています。
私が持っているカウント部は考え出しました。私は.txtファイルを繰り返し処理できる必要があり、プログラムが探している単語が現れるたびに、その単語の前の整数をリストに追加する必要があります。 –
さて、私はいくつかのコードを追加しました。私はあなたのデータをより正式に扱う必要があると思います。テキストとスコアを区切り、その後、それらを正式な位置に保ちます。あなたはいつ何が何であるかを知っているでしょう。 –