forked from Lobss001/Assignment_6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
51 lines (40 loc) · 1.76 KB
/
main.py
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
47
48
49
50
51
from typing import List
def path_to_file_list(path: str) -> List[str]:
"""Reads a file and returns a list of lines in the file"""
# Will this be working?
lines = open(path, 'r').read().split('\n')
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
def process_file(file):
if '\\' in file:
file = file.replace('\\', '\\\\')
if '/' or '"' in file:
file = file.replace('/', '\\/')
file = file.replace('"', '\\"')
return file
# Template for json file
template_start = '{\"English\":\"'
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)
processed_file_list.append(template_end + english_file + template_mid + 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, 'w') as f:
for file in file_list:
f.write(file + '\n')
if __name__ == "__main__":
path = './'
german_path = './german.txt'
english_path = './english.txt'
english_file_list = path_to_file_list(english_path)
german_file_list = path_to_file_list(german_path)
processed_file_list = train_file_list_to_json(english_file_list, german_file_list)
write_file_list(processed_file_list, path+'concated.json')