-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre_process.py
80 lines (52 loc) · 1.94 KB
/
pre_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
import nltk
import re
import io
def remove_unusual_line_terminators(filepath):
with io.open(filepath, 'r', newline='', encoding='utf-8') as file:
text = file.read()
# Remove unusual line terminators
text = text.replace('\r', '').replace('\x0b', '').replace('\x0c', '')
with io.open(filepath, 'w', encoding='utf-8') as file:
file.write(text)
print(f"Unusual line terminators removed. Output saved at: {filepath}")
def main():
"""
Remove English words, numbers and emojis from the raw text (.txt files)
"""
filename = 'data/input/v3/raw.txt'
remove_unusual_line_terminators(filename)
with open(filename, 'r', encoding='utf-8') as f:
text = f.read()
print("File read")
# Tokenize the text using nltk
text = nltk.word_tokenize(text)
print("Text tokenized")
# Clean the text
text = clean_text(text)
text = remove_english_words(text)
text = remove_emoji(text)
# save the cleaned text
with open('data/input/v3/pre.txt', 'w') as f:
f.write(' '.join(text))
print("Text cleaned")
def clean_text(words):
# Remove numbers
words = [re.sub(r'\d+', '', word) for word in words]
print("Numbers removed")
return words
def remove_english_words(words):
# Define a regular expression pattern to match English words
english_word_pattern = r'[A-Za-z]+'
# Filter out English words
non_english_words = [word for word in words if not re.match(english_word_pattern, word)]
print("English words removed")
return non_english_words
def remove_emoji(words):
# Define a regular expression pattern to match emojis
emoji_pattern = r'[\U0001F600-\U0001F64F\U0001F300-\U0001F5FF\U0001F680-\U0001F6FF\U0001F1E0-\U0001F1FF]+'
# Filter out emojis
non_emoji_words = [word for word in words if not re.match(emoji_pattern, word)]
print("Emojis removed")
return non_emoji_words
if __name__ == '__main__':
main()