-
Notifications
You must be signed in to change notification settings - Fork 0
/
JurivocData.py
338 lines (280 loc) · 13.1 KB
/
JurivocData.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import os
import pandas as pd
import re
blockS_ID = ["UF","BT","SN","SA","USE","UFA","NT","USA","AND"]
LANGUAGE_DICT = {"ITA":"it","GER":"de"}
langID = ['GER','ITA']
class dataset:
def __init__(self, directory_input):
# directory input files
self.directoryFile = directory_input
# Save all Name of file and dataframe
self.recordOutput = []
def readFiles(self) -> list:
dataset = []
source_language = []
for f in os.listdir(self.directoryFile):
nameFile = f
filename = os.path.splitext(os.path.basename(f))[0]
fileInput = os.path.join(self.directoryFile, f)
if os.path.isfile(fileInput):
f = open(fileInput,'r', encoding="ISO-8859-1")
rows = f.readlines()
dfGetStructure = pd.DataFrame(
(
# read the data and save in the dataframe
[len(row)-len(row.lstrip()), row.replace("\n", "")] for row in rows
),
columns = ("level","title")
)
# Remove all rows not necessary
dfGetStructure.drop(dfGetStructure[dfGetStructure["level"] == 0].index, inplace=True)
dfGetStructure['title'] = dfGetStructure['title'].str.lstrip()
# remove all row when title row is empty
dfGetStructure.drop(dfGetStructure[dfGetStructure["title"] == ''].index, inplace=True)
df = dfGetStructure.reset_index(drop=True)
print("File: {}".format(filename))
if "_fre_" in filename:
# Update df for language
source_language.append(self.language_processing(df, filename))
else:
# Preprocessing
dftitles = self.update_titles(df)
# Add block Column
dfblock = self.add_block_column(dftitles)
# Add title_block Column
dftitle_block = self.add_title_block(dfblock)
# Preporcessing all
dfUpdateblock = self.update_title_block(dftitle_block)
dfUpdateBT = self.update_title_block_joint(dfUpdateblock,'BT')
dfUpdateUF = self.update_title_block_joint(dfUpdateBT,'UF')
dfUpdateUSA = self.update_title_block_joint(dfUpdateUF,'USA')
dfUpdateAND = self.update_title_block_joint(dfUpdateUSA,'AND')
dfUpdateSA = self.update_title_block_joint(dfUpdateAND,'SA')
dfOutput = self.update_sn_block(dfUpdateSA)
# if title is a number then convert title in 'c_'+title
dfOutput['title'] = dfOutput['title'].apply(self.update_title_dataframe)
dataset.append([filename,dfOutput])
# Preprocessing for language DF
if len(source_language) > 0:
dataset.append(["dbLanguage",pd.concat(source_language)])
return dataset
def update_title_dataframe(self,title:str) -> str:
stitle = ''
if re.match('^[0-9]+$',title):
stitle = 'C_'+title
else:
stitle = title
return stitle
def update_titles(self, df:pd.DataFrame) -> pd.DataFrame:
print("Update title ...")
# Evaluate and update dataframe
indexMax = pd.Series(df['level'].squeeze()).index.max()
data_duplicate = []
data = []
auxtitle = ''
auxtitle_full = ''
level_nextValue = 0
for idx, row in df.iterrows():
# Update title header
if idx < indexMax:
nextIdx = idx+1
level_nextValue = df.at[idx+1,"level"]
if int(row['level']) == 1:
if row['level'] == level_nextValue:
# Update title
title = row['title'] + ' ' + df.at[nextIdx,'title']
data.append([row['level'],title])
auxtitle = df.at[nextIdx,'title']
auxtitle_full = title
else:
if row['title'] != auxtitle:
data.append([row['level'],row['title']])
auxtitle = ''
#else:
# data.append([row['level'],auxtitle_full])
else:
if row['title'] != auxtitle:
data.append([row['level'],row['title']])
else:
data.append([row['level'],auxtitle_full])
dfProcess = pd.DataFrame(data=data,columns=['level','title'])
return dfProcess
def get_block(self,level,title) -> str:
Bock_Key = ""
if int(level ) > 1:
try:
idblock = title.split()[0].lstrip()
nItem = len(title.split())
if nItem > 1:
if idblock in blockS_ID:
Bock_Key = idblock
elif idblock in langID:
Bock_Key = idblock
else:
Bock_Key = ''
else:
Bock_Key = ''
except:
Bock_Key = ''
return Bock_Key
def add_block_column(self,df:pd.DataFrame) -> pd.DataFrame:
print("Add block ...")
# Add block
df["block"] = df.apply(lambda t : self.get_block(t['level'],t['title']), axis=1)
return df
def split_title(self,level:str,block:str,title:str) -> str:
titleblock = ''
if int(level) > 1:
if bool(block):
try:
listtitle = title.split()
listtitle.remove(block)
titleblock = " ".join(listtitle).lstrip()
except:
titleblock = title
else:
titleblock = title
return titleblock
def add_title_block(self,df:pd.DataFrame) -> pd.DataFrame:
print("Add Title Block ...")
df["title_block"] = df.apply(lambda t : self.split_title(t['level'],t['block'],t['title']), axis=1)
return df
def update_title_block(self,df:pd.DataFrame) -> pd.DataFrame:
print("Update Title Block ...")
data = []
blockAux = ''
titleAux = ''
for idx,row in df.iterrows():
nlevel = int(row['level'])
block = row['block']
if nlevel == 1:
titleAux = row['title']
data.append([row['level'],row['title'],row['block'],row['title_block']])
else:
if block:
# Get block Id
blockAux = block
data.append([row['level'],titleAux,block,row['title_block']])
else:
data.append([row['level'],titleAux,blockAux,row['title_block']])
dfUpdate = pd.DataFrame(data = data,columns=['level','title','block','title_block'])
return dfUpdate
def update_title_block_joint(self,df:pd.DataFrame,blockId :str) -> pd.DataFrame:
# Evaluate and update dataframe
indexMax = pd.Series(df['level'].squeeze()).index.max()
data_duplicate = []
data = []
auxtitle = ''
nextIdx = 0
for idx, row in df.iterrows():
level = row['level']
titleH = row['title']
block = row['block']
titleBlock = row['title_block']
# Update title header
if idx < indexMax:
nextIdx = idx+1
sTitleBlock = ""
if block == blockId:
if titleBlock == auxtitle:
data_duplicate.append([titleH,titleBlock])
else:
# find text how title header
dfTmp = df[df['title'].isin([titleBlock])]
if len(dfTmp) > 0:
sTitleBlock = titleBlock
else:
# Update title
title_joint = titleBlock + ' ' + df.iloc[nextIdx].at['title_block']
#print('title {} | block {} | title {}'.format(titleH,block,title_joint))
dfTmp2 = df[df['title'].isin([title_joint])]
if len(dfTmp2) > 0:
auxtitle = df.iloc[nextIdx].at['title_block']
sTitleBlock = title_joint
else:
sTitleBlock = titleBlock
if len(sTitleBlock) > 0:
data.append([level,titleH,block,sTitleBlock])
else:
data.append([level,titleH,block,titleBlock])
dfOtput = pd.DataFrame(data=data,columns=['level','title','block','title_block'])
return dfOtput
def update_sn_block(self,df:pd.DataFrame) -> pd.DataFrame:
print('Update dataframe SN...')
titlesKey = pd.Series(df['title'].to_list()).drop_duplicates().to_list()
data = []
datalist = []
for title in titlesKey:
# Get Dataframe block from title id
dfFilter = df[df['title'].isin([title])]
blocks = pd.Series(dfFilter['block'].to_list()).drop_duplicates().to_list()
if 'SN' in blocks:
#Filter SN in Dataframe
dfFilterSN = dfFilter[dfFilter['block'].isin(["SN"])]
if len(dfFilterSN) > 1:
# Get all SN title
str_note = " ".join(str(x) for x in dfFilterSN["title_block"].to_list())
# Drop all SN row
dftemp = dfFilter.drop(dfFilter[dfFilter["block"] == "SN"].index)
for l in dftemp.values.tolist():
datalist.append(l)
# Add row SN in dataframe
datalist.append(["3",title,"SN",str_note])
else:
for l in dfFilter.values.tolist():
datalist.append(l)
else:
for l in dfFilter.values.tolist():
datalist.append(l)
dfTmp = pd.DataFrame(data = datalist, columns=["level","title","block","title_block"])
return dfTmp
def preprocessing_titles(self,df:pd.DataFrame) -> pd.DataFrame:
# Evaluate and update dataframe
indexMax = pd.Series(df['level'].squeeze()).index.max()
data = []
auxtitle = ''
auxtitle_full = ''
level_nextValue = 0
for idx,row in df.iterrows():
nlevel = int(row['level'])
if nlevel == 1:
data.append([row['level'],row['title']])
elif nlevel == 3:
# Update title header
if idx < indexMax:
nextIdx = idx+1
level_nextValue = int(df.at[idx+1,"level"])
if level_nextValue == 8:
# Update title
title = row['title'] + ' ' + df.at[nextIdx,'title']
data.append([row['level'],title])
else:
data.append([row['level'],row['title']])
dfProcess = pd.DataFrame(data=data,columns=['level','title'])
return dfProcess
def language_processing(self, df:pd.DataFrame, nameFile: str) -> pd.DataFrame:
dftitles = self.update_titles(df)
#dftitles.to_csv(nameFile+'_titles.csv',index=False)
# this function of preprocessing, fixes the problems when the phrase is jump in new line
dfTitles_preprocessing = self.preprocessing_titles(dftitles)
#dfTitles_preprocessing.to_csv(nameFile+'_preprocessing_titles.csv',index=False)
df_block = self.add_block_column(dfTitles_preprocessing)
df_title = self.add_title_block(df_block)
dfUpdateBT = self.update_title_block_joint(df_title,'BT')
dfUpdateUF = self.update_title_block_joint(dfUpdateBT,'UF')
dfUpdateUSA = self.update_title_block_joint(dfUpdateUF,'USA')
dfUpdateAND = self.update_title_block_joint(dfUpdateUSA,'AND')
dfUpdateSA = self.update_title_block_joint(dfUpdateAND,'SA')
dfLanguage = self.update_title_block(dfUpdateSA)
dftmp = dfLanguage[dfLanguage['level'] > 1]
dfTmp2 = dftmp.reset_index(drop=True)
dfOutputLanguage = dfTmp2.drop(['level'], axis=1)
dfOutput = dfOutputLanguage.rename(columns={"block":"language","title_block":"title_traduction"})
dfOutput['language'] = dfOutput['language'].apply([lambda l : LANGUAGE_DICT[l]])
# if title is a number then convert title in 'c_'+title
dfOutput['title'] = dfOutput['title'].apply(self.update_title_dataframe)
return dfOutput
def read_file(self) -> list:
# Return a list with name of file and dataframe
return self.readFiles()