-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathM_PreProcess.py
38 lines (32 loc) · 1.6 KB
/
M_PreProcess.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
import re
def preprocess(filename):
f = open(filename, 'r', encoding='utf-8')
cleaned_data = ""
for sentence in f.readlines():
if sentence.strip() == 'References':
break
if len(sentence.split()) > 4:
cleaned_data += sentence
cleaned_data = re.sub(r'\bhttp[s]?://\S+\b', '', cleaned_data) # Remove URLs
cleaned_data = re.sub(r'\s+', ' ', cleaned_data) # Replace multiple spaces with a single space
cleaned_data = re.sub(r'\s([.,!?])', r'\1', cleaned_data) # Remove spaces before punctuation
# Remove multiple dots and inconsistencies
cleaned_data = re.sub(r'\.{2,}', '', cleaned_data) # Remove multiple dots
# cleaned_data = re.sub(r"[^\w\s]", "", cleaned_data)
cleaned_data = re.sub(r'-\s+', '', cleaned_data)
with open("KeyTakeaway/cleanTest.txt", 'w', encoding='utf-8') as f:
f.write(cleaned_data)
def preprocess_sum(filename):
f = open(filename, 'r', encoding='utf-8')
cleaned_data = ""
for sentence in f.readlines():
# if sentence.strip() == 'References':
# break
if len(sentence.split()) > 4:
cleaned_data += sentence
cleaned_data = re.sub(r'\bhttp[s]?://\S+\b', '', cleaned_data) # Remove URLs
cleaned_data = re.sub(r'\.{2,}', '', cleaned_data) # Remove multiple dots
# cleaned_data = re.sub(r"[^\w\s]", "", cleaned_data)
cleaned_data = re.sub(r'-\s+', '', cleaned_data)
with open(f"KeyTakeaway/clean_sum.txt", 'w', encoding='utf-8') as f:
f.write(cleaned_data)