-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
240 lines (195 loc) · 8.41 KB
/
utils.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import os
import csv
from nltk.tokenize import TweetTokenizer
from nltk.corpus import stopwords
from nltk import word_tokenize, pos_tag
from string import punctuation
import codecs
import numpy as np
import pandas as pd
punctuation = set(punctuation)
numbers = set('1234567890')
sws = stopwords.words('english')
sws.extend(["ca","okay","yeah","gon","do","um","yes","get","uh","would","something","rt"])
sws = set(sws)
def remove_repeated_sentences_IBM(data):
'''
remove sentences that are repeated in the IBM data *with the same target*
Args:
data: list of dicts
Returns: new_data, list of dicts containing unique sentences
'''
sentences = dict()
new_data = []
for ins in data:
sentence, target = ins['Sentence'], ins["Target"]
if sentence in sentences and target in sentences[sentence]:
continue
else:
new_data.append(ins)
if sentence not in sentences:
sentences[sentence] = set()
sentences[sentence].add(target)
return new_data
def trim_sentence(sentence_words, target_keywords, target):
'''
Args:
sentence_words: list of strs (words)
target_keywords: most important words in the target this sentence corresponds to
target: the original target
Returns:
sentence_words: list of strs (words)
bool, if True, it means we detected some overlap but the sentence was left unchanged
'''
# see if the first X words are the target or the target + not/nt
kws = len(target_keywords)
len_target = len(target.split())
targetset = set(target.lower().split())
targetset.add("not")
targetset.add("n't")
if sentence_words[:len_target] == target.lower().split() or set(sentence_words[:len_target+1]).issubset(targetset):
# check if the next word is because, as, since, comma or stop.
if "because" in sentence_words[len_target:len_target+3]:
position = sentence_words.index("because")
if sentence_words[position + 1] == "of":
position += 1
elif "as" in sentence_words[len_target:len_target+3]:
position = sentence_words.index("as")
elif "since" in sentence_words[len_target:len_target + 3]:
position = sentence_words.index("since")
elif "," in sentence_words[len_target:len_target + 3]:
position = sentence_words.index(",")
elif "." in sentence_words[len_target:len_target + 3]:
position = sentence_words.index(".")
else:
q=2 #to, so, so that, due to, for, in ordre to
return sentence_words, True # True means it has overlap but it didnt change
return sentence_words[position + 1:], False
else:
return sentence_words, False
def determine_target_keywords(data):
targets = set([ins['Original Target'] for ins in data])
target_keywords = dict()
# I basically postag the target and keep only nouns, verbs and adjectives
for target in targets:
postagged_target = pos_tag(word_tokenize(target.lower()), tagset="universal")
target_keywords[target] = set([w for w, p in postagged_target if p in ["NOUN","VERB","ADJ"] and w not in sws])
return target_keywords
def read_IBM_dataset(dataset_name, trim=True, path='Data/', return_df=False):
'''
Reads in an IBM dataset (for example ArgQP (30k))
Args:
dataset_name: string: 30k (ArgQP in the paper), CS, ArgKP, XArgMining-Arg, XArgMining-Evi
return_df: whether we want to return a dataframe
trim: bool, if True, we shorten some sentences that have a high overlap with the target
(for example, many sentences start like "We should ban abortion because ..." - we only keep what's after "because")
Returns: data, either a list of dicts (each dict corresponds to one sentence) or, if return_df, a dataframe
'''
if dataset_name == "30k": # ArgQP
print('here')
fn = "IBM_Debater_(R)_arg_quality_rank_30k/arg_quality_rank_30k.csv"
cols_to_del = []
cols_to_change = [('topic', "Target"), ('argument', "Sentence"), ('stance_WA', "Stance")]
replacement_dict = {1: "FAVOR", -1: "AGAINST", 0: "NONE"}
df = pd.read_csv(path + fn, sep=",")
for col in cols_to_del:
del df[col]
for oldcol, newcol in cols_to_change:
df[newcol] = df[oldcol]
del df[oldcol]
df["Stance"].replace(replacement_dict, inplace=True)
if return_df:
print("incompatible with trimming")
return df
print("REMOVING instances with low stance clarity")
if dataset_name == "30k":
clear = [r for i, r in df.iterrows() if r['stance_WA_conf'] >= 0.6]
df = pd.DataFrame(clear)
data = df.to_dict('records')
for ins in data:
tokens = word_tokenize(ins['Sentence'])
ins["tokens"] = [t.lower() for t in tokens] # Reminder: this is lowercased, tokenized_tweet is not
ins["Original Target"] = ins["Target"]
ins["Target"] = ins["Target"] + "%" + dataset_name
data = remove_repeated_sentences_IBM(data)
if trim:
target_keywords = determine_target_keywords(data)
for ins in data:
target = ins["Original Target"]
ins["tokens"], it_has_overlap_but_unchanged = trim_sentence(ins["tokens"], target_keywords[ins["Original Target"]], target)
return data
def read_twitter_dataset(dataset_name, debugging=False, path='Data/'):
'''used to be 'read_dataset'.
dataset_name can be "semeval2016", "covid19", "pstance"
if debugging=True, we only process and return info for 2,000 tweets
:return: data (a list of tweets, each tweet is a dictionary) '''
data = []
if not path:
diri = "Data/"
elif path:
diri = path
if dataset_name == "semeval2016":
diri += "semeval2016_stance/StanceDataset/"
elif dataset_name == "pstance":
diri += "PStance/"
elif dataset_name == "covid19":
diri += "covid_unabridged_dataset/"
print("omitting 'noisy' versions of the datasets")
for fn in os.listdir(diri):
if fn.endswith(".csv") and not "noisy" in fn:
with codecs.open(diri + fn, 'r', encoding='utf-8',
errors='ignore') as f:
csvreader = csv.DictReader(f, delimiter=",")
data.extend(list(csvreader))
if debugging:
print("DEBUGGING MODE!")
data = data[:2000]
# Update the names of the targets so they include the dataset name too,
# because one target (Donald Trump) is present in two datasets
tt = TweetTokenizer()
for ins in data:
ins['Target'] = ins['Target'].replace(" ","_")
ins['Target'] += "%" + dataset_name
tokens = tt.tokenize(ins['Tweet'])
ins["tokenized_tweet"] = tokens # not lowercased, original but tokenized tweet
ins["tokens"] = [t.lower() for t in tokens if t.lower() != "#semst" and "\n" not in t.lower()] # this is lowercased and cleaned
return data
# FUNCTIONS FOR LOADING A TXT FILE WITH VECTORS
# First two functions taken/adapted from https://github.com/NLPrinceton/ALaCarte/blob/master/alacarte.py
def make_printable(string):
'''returns printable version of given string
'''
return ''.join(filter(str.isprintable, string))
def load_vectors(vectorfile):
'''loads word embeddings from .txt
Args:
vectorfile: .txt file in "word float ... " format
Returns:
(word, vector) generator
'''
SPACE = ' '
FLOAT = np.float32
words = set()
with open(vectorfile, 'r') as f:
for line in f:
index = line.index(SPACE)
word = make_printable(line[:index])
if not word in words:
words.add(word)
yield word, np.fromstring(line[index + 1:], dtype=FLOAT, sep=SPACE)
# Function taken/adapted from https://github.com/NLPrinceton/ALaCarte/blob/master/alacarte.py
def dump_vectors(vector_dict, vectorfile):
'''dumps embeddings to .txt
Args:
generator: (gram, vector) generator; vector can also be a scalar
vectorfile: .txt file
Returns:
None
'''
with open(vectorfile, 'w') as f:
for gram, vector in vector_dict.items():
numstr = ' '.join(map(str, vector.tolist())) if vector.shape else str(vector)
f.write(gram + ' ' + numstr + '\n')
def find_semcor_paths(semcor_dir="semcor_representations/"):
dirs = [semcor_dir + diri for diri in os.listdir(semcor_dir) if os.path.isdir(semcor_dir + diri)]
return dirs