-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanswer_process.py
More file actions
139 lines (113 loc) · 4 KB
/
answer_process.py
File metadata and controls
139 lines (113 loc) · 4 KB
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
import pandas as pd
import os
import random
import time
import json
import pickle
import numpy as np
# from sentence_transformers import SentenceTransformer, util
##### for generate triple from answer######
def get_triple(model_name):
triple_output_file = model_name + "_triple.txt"
input_file = model_name + "_answer.txt"
current_i = 0
intentions = []
needs = []
final_dict = {}
flag = False
flag_not_error = True
with open(input_file,'r') as f:
for lines in f.readlines():
if lines == '\n':continue
elif lines[:7].lower() == 'session':
current_session = lines.strip().split(':')[1].strip()
elif lines[:5].lower() == 'error': flag_not_error=False
elif lines[:5].lower()=='needs':
flag = True
elif lines[:9].lower()=='intention':
print("intention: ", lines)
try:
answer = lines.strip().split(':')[1].strip()
except:continue
if flag:
needs.append(answer)
else:
intentions.append(answer)
elif lines[:5]=='-----':
if flag_not_error:
final_dict[current_i]=[current_session,intentions,needs]
current_i+=1
intentions = []
needs = []
flag_not_error = True
flag = False
with open(triple_output_file,'w') as f:
for i in final_dict:
sessions,intentions,needs = final_dict[i]
print(sessions,intentions,needs)
if len(needs)<len(intentions): needs.extend("null" for _ in range(len(intentions)-len(needs)))
for idx,intention in enumerate(intentions):
f.write(str(sessions)+'\t'+str(intention)+'\t'+str(needs[idx])+'\n')
##### for session idx #######
def get_session_idx_for_transformer():
result_file = 'session.pkl'
session_all =[]
s = []
last_session = 'start'
curr_id = 0
with open('result_triple.txt','r') as f:
for i,lines in enumerate(f.readlines()):
lists = lines.strip().split('\t')
session = lists[0]
if last_session == 'start':
last_session = session
s.append(i)
elif last_session ==session:
s.append(i)
else:
session_all.append(s)
s = [i]
last_session = session
session_all.append(s)
with open(result_file,'wb') as f:
pickle.dump(session_all,f)
#### for all intentions ####
def get_intention_sentence_for_transformer():
result_file = 'sentences.pkl'
session_all =[]
with open('result_triple.txt','r') as f:
for lines in f.readlines():
lists = lines.strip().split('\t')
intentions = lists[1]
session_all.append(intentions)
with open(result_file,'wb') as f:
pickle.dump(session_all,f)
### get graph ####
def cal_intention_sim():
with open('sentences.pkl','rb') as f:
sentences = pickle.load(f)
model = SentenceTransformer('all-mpnet-base-v2')
## try other training dataset
#Encode all sentences
embeddings = model.encode(sentences)
#Compute cosine similarity between all pairs
cos_sim = util.cos_sim(embeddings, embeddings)
mat = cos_sim.numpy()
with open('session.pkl','rb') as f:
sen_idx = pickle.load(f)
all_row = []
all_col = []
for idxs in sen_idx:
for i in range(len(idxs)-1):
for j in range(i+1,len(idxs)):
all_row.append(idxs[i])
all_col.append(idxs[j])
mat[all_row,all_col] = 0
mat[all_col,all_row]=0
final_mat = mat - np.eye(len(mat))
with open('intention_score_mpnet.pkl','wb') as f:
pickle.dump(final_mat,f)
if __name__ == '__main__':
for model_name in ['gpt3.5', 'gpt4']:
get_triple(model_name)
print('triple done' + model_name)