File size: 925 Bytes
f1316e7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import json
def keep_only_one_source_domain(json_file_path):
with open(json_file_path, 'r') as file:
data = json.load(file)
seen_domains = set()
unique_data = []
for item in data:
source_domain = item.get('source_domain')
json_obj = {}
json_obj["url"] = item["url"]
json_obj["source_domain"] = item["source_domain"]
json_obj["title"] = item["title"]
json_obj["raw_content"] = " ".join(item["raw_content"].split()[:50])
if source_domain not in seen_domains:
seen_domains.add(source_domain)
unique_data.append(json_obj)
with open("unique_source_documents.json", 'w') as file:
json.dump(unique_data, file, indent=4, ensure_ascii=False)
# Usage example:
json_file_path = 'selected_documents.json' # Replace 'data.json' with the path to your JSON file
keep_only_one_source_domain(json_file_path)
|