|
import gzip |
|
import json |
|
|
|
|
|
print("--------> Sequential processing setup!") |
|
relations_dict = {} |
|
|
|
|
|
file_path = "conceptnet-assertions-5.7.0.csv.gz" |
|
|
|
|
|
def parse_line(line): |
|
parts = line.strip().split("\t") |
|
if len(parts) >= 3: |
|
|
|
start_node_uri = parts[2] |
|
end_node_uri = parts[3] |
|
|
|
|
|
start_lang_code = start_node_uri.split("/")[2] |
|
language_code = start_lang_code |
|
|
|
|
|
start_node = start_node_uri.split("/")[3] |
|
end_node = end_node_uri.split("/")[3] |
|
|
|
|
|
if end_node_uri.startswith("http://"): |
|
return |
|
|
|
|
|
if language_code not in relations_dict: |
|
relations_dict[language_code] = {} |
|
|
|
|
|
if start_node not in relations_dict[language_code]: |
|
relations_dict[language_code][start_node] = [] |
|
|
|
relations_dict[language_code][start_node].append(end_node) |
|
|
|
|
|
with gzip.open(file_path, "rt") as f: |
|
print("--------> File opened!") |
|
for line in f: |
|
parse_line(line) |
|
|
|
|
|
|
|
print("--------> About to save the file!") |
|
output_file_path = "cn_relations.json" |
|
with open(output_file_path, "w") as json_file: |
|
json.dump(relations_dict, json_file, ensure_ascii=False) |
|
|
|
print("Data saved to", output_file_path) |