-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.py
132 lines (83 loc) · 4.24 KB
/
process.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import time
from memory_profiler import profile
class Preprocess:
# __init__ method is profiled to calculate memory consumption of process. It includes initialisation of all objects and
# calling of functions.
@profile
def __init__(self):
self.find_words = self.read_find_words("find_words.txt")
self.french_dict = self.read_french_dictionary("french_dictionary.csv")
self.words_replaced = []
self.set_words_replaced = set()
self.set_size = 0
self.process_file("t8.shakespeare.txt","t8.shakespeare.translated.txt")
self.create_frequency_csv()
print("List of words replaced: \n")
print(self.words_replaced)
print()
print("Count of each word replaced:\n")
print(self.find_words)
print()
# uses self.find_words and self.french_dict to write English word, French Word and Number of times the english word is
# replaced(Frequency) in a Frequency.csv file.
def create_frequency_csv(self):
import csv
with open("Frequency.csv",'a+',encoding="utf-8",newline='') as freqfile:
writer = csv.writer(freqfile,delimiter=',')
writer.writerow(["English word","French word","Frequency"])
for key in list(self.find_words.keys()):
writer.writerow([key]+[self.french_dict[key]]+[self.find_words[key]])
return
# reads find_words.txt file and returns dictionary of words.
def read_find_words(self,path):
from collections import defaultdict
words_dict = defaultdict(int)
with open(path,'r',encoding="utf-8") as fileobj:
for line in fileobj:
words_dict[line.split("\n")[0]]=0
return dict(words_dict)
# reads french_dictionary.csv and returns dictionary of words to translate.
def read_french_dictionary(self,path):
import csv
a_csv_file = open(path, "r",encoding="utf-8")
dict_reader = csv.DictReader(a_csv_file,fieldnames=['English','French'])
french_dict = {row['English']:row['French'] for row in dict_reader}
a_csv_file.close()
return french_dict
# process each line of input file. Splits the line on " " and check if each word belongs to list of words to replace.
# If does, the word is replaced with its French alternative and self.find_words value for the replaced word is incremented by one.
def find_replace(self,line,outputobj):
sentence_words = line.split(" ")
new_line = ""
for word in sentence_words:
if word in self.find_words.keys():
new_line+=self.french_dict[word]
new_line+=" "
self.find_words[word]+=1
self.set_size = len(self.set_words_replaced)
self.set_words_replaced.add(word)
if self.set_size!=len(self.set_words_replaced):
self.words_replaced.append(word)
else:
new_line+=word
new_line+=" "
new_line=new_line.rstrip(" ")
outputobj.write(new_line)
return
# opens input file = "t8.shakespeare.txt" and output file = "t8.shakespeare.translated.txt" and perform find and replace
# process line by line
def process_file(self,input_file,output_file):
with open(output_file,'a+',encoding="utf-8") as outputobj:
with open(input_file,'r',encoding="utf-8") as inputobj:
for line in inputobj:
#print(line)
self.find_replace(line,outputobj)
del self.set_words_replaced
del self.set_size
return
if __name__=="__main__":
start = time.time()
# Calling Preprocess object
Preprocess()
print("Total execution time: \n")
print(time.time()-start)