-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict_cause_effect.py
163 lines (122 loc) · 5.77 KB
/
predict_cause_effect.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
from transformers import AutoTokenizer, AutoModel
import pandas as pd
import os
import torch
from nltk.tokenize import TweetTokenizer
from emoji import demojize
import re
import glob
import joblib
########### MODEL PARAMETERS ##################
bert_model = "vinai/bertweet-base" # "bert-large-uncased"; "roberta-large"
dataPath = "result_causal_sentence_prediction"
csv_files = glob.glob(os.path.join(dataPath, "*.csv"))
result_dir = "result_cause_effect_prediction"
# Always predicts batch_size many tweets and stores result in result_dir
batch_size = 10000 #65552 #1024 #512 # # 65552 #32776 #16388 #8194 #2048
cause_effect_model = joblib.load("./model-causal-span/bertEmbeddings_simpleCRF.pkl")
################### LOAD BERT TOKENIZER AND MODEL #############################
tokenizer = AutoTokenizer.from_pretrained(bert_model, padding = "max_length", truncation = True, max_length = 512, return_offsets_mapping=True )
model = AutoModel.from_pretrained(bert_model)
################## LOAD DATA ######################
tuples = []
for file in csv_files:
with open(file, "r") as f:
lines = f.readlines()
for i, line in enumerate(lines):
if i == 0: # header
if line.endswith("\n"):
line = line[:-2]
header = line.split(",")[1:]
else:
index, ll = line.split(",", 1)
ll, prob = ll.rsplit(",", 1)
if prob.endswith("\n"):
prob = prob[:-2]
text, pred = ll.rsplit(",", 1)
if text.startswith('"') and text.endswith('"'):
text = text[1:-1]
#text = text[]
#print(text)
tuples.append((text, pred, prob))
print("N tweets from file:", len(tuples))
df = pd.DataFrame(tuples, columns=["text", "pred", "proba"])
df.pred = pd.to_numeric(df.pred)
df.proba = pd.to_numeric(df.proba)
print("Predicted causal sentences:")
print(df.pred.value_counts())
############ ONLY TAKE CAUSAL SENTENCES ##########################
df_causal = df[df["pred"] == 1]#.sample(n=100, random_state=0) # SAMPLE ONLY FOR TESTING
df_causal["tokenized"] = df_causal["text"].str.split(" ")
########################### Check if cuda available ############################
print("Cuda available: ", torch.cuda.is_available())
device = torch.device('cuda:1' if torch.cuda.is_available() else 'cpu')
print("Selected {} for this notebook".format(device))
################## GET BERT EMBEDDINGS + OTHER FEATURES ###############################""
def get_word_embeddings(sentence, sentence_tokenised):
""" Get word embeddings for each word in sentence """
ids = tokenizer.encode(sentence)
ids_tensor = torch.tensor(ids).unsqueeze(0) # Batch size: 1
word_vectors = model(ids_tensor)[0].squeeze()
word_embeddings_all = []
for word in sentence_tokenised: # average word embeddings of sub-tokens
word_encoded = tokenizer.encode(word)
word_encoded.remove(tokenizer.cls_token_id)
word_encoded.remove(tokenizer.sep_token_id)
word_indices = [ids.index(encoded_id) for encoded_id in word_encoded ]
# average all sub_word vectors of word
word_vector = torch.zeros((768))
for sub_token_id in word_indices:
word_vector += word_vectors[sub_token_id]
word_vector /= len(word_indices)
word_embeddings_all.append(word_vector)
return word_embeddings_all
def word2features(word, i, wordembedding):
features = {
'word.lower()': word.lower(),
'word.isupper()': word.isupper(),
'word.isdigit()': word.isdigit(),
'wordlength': len(word),
'wordinitialcap': word[0].isupper(),
'wordmixedcap': len([x for x in word[1:] if x.isupper()])>0,
'wordallcap': len([x for x in word if x.isupper()])==len(word),
'distfromsentbegin': i
}
# here you add 300 (fastText) / 768 (Bert) features (one for each vector component)
for iv,value in enumerate(wordembedding):
features['v{}'.format(iv)]=value
return features
def sent2features(sentence, tokenized):
word_vectors = get_word_embeddings(sentence, tokenized)
return [word2features(tokenized[i], i, word_vectors[i]) for i in range(len(tokenized))]
######################## Prediction loop ###################################
for i in range(0, df_causal.shape[0], batch_size):
print(i)
tweet_subset = df_causal[i:i+batch_size]
try:
X = [sent2features(sentence, tokenized)
for sentence, tokenized in zip(tweet_subset.text.values.tolist()
, tweet_subset.tokenized.values.tolist())]
predictions = cause_effect_model.predict(X)
cause_effect_DF = pd.DataFrame({"text":tweet_subset.text,
"tokenized": tweet_subset.tokenized,
"predictions": predictions})
except:
print("Error in batch -> execute single tweets...")
X = []
texts = []
tokenized_texts = []
for sentence, tokenized in zip(tweet_subset.text.values.tolist() , tweet_subset.tokenized.values.tolist()):
try:
X.append(sent2features(sentence, tokenized))
texts.append(sentence)
tokenized_texts.append(tokenized)
except:
print("\nError and ignore tweet:")
print("sentence:\t", sentence)
print("tokenized:\t", tokenized)
predictions = cause_effect_model.predict(X)
cause_effect_DF = pd.DataFrame({"text":texts,
"tokenized": tokenized_texts,
"predictions": predictions})
cause_effect_DF.to_csv(result_dir+"/cause_effect_predictions_part_{}.csv".format(i), sep=";")