File size: 1,580 Bytes
1ce19cf |
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
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:
# data = json.loads(line.strip())
# Detecting language for "Human" and "Assistant" values
human_language = get_language(line)
# print(human_language)
# print(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) |