-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_semantic_features.py
169 lines (162 loc) · 9.09 KB
/
generate_semantic_features.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
import pickle
import numpy as np
import pandas as pd
from dependency_parse import get_pos_dep
from semantic_similarity import align
from features import *
import gzip
def train_features():
with gzip.open('data_dump/stanfordData_train_ner.nlp', 'rb') as handle:
rows = []
while True:
try:
d = pickle.load(handle)
id = d['id']
print(id)
is_duplicate = d['is_duplicate']
#print(d['q1']['raw'])
#print(d['q2']['raw'])
S = get_pos_dep(d['q1']['toks'], d['q1']['deps'])
T = get_pos_dep(d['q2']['toks'], d['q2']['deps'])
#print(S)
#print(T)
A = align(S,T)
#print(A)
#print('Len(S):',len(S))
#print('Len(T):', len(T))
#print('Len(A):', len(A))
# Semantic Similarity Features
S_sem_sim = percentage_semantic_similarity_one(S, A)
T_sem_sim = percentage_semantic_similarity_one(T, A)
sem_sim = percentage_semantic_similarity_both(S, T, A)
# Noun Features
S_unmatch_n, T_unmatch_n = number_unmatched(S, T, A, 'n', inferred_pos=True)
S_unmatch_n_p, T_unmatch_n_p = percent_unmatched(S, T, A, 'n', inferred_pos=True)
# Adjective Features
S_unmatch_a, T_unmatch_a = number_unmatched(S, T, A, 'a', inferred_pos=True)
S_unmatch_a_p, T_unmatch_a_p = percent_unmatched(S, T, A, 'a', inferred_pos=True)
# Verb Features
S_unmatch_v, T_unmatch_v = number_unmatched(S, T, A, 'v', inferred_pos=True)
S_unmatch_v_p, T_unmatch_v_p = percent_unmatched(S, T, A, 'v', inferred_pos=True)
# Personal Pronoun Feature
S_unmatch_pp, T_unmatch_pp = number_unmatched(S, T, A, 'PRP', inferred_pos=False)
S_unmatch_pp_p, T_unmatch_pp_p = percent_unmatched(S, T, A, 'PRP', inferred_pos=False)
# WH-Pronoun Feature
S_unmatch_wp, T_unmatch_wp = number_unmatched(S, T, A, 'WP', inferred_pos=False)
S_unmatch_wp_p, T_unmatch_wp_p = percent_unmatched(S, T, A, 'WP', inferred_pos=False)
# Numbers Feature
S_unmatch_num, T_unmatch_num = number_unmatched(S, T, A, 'CD', inferred_pos=False)
S_unmatch_num_p, T_unmatch_num_p = percent_unmatched(S, T, A, 'CD', inferred_pos=False)
# NER Feature
S_unmatch_ner, T_unmatch_ner = ner_unmatched(S, T)
# Length Difference Feature
len_dif = len_difference(S, T)
len_dif_p = len_difference_p(S, T)
# Put all features in a row
features_row = [id,
S_sem_sim, T_sem_sim, sem_sim,
S_unmatch_n, T_unmatch_n, S_unmatch_n_p, T_unmatch_n_p,
S_unmatch_a, T_unmatch_a, S_unmatch_a_p, T_unmatch_a_p,
S_unmatch_v, T_unmatch_v, S_unmatch_v_p, T_unmatch_v_p,
S_unmatch_pp, T_unmatch_pp, S_unmatch_pp_p, T_unmatch_pp_p,
S_unmatch_wp, T_unmatch_wp, S_unmatch_wp_p, T_unmatch_wp_p,
S_unmatch_num, T_unmatch_num, S_unmatch_num_p, T_unmatch_num_p,
S_unmatch_ner, T_unmatch_ner,
len_dif, len_dif_p,
is_duplicate]
rows.append(features_row)
except EOFError:
break
columns = ['id',
'S_sem_sim', 'T_sem_sim', 'sem_sim',
'S_unmatch_n','T_unmatch_n','S_unmatch_n_p','T_unmatch_n_p',
'S_unmatch_a','T_unmatch_a','S_unmatch_a_p','T_unmatch_a_p',
'S_unmatch_v','T_unmatch_v','S_unmatch_v_p','T_unmatch_v_p',
'S_unmatch_pp','T_unmatch_pp','S_unmatch_pp_p','T_unmatch_pp_p',
'S_unmatch_wp','T_unmatch_wp','S_unmatch_wp_p','T_unmatch_wp_p',
'S_unmatch_num', 'T_unmatch_num', 'S_unmatch_num_p', 'T_unmatch_num_p',
'S_unmatch_ner', 'T_unmatch_ner',
'len_dif', 'len_dif_p',
'is_duplicate']
df = pd.DataFrame(np.array(rows), columns=columns)
df.to_csv('Features/dean_train_features.csv', index=False)
def test_features():
frames = []
for i in range(1,6):
filename = 'data_dump/stanfordData_test_p' + str(i) + '.nlp'
rows = []
with gzip.open(filename, 'rb') as handle:
while True:
try:
d = pickle.load(handle)
id = d['id']
print(id)
#print(d['q1']['raw'])
#print(d['q2']['raw'])
S = get_pos_dep(d['q1']['toks'], d['q1']['deps'])
T = get_pos_dep(d['q2']['toks'], d['q2']['deps'])
# print(S)
# print(T)
A = align(S, T)
# print(A)
# print('Len(S):',len(S))
# print('Len(T):', len(T))
# print('Len(A):', len(A))
# Semantic Similarity Features
S_sem_sim = percentage_semantic_similarity_one(S, A)
T_sem_sim = percentage_semantic_similarity_one(T, A)
sem_sim = percentage_semantic_similarity_both(S, T, A)
# Noun Features
S_unmatch_n, T_unmatch_n = number_unmatched(S, T, A, 'n', inferred_pos=True)
S_unmatch_n_p, T_unmatch_n_p = percent_unmatched(S, T, A, 'n', inferred_pos=True)
# Adjective Features
S_unmatch_a, T_unmatch_a = number_unmatched(S, T, A, 'a', inferred_pos=True)
S_unmatch_a_p, T_unmatch_a_p = percent_unmatched(S, T, A, 'a', inferred_pos=True)
# Verb Features
S_unmatch_v, T_unmatch_v = number_unmatched(S, T, A, 'v', inferred_pos=True)
S_unmatch_v_p, T_unmatch_v_p = percent_unmatched(S, T, A, 'v', inferred_pos=True)
# Personal Pronoun Feature
S_unmatch_pp, T_unmatch_pp = number_unmatched(S, T, A, 'PRP', inferred_pos=False)
S_unmatch_pp_p, T_unmatch_pp_p = percent_unmatched(S, T, A, 'PRP', inferred_pos=False)
# WH-Pronoun Feature
S_unmatch_wp, T_unmatch_wp = number_unmatched(S, T, A, 'WP', inferred_pos=False)
S_unmatch_wp_p, T_unmatch_wp_p = percent_unmatched(S, T, A, 'WP', inferred_pos=False)
# Numbers Feature
S_unmatch_num, T_unmatch_num = number_unmatched(S, T, A, 'CD', inferred_pos=False)
S_unmatch_num_p, T_unmatch_num_p = percent_unmatched(S, T, A, 'CD', inferred_pos=False)
# NER Feature
S_unmatch_ner, T_unmatch_ner = ner_unmatched(S, T)
# Length Difference Feature
len_dif = len_difference(S, T)
len_dif_p = len_difference_p(S, T)
# Put all features in a row
features_row = [id,
S_sem_sim, T_sem_sim, sem_sim,
S_unmatch_n, T_unmatch_n, S_unmatch_n_p, T_unmatch_n_p,
S_unmatch_a, T_unmatch_a, S_unmatch_a_p, T_unmatch_a_p,
S_unmatch_v, T_unmatch_v, S_unmatch_v_p, T_unmatch_v_p,
S_unmatch_pp, T_unmatch_pp, S_unmatch_pp_p, T_unmatch_pp_p,
S_unmatch_wp, T_unmatch_wp, S_unmatch_wp_p, T_unmatch_wp_p,
S_unmatch_num, T_unmatch_num, S_unmatch_num_p, T_unmatch_num_p,
S_unmatch_ner, T_unmatch_ner,
len_dif, len_dif_p]
rows.append(features_row)
except EOFError:
break
columns = ['id',
'S_sem_sim', 'T_sem_sim', 'sem_sim',
'S_unmatch_n', 'T_unmatch_n', 'S_unmatch_n_p', 'T_unmatch_n_p',
'S_unmatch_a', 'T_unmatch_a', 'S_unmatch_a_p', 'T_unmatch_a_p',
'S_unmatch_v', 'T_unmatch_v', 'S_unmatch_v_p', 'T_unmatch_v_p',
'S_unmatch_pp', 'T_unmatch_pp', 'S_unmatch_pp_p', 'T_unmatch_pp_p',
'S_unmatch_wp', 'T_unmatch_wp', 'S_unmatch_wp_p', 'T_unmatch_wp_p',
'S_unmatch_num', 'T_unmatch_num', 'S_unmatch_num_p', 'T_unmatch_num_p',
'S_unmatch_ner', 'T_unmatch_ner',
'len_dif', 'len_dif_p']
df_i = pd.DataFrame(np.array(rows), columns=columns)
frames.append(df_i)
df = pd.concat(frames)
df.to_csv('Features/dean_test_features.csv', index=False)
# Actually generate the feature csvs here.
#train_features()
test_features()