-
Notifications
You must be signed in to change notification settings - Fork 0
/
Text_cleaner.py
49 lines (34 loc) · 1.36 KB
/
Text_cleaner.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
from __future__ import print_function
import numpy as np
import pandas as pd
import re
import unicodedata
#word_tokenize accepts a string as an input, not a file.
rawtext_filename = "E:\_Translation\data\eng-fra.txt" #KEY IN PATH OF SOURCE FILE
cleantext_filename = "E:\_Translation\data\eng-fra_clean.txt" #KEY IN PATH OF THE DESTINATION AND CLEAN TEXT FILE
max_length = 8
#File Loading
###################################
df = pd.read_csv(rawtext_filename,header=None,encoding = "utf-8", sep='\t')
###################################
#Converts text to ascii and remove unwanted special characters.
###################################
def unicodeToAscii(s):
return ''.join(
c for c in unicodedata.normalize('NFD', s)
if unicodedata.category(c) != 'Mn'
)
#Removing punctuations from the text
def normalizeString(s):
s = unicodeToAscii(s.lower().strip())
s = re.sub(r"([.!?])", r" \1", s)
s = re.sub(r"[^a-zA-Z.!?]+", r" ", s)
return s
df1=pd.DataFrame()
for i in range(len(df.iloc[:,1])):
if len(df.iloc[i,0].split()) < max_length:
df.iloc[i, 0] = normalizeString(df.iloc[i, 0])
df.iloc[i, 1] = normalizeString(df.iloc[i, 1])
df1 = df1.append(df.loc[i], ignore_index= False)
df1.to_csv(cleantext_filename,sep='\t',header=False,index = False)
print("DONE...")