|
|
import csv |
|
|
import json |
|
|
from langdetect import detect |
|
|
|
|
|
def get_language(text): |
|
|
try: |
|
|
return detect(text) |
|
|
except: |
|
|
return "Unknown" |
|
|
|
|
|
def read_json_file_line_by_line(file_path): |
|
|
data_list = [] |
|
|
with open(file_path, 'r') as file: |
|
|
for line in file: |
|
|
try: |
|
|
|
|
|
|
|
|
human_language = get_language(line) |
|
|
|
|
|
|
|
|
|
|
|
data_list.append({ |
|
|
'Human Language': human_language, |
|
|
'Line': line |
|
|
}) |
|
|
|
|
|
except json.JSONDecodeError as e: |
|
|
print(f"Error decoding JSON: {e}") |
|
|
continue |
|
|
except KeyError as e: |
|
|
print(f"Error accessing key in JSON: {e}") |
|
|
continue |
|
|
|
|
|
return data_list |
|
|
|
|
|
def write_to_text_file(file_path, data_list): |
|
|
with open(file_path, 'w', encoding='utf-8') as file: |
|
|
for data in data_list: |
|
|
line = f"{data['Human Language']},{data['Line']}\n" |
|
|
file.write(line) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
json_file_path = "/home/rainer/Downloads/oobabooga_linux/openassistant_best_replies_train.jsonl" |
|
|
text_file_path = "/home/rainer/Downloads/oobabooga_linux/openassistant_best_replies_train_with_lang.jsonl" |
|
|
data_list = read_json_file_line_by_line(json_file_path) |
|
|
write_to_text_file(text_file_path, data_list) |