-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathBEA_feature_correlation.py
174 lines (130 loc) · 5.19 KB
/
BEA_feature_correlation.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
import spacy
import lftk.lftk as lftk
import pandas as pd
from tqdm import tqdm
nlp = spacy.load("en_core_web_sm")
def evaluate_with_readability(searched_features: list):
# list to populate
target_and_features = []
# read data
df = pd.read_csv('data_task/CLEAR.csv')
print(df.head())
# change pandas df to list of dictionaries
data_list_dict = df.to_dict("records")
i = 0
# iterate
for item in tqdm(data_list_dict):
text = item['Excerpt']
readability = item['CAREC_M']
# load into spaCy
doc = nlp(text)
# start LFTK
LFTK = lftk.Extractor(docs = doc)
LFTK.customize(stop_words=True, punctuations=True, round_decimal=3)
# extract
extracted_features = LFTK.extract(features = searched_features)
extracted_features['readability'] = readability
# populate list
target_and_features.append(extracted_features)
# convert back to df
df_with_features = pd.DataFrame(target_and_features)
correlation = df_with_features.corr(method='pearson')
correlation_readability = correlation.readability.sort_values(ascending=False)
correlation_readability.to_csv("correlation_readability.csv")
def evaluate_with_essay_scoring(searched_features: list):
# list to populate
target_and_features = []
# read data
df = pd.read_csv('data_task/asap.csv')
print(df.head())
# change pandas df to list of dictionaries
data_list_dict = df.to_dict("records")
i = 0
# iterate
for item in tqdm(data_list_dict):
text = item['essay']
essay_score = item['domain1_score']
# load into spaCy
doc = nlp(text)
# start LFTK
LFTK = lftk.Extractor(docs = doc)
LFTK.customize(stop_words=True, punctuations=True, round_decimal=3)
# extract
extracted_features = LFTK.extract(features = searched_features)
extracted_features['essay_score'] = essay_score
# populate list
target_and_features.append(extracted_features)
# convert back to df
df_with_features = pd.DataFrame(target_and_features)
correlation = df_with_features.corr(method='pearson')
correlation_essay_score = correlation.essay_score.sort_values(ascending=False)
correlation_essay_score.to_csv("correlation_essay_score.csv")
def evaluate_with_fake_news(searched_features: list):
# list to populate
target_and_features = []
# read data
df = pd.read_csv('data_task/liar.csv')
print(df.head())
# modify
labels = {"FALSE": 0, "barely-true": 1, "half-true": 2, "mostly-true": 3, "TRUE": 4}
df.replace({"label": labels}, inplace=True)
# change pandas df to list of dictionaries
data_list_dict = df.to_dict("records")
i = 0
# iterate
for item in tqdm(data_list_dict):
if item['label'] != 'pants-fire':
text = item['statement']
true_score = item['label']
# load into spaCy
doc = nlp(text)
# start LFTK
LFTK = lftk.Extractor(docs = doc)
LFTK.customize(stop_words=True, punctuations=True, round_decimal=3)
# extract
extracted_features = LFTK.extract(features = searched_features)
extracted_features['true_score'] = true_score
# populate list
target_and_features.append(extracted_features)
# convert back to df
df_with_features = pd.DataFrame(target_and_features)
correlation = df_with_features.corr(method='pearson')
correlation_true_score = correlation.true_score.sort_values(ascending=False)
correlation_true_score.to_csv("correlation_true_score.csv")
def evaluate_with_hate_speech(searched_features: list):
# list to populate
target_and_features = []
# read data
df = pd.read_csv('data_task/hateval2019.csv')
print(df.head())
# change pandas df to list of dictionaries
data_list_dict = df.to_dict("records")
i = 0
# iterate
for item in tqdm(data_list_dict):
text = item['text']
hate_speech = item['HS']
# load into spaCy
doc = nlp(text)
# start LFTK
LFTK = lftk.Extractor(docs = doc)
LFTK.customize(stop_words=True, punctuations=True, round_decimal=3)
# extract
extracted_features = LFTK.extract(features = searched_features)
extracted_features['hate_speech'] = hate_speech
# populate list
target_and_features.append(extracted_features)
# convert back to df
df_with_features = pd.DataFrame(target_and_features)
correlation = df_with_features.corr(method='pearson')
correlation_hate_speech = correlation.hate_speech.sort_values(ascending=False)
correlation_hate_speech.to_csv("correlation_hate_speech.csv")
if __name__ == "__main__":
# Retreive all available features
searched_features = lftk.search_features(return_format = "list_key")
print(f"{len(searched_features)} loaded")
# Evaluate
evaluate_with_hate_speech(searched_features)
evaluate_with_fake_news(searched_features)
evaluate_with_essay_scoring(searched_features)
evaluate_with_readability(searched_features)