gpt4 book ai didi

python - Movie File 将文件解析为以下形式的字典

转载 作者:太空宇宙 更新时间:2023-11-03 20:05:16 24 4
gpt4 key购买 nike

1.6。推荐电影创建一个函数来计算一组电影评论中有多少个相似的关键字并推荐关键词数量最相似的电影。此任务的解决方案需要使用字典。电影评论和关键字位于名为 film_reviews.txt 的文件中,以逗号分隔。第一个术语是电影名称,其余术语是电影的关键字标签(即“惊人的”、“诗意的”、“可怕的”等等)。

函数名称:similar_movie()

参数/参数:电影名称

返回:与作为参数传递的电影相似的电影列表

film_reviews.txt -

7 Days in Entebbe,fun,foreign,sad,boring,slow,romance
12 Strong,war,violence,foreign,sad,action,romance,bloody
A Fantastic Woman,fun,foreign,sad,romance
A Wrinkle in Time,book,witty,historical,boring,slow,romance
Acts of Violence,war,violence,historical,action
Annihilation,fun,war,violence,gore,action
Armed,foreign,sad,war,violence,cgi,fancy,action,bloody
Black '47,fun,clever,witty,boring,slow,action,bloody
Black Panther,war,violence,comicbook,expensive,action,bloody

最佳答案

我认为这对你有用

film_data = {'films': {}} 
with open('film_reviews.txt', 'r') as f:
for line in f.readlines():
data = line.split(',')
data[-1] = data[-1].strip() # removing new line character
film_data['films'][data[0].lower()] = data[1:]

def get_smilar_movie(name):
if name.lower() in film_data['films'].keys():
original_review = film_data['films'][name.lower()]
similarities = dict()
for key in film_data['films']:
if key == name.lower():
continue
else:
similar_movie_review = set(film_data['films'][key])
overlap = set(original_review) & similar_movie_review
universe = set(original_review) | similar_movie_review
# % of overlap compared to the first movie = output1
output1 = float(len(overlap)) / len(set(original_review)) * 100
# % of overlap compared to the second movie = output2
output2 = float(len(overlap)) / len(similar_movie_review) * 100
# % of overlap compared to universe
output3 = float(len(overlap)) / len(universe) * 100
similarities[output1 + output2 + output3] = dict()
similarities[output1 + output2 + output3]['reviews'] = film_data['films'][key]
similarities[output1 + output2 + output3]['movie'] = key
max_similarity = max(similarities.keys())
movie2 = similarities[max_similarity]
print(name,' reviews ',film_data['films'][name.lower()])
print('similar movie ',movie2)
print('Similarity = {0:.2f}/100'.format(max_similarity/3))
return movie2['movie']
return None

get_similar_movie 函数将从 film_data dict 返回最相似的电影。该函数将采用电影名称作为参数

关于python - Movie File 将文件解析为以下形式的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59004134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com