import ast # needed to read text as a dictionary
#import operator # needed to find maximum value
def define_words():
'''
Finds the word with the most occurences
Asks for a definition
Replaces entry in working dictionary
Writes to a new final dictionary
'''
with open('/Users/admin/Desktop/Dante Dictionary/parole_di_dante.txt','r', encoding = "utf-8") as dic:
dante_dict = ast.literal_eval(dic.read())# reads text as a dictionary
#key_to_find = ''
#definition = ''
key_to_find = max(dante_dict.items(), key=operator.itemgetter(1))[0]
print('The word needing to be defined is ', key_to_find)
definition = input('Definition ? : ')
for key_to_find in dante_dict.keys():
#if key == key_to_find:
dante_dict[key_to_find] = definition
with open('/Users/admin/Desktop/Dante Dictionary/parole_di_dante.txt', 'w', encoding = 'utf-8') as outfile:
outfile.write(str(dante_dict))
with open('/Users/admin/Desktop/Dante Dictionary/final_dante_dictionary.txt', 'w', encoding = 'utf-8') as finalfile:
finalfile.write(str(dante_dict))
上記を使用して、次の形式で辞書から返します。
{'sicuramente,': 11, 'beatitudo,': 11, 'concetti:': 15, 'ello:': 15, 'ello;': 16, 'favella,': 33, 'fene.': 13, 'ello,': 22, 'spira,': 66,'che': 560,…}
ただし、上記の「プログラム」を実行すると、「それ」として定義する「che」という単語で言うと、返されるものは次のとおりです。
{'sicuramente,': 'that', 'beatitudo,': 'that', 'concetti:': 'that', 'ello:': 'that', 'ello;': 'that', 'favella,': 'that', 'fene.': 'that', 'ello,': 'that', 'spira,': 'that','che': 'that'…} instead of changing only the entry that I want to edit.
明らかに私は愚かなことをしています。また、句読点を削除できていないということもありません。あなたの助けに感謝します。ありがとうございました