To clean the idiom.json dataset and remove duplicate entries based on the word key, you can use a dictionary-based deduplication strategy. This process ensures that for any given word, only one instance is kept in the final output. The logic compares the string representation of the idiom objects to decide which one to retain, then saves the unique values to a new file.
import json
# Load the original data
with open("archived/idiom.json") as fp:
idioms = json.load(fp)
# Deduplicate using a dictionary where 'word' is the key
check = dict()
for idiom in idioms:
# If the word exists, keep the one with the 'greater' string representation
if idiom["word"] in check and str(idiom) > str(check[idiom["word"]]):
check[idiom["word"]] = idiom
else:
check[idiom["word"]] = idiom
# Save the cleaned list to a new file
with open("data/idiom-clean.json", "w+", encoding="utf-8") as fp:
json.dump(list(check.values()), fp, ensure_ascii=False)