Write me python code that matches 2 json files with categories and subcategories and then shows the percentage of match they have based on a newly made up legend
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here is Python code that matches 2 json files with categories and subcategories and calculates the percentage match between them based on a custom legend:
python
import json
# Sample category json files
file1 = '''
{
"categories": [
{"name": "Fruits", "subcategories": ["Apples", "Oranges", "Bananas"]},
{"name": "Vegetables", "subcategories": ["Carrots", "Spinach", "Broccoli"]}
]
}
'''
file2 = '''
{
"categories": [
{"name": "Fruits", "subcategories": ["Apples", "Oranges"]},
{"name": "Vegetables", "subcategories": ["Carrots", "Broccoli"]}
]
}
'''
# Load json
data1 = json.loads(file1)
data2 = json.loads(file2)
# Custom legend
legend = {
"100%": "Perfect match",
"75-99%": "Very high match",
"50-74%": "High match",
"25-49%": "Moderate match",
"0-24%": "Low match"
}
# Calculate match
categories_matched = 0
subcats_matched = 0
for cat1 in data1["categories"]:
for cat2 in data2["categories"]:
if cat1["name"] == cat2["name"]:
categories_matched += 1
common_subcats = set(cat1["subcategories"]).intersection(set(cat2["subcategories"]))
subcats_matched += len(common_subcats)
total_subcats = sum([len(cat["subcategories"]) for cat in data1["categories"]])
match_percent = (categories_matched + subcats_matched/total_subcats) * 100
# Print result
for k, v in legend.items():
if match_percent >= float(k.split("-")[0]) and match_percent <= float(k.split("-")[1]):
print(f"{v} - {match_percent:.2f}% match")
This calculates the percentage match based on number of common categories and percentage of matching subcategories. It then prints out the match level from the custom legend. You can modify the json files and legend as needed.