-
Notifications
You must be signed in to change notification settings - Fork 5
/
service.py
63 lines (53 loc) · 2.14 KB
/
service.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
import grpc
import argparse
import os
import sys
import imp
from service_spec.DSSMService_pb2 import DSSMRequest
from service_spec.DSSMService_pb2_grpc import DSSMStub
def searchWord(string, file):
with open(file) as myfile:
if string.strip():
for word in string.split(" "):
if word in myfile.read():
myfile.seek(0)
else:
string = string.replace(word, "")
myfile.seek(0)
myfile.close()
return string
# Reading from file
def getVarFromFile(filename):
f = open(filename)
global data
data = imp.load_source('data', '', f)
f.close()
return data
if __name__ == "__main__":
# open a gRPC channel
channel = grpc.insecure_channel('localhost:8001')
stub = DSSMStub(channel)
parser = argparse.ArgumentParser()
parser.add_argument("--qry", type=str, help='query entry (you could only use terms from the model this network trained)')
parser.add_argument("--ans1", type=str, help='first answer to compare relevance with the query (you could only use terms from the model this network trained)')
parser.add_argument("--ans2", type=str, help='second answer to compare relevance with the query (you could only use terms from the model this network trained)')
args = parser.parse_args()
if len(sys.argv) == 1:
parser.print_help()
sys.exit()
# Check whether user's query and answers list suits the model
data = getVarFromFile("variables.txt")
qry = searchWord(args.qry, data.query_wf)
ans_1 = searchWord(args.ans1, data.answer_wf)
ans_2 = searchWord(args.ans2, data.answer_wf)
if not qry.split():
print("Your query entry doesn't include terms that exist in the model.")
elif not ans_1.split():
print("Your entry for first answer doesn't include terms that exist in the model")
elif not ans_2.split():
print("Your entry for second answer doesn't include terms that exist in the model")
else:
response = stub.semantic_modeling(DSSMRequest(qry = qry, ans1 = ans_1, ans2 = ans_2))
print("Query to Answer similarity: ", response.qry_ans_similarity)
print("Query to Answer 2 similarity: ", response.qry_ans2_similarity)
print("\"", max(args.ans1, args.ans2), "\" is a better answer for \"", args.qry, "\"")