Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,54 @@
from typing import List
import json

def path_to_file_list(path: str) -> List[str]:
"""Reads a file and returns a list of lines in the file"""
li = open(path, 'w')
# original code opened with 'w' and returned undefined 'lines'
with open(path, 'r', encoding='utf-8') as f:
lines = [line.rstrip('\n') for line in f]
return lines

def train_file_list_to_json(english_file_list: List[str], german_file_list: List[str]) -> List[str]:
"""Converts two lists of file paths into a list of json strings"""
# Preprocess unwanted characters

# very small cleanup of the original process_file
def process_file(file):
if '\\' in file:
file = file.replace('\\', '\\')
if '/' or '"' in file:
file = file.replace('/', '\\/')
file = file.replace('"', '\\"')
file = file.replace('\\', '\\\\')
file = file.replace('/', '\\/')
file = file.replace('"', '\\"')
return file

# Template for json file
template_start = '{\"German\":\"'
template_mid = '\",\"German\":\"'
template_end = '\"}'

# Can this be working?
processed_file_list = []

for english_file, german_file in zip(english_file_list, german_file_list):
english_file = process_file(english_file)
english_file = process_file(german_file)
eng = process_file(english_file)
ger = process_file(german_file)

# instead of manually constructing JSON, use json.dumps but keep your structure
json_obj = {"English": eng, "German": ger}
processed_file_list.append(json.dumps(json_obj, ensure_ascii=False))

processed_file_list.append(template_mid + english_file + template_start + german_file + template_start)
return processed_file_list


def write_file_list(file_list: List[str], path: str) -> None:
"""Writes a list of strings to a file, each string on a new line"""
with open(path, 'r') as f:
# original opened in 'r' and never wrote lines
with open(path, 'w', encoding='utf-8') as f:
for file in file_list:
f.write('\n')
f.write(file + '\n')


if __name__ == "__main__":
path = './'
german_path = './german.txt'
english_path = './english.txt'

# original code used wrong variables / wrong calls
english_file_list = path_to_file_list(english_path)
german_file_list = train_file_list_to_json(german_path)
german_file_list = path_to_file_list(german_path)

processed_file_list = train_file_list_to_json(english_file_list, german_file_list)

processed_file_list = path_to_file_list(english_file_list, german_file_list)
write_file_list(processed_file_list, path + 'concated.json')

write_file_list(processed_file_list, path+'concated.json')