|
import os |
|
import gzip |
|
import json |
|
from tqdm import tqdm |
|
import multiprocessing |
|
from multiprocessing import Manager |
|
|
|
manager = multiprocessing.Manager() |
|
processed_domains = manager.dict() |
|
other_selected_domains = manager.list() |
|
|
|
|
|
with open("unique_source_documents.json", 'r') as file: |
|
for obj in json.load(file): |
|
processed_domains[obj["source_domain"]] = 1 |
|
|
|
|
|
|
|
def is_similar(url, source_domain): |
|
url = url.replace("https://", "").replace("http://", "").replace("www.", "").replace(".html", "").replace("home", "").replace("index", "").strip("/") |
|
source_domain = source_domain.replace("www.", "") |
|
return url == source_domain |
|
|
|
def extract_domains_from_file_with_url(filepath): |
|
with gzip.open(filepath, 'rt', encoding='utf-8') as f: |
|
for line in f: |
|
data = json.loads(line) |
|
source_domain = data.get('source_domain') |
|
url = data.get('url') |
|
print("here") |
|
if not (source_domain in processed_domains) and is_similar(url, source_domain): |
|
processed_domains[source_domain] = 1 |
|
|
|
json_obj = {} |
|
json_obj["url"] = data["url"] |
|
json_obj["source_domain"] = data["source_domain"] |
|
json_obj["title"] = data["title"] |
|
json_obj["raw_content"] = " ".join(data["raw_content"].split()[:50]) |
|
|
|
other_selected_domains.append(json_obj) |
|
print("Done") |
|
|
|
def process_file_with_url(file_path, counter): |
|
|
|
print("Inside process") |
|
if file_path.endswith('.json.gz'): |
|
extract_domains_from_file_with_url(file_path) |
|
|
|
counter.value += 1 |
|
|
|
def find_unique_domains_with_url(folder_path): |
|
total_files = 10 |
|
progress_bar = tqdm(total=total_files, desc="Processing") |
|
|
|
|
|
manager = Manager() |
|
counter = manager.Value('i', 0) |
|
|
|
for root, dirs, files in os.walk(folder_path): |
|
for file in files: |
|
file_path = os.path.join(root, file) |
|
|
|
process_file_with_url(file_path, counter) |
|
|
|
break |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
progress_bar.close() |
|
|
|
|
|
|
|
snapshots_folder = '/mnt/weka/peacock/wet-data/output/toxic_filtered_without_bloom_new' |
|
|
|
|
|
find_unique_domains_with_url(snapshots_folder) |
|
|
|
with open("new_unique_source_documents_with_url.json", 'w') as file: |
|
json.dump(list(other_selected_domains), file, indent=4, ensure_ascii=False) |
|
|