-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.py
More file actions
64 lines (55 loc) · 1.82 KB
/
Copy pathpreprocess.py
File metadata and controls
64 lines (55 loc) · 1.82 KB
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
import pandas as pd
import os
import re
import time
from os import walk
from tqdm import tqdm
def get_file_list(dir):
"""
This function gets List of all files in a directory
Parametrs
---------
Dir:
Directory path in workspace containing all Documents
Returns
-------
files: List
Returns List of all files in a directory
"""
files = os.listdir(dir) # lists all files in the directory
# files.reverse()
print(files)
return files
def preprocess(temp):
"""
This function preprocess all files and overwrites the original files\n
This is done by using custom regular expression to elemenate useless charecters from file
Parametrs
---------
NULL
Returns
-------
NULL
"""
print(tqdm)
file_list = get_file_list(temp)
# print(file_list)
for i in tqdm(range(len(file_list))):
file_path='./temp/'+file_list[i]
with open(file_path, encoding="utf8", mode="r+" ,errors='ignore') as file: #openining encoding files by ignoring errors
print(file)
file_data = file.read()
file_data=file_data.lower()
# print(file_data[0])
file_data = re.sub(r'[\n\r\s ,.;"\'’”()|/]',"",file_data) #regex to remove unnecessary charecters in file
# print(file_data)
file.seek(0) #sets file current positon to 0 so new data overwrites original data
file.write(file_data) # writing data back to file
file.truncate() #truncate until current positon of file (input descriptor)
# return
if __name__=='__main__':
time_start=time.time()
# get_file_list("./temp")
preprocess()
time_end=time.time()
print(time_end-time_start)