🚀 作者 :“码上有前”
🚀 文章简介 :Python
🚀 欢迎小伙伴们 点赞👍、收藏⭐、留言💬
python练习题
- 完整代码
完整代码
from collections import Counter
from data_keywords import extract_keywords, extract_keywords_from_json
def calculate_probability(list1, list2):
count_dict = {value1: round(sum(value1 in value2 for value2 in list2) / len(list2), 2) for value1 in list1}
sorted_dict = dict(sorted(count_dict.items(), key=lambda x: x[1], reverse=True))
top_three = list(sorted_dict.items())[:3]
return top_three
# 假设这是给定的关键词列表
given_keywords = ['自营', '赠', '满赠','京东物流','免邮','2免1','2件7.5折','跨店每满','券']
category_given_keywords = ['自营', '赠', '满赠','京东物流','免邮','2免1','2件7.5折','跨店每满','券',"包税","官方立减15%"]
folder_path = './Cosmetic_data/Brand_Classification/brand&details_analysis'
categories_path = "./Cosmetic_data/Makeup_Classification/pcommit&details_analysis"
keyword_column = '关键词'
new_keyword_list = extract_keywords(folder_path, keyword_column)
categories_keywords_list = extract_keywords_from_json(categories_path, keyword_column)
result = calculate_probability(given_keywords, new_keyword_list)
# print("最高的三个关键词和其概率:", result)
calculate_result = calculate_probability(category_given_keywords, categories_keywords_list)
# print("最高的三个关键词和其概率:", calculate_result)