-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocessing.py
218 lines (165 loc) · 6.8 KB
/
preprocessing.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
from nltk.corpus import stopwords
import nltk
from collections import Counter
import pickle
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
import re
def create_dynamic_stopwords(corpus_path, tfidf_threshold_percentile=10, freq_threshold=0.01):
"""
Create a dynamic list of stopwords based on both TF-IDF scores and word frequencies.
Args:
- corpus_path (str): Path to the corpus file.
- tfidf_threshold_percentile (float): The percentile threshold for considering a word as a stopword based on TF-IDF scores.
- freq_threshold (float): The threshold for considering a word as a stopword based on frequency.
Returns:
- set: A set of dynamically determined stopwords.
"""
# Read the corpus
with open(corpus_path, 'r') as file:
text = file.read()
# Extract lowercase sentences (utterances) from the text
utterances = re.findall(r'\n([a-z ].+)', text)
# Calculate TF-IDF scores
tfidf_vectorizer = TfidfVectorizer()
tfidf_matrix = tfidf_vectorizer.fit_transform(utterances)
feature_names = tfidf_vectorizer.get_feature_names_out()
word_tfidf_scores = tfidf_matrix.sum(axis=0).A1
word_tfidf_dict = dict(zip(feature_names, word_tfidf_scores))
# Determine the TF-IDF threshold for stopwords
tfidf_threshold = np.percentile(list(word_tfidf_dict.values()), tfidf_threshold_percentile)
# Calculate word frequencies
word_freq = Counter(word.lower() for utterance in utterances for word in utterance.split())
total_words = sum(word_freq.values())
# Identify stopwords based on TF-IDF and frequency
stopwords = {word for word, score in word_tfidf_dict.items() if score <= tfidf_threshold}
stopwords.update({word for word, freq in word_freq.items() if freq / total_words > freq_threshold})
return stopwords
def process_rollins(corpus_path, stopwords):
"""
Process the Rollins corpus with stopwords removal from the sentences.
Args:
- corpus_path (str): Path to the Rollins corpus file.
- stopwords (set): A set of stopwords to be filtered out.
Returns:
- list: A list of (spoken_words, referents) pairs.
"""
with open(corpus_path, 'r') as file:
lines = file.readlines()
data = []
spoken_words = []
referents = []
for line in lines:
words = line.strip().split()
if not words:
continue
if words[0].isupper():
referents = words
if spoken_words:
data.append((spoken_words, referents))
spoken_words = []
else:
filtered_words = [word for word in words if word.lower() not in stopwords]
spoken_words = filtered_words
return data
def process_raw_rollins(corpus_path):
"""
Process the Rollins corpus with stopwords removal from the sentences.
Args:
- corpus_path (str): Path to the Rollins corpus file.
- stopwords (set): A set of stopwords to be filtered out.
Returns:
- list: A list of (spoken_words, referents) pairs.
"""
with open(corpus_path, 'r') as file:
lines = file.readlines()
data = []
spoken_words = []
referents = []
for line in lines:
words = line.strip().split()
if not words:
continue
if words[0].isupper():
referents = words
if spoken_words:
data.append((spoken_words, referents))
spoken_words = []
else:
filtered_words = [word for word in words if word.lower()]
spoken_words = filtered_words
return data
def process_gold(lexicon_path):
"""
Process the gold standard lexicon.
Args:
- lexicon_path (str): Path to the gold standard lexicon file.
Returns:
- dict: The gold standard lexicon.
"""
# Open and read the file specified by the lexicon_path
with open(lexicon_path, 'r') as file:
lines = file.readlines()
# Initialize the gold standard lexicon
gold_standard = {}
# Process each line in the file
for line in lines:
word, ref = line.strip().split()
gold_standard.setdefault(ref, []).append(word)
return gold_standard
def process_rollins_combined(corpus_path, static_stopwords, dynamic_stopwords):
"""
Process the Rollins corpus with both static and dynamic stopwords removal.
Args:
- corpus_path (str): Path to the Rollins corpus file.
- static_stopwords (set): A set of static stopwords to be filtered out.
- dynamic_stopwords (set): A set of dynamic stopwords to be filtered out.
Returns:
- list: A list of (spoken_words, referents) pairs.
"""
combined_stopwords = static_stopwords.union(dynamic_stopwords)
with open(corpus_path, 'r') as file:
lines = file.readlines()
data = []
spoken_words = []
referents = []
for line in lines:
words = line.strip().split()
if not words:
continue
if words[0].isupper():
referents = words
if spoken_words:
data.append((spoken_words, referents))
spoken_words = []
else:
filtered_words = [word for word in words if word.lower() not in combined_stopwords]
spoken_words = filtered_words
return data
# Paths to the corpus and lexicon files
corpus_path = "rollins.txt"
lexicon_path = "gold.txt"
nltk.download('stopwords')
static_stopwords = set(stopwords.words('english'))
data_pairs_raw = process_raw_rollins(corpus_path)
# Process the file with static stopwords
data_pairs_static = process_rollins(corpus_path, static_stopwords)
# Create dynamic stopwords list
dynamic_stopwords = create_dynamic_stopwords(corpus_path)
print(dynamic_stopwords)
# Process the file with dynamic stopwords
data_pairs_static = process_rollins(corpus_path, static_stopwords)
data_pairs_dynamic = process_rollins(corpus_path, dynamic_stopwords)
gold_standard = process_gold(lexicon_path)
data_pairs_combined = process_rollins_combined(corpus_path, static_stopwords, dynamic_stopwords)
with open('data_pairs.pkl', 'wb') as file:
pickle.dump(data_pairs_raw, file)
with open('data_pairs_combined.pkl', 'wb') as file:
pickle.dump(data_pairs_combined, file)
with open('data_pairs_static.pkl', 'wb') as file:
pickle.dump(data_pairs_static, file)
with open('data_pairs_dynamic.pkl', 'wb') as file:
pickle.dump(data_pairs_dynamic, file)
# Save the processed gold_standard using pickle
with open('gold_standard.pkl', 'wb') as file:
pickle.dump(gold_standard, file)