forked from hsamand/casmacat-cat-server
-
Notifications
You must be signed in to change notification settings - Fork 3
/
paraphrase-client.py
executable file
·47 lines (41 loc) · 1.18 KB
/
paraphrase-client.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
#!/usr/bin/env python
import json
import optparse
import urllib
import sys
def main():
parser = optparse.OptionParser("%prog [args] URL")
parser.add_option("-n", "--number-options", dest="noptions",
help="Number of options", type="int")
parser.set_defaults(
noptions = 10
)
options,args = parser.parse_args(sys.argv)
if len(args) < 2:
parser.error("Need to specify a URL")
url = args[1]
print "e.g. I will || give a lecture || tomorrow"
while True:
print "in> ",
line = sys.stdin.readline()
if not line: break
fields = line[:-1].split(" || ")
if len(fields) != 3:
print "Error: select a segment for rephrasing as in the example"
continue
params = urllib.urlencode({"q": line[:-1]})
ufh = urllib.urlopen(url + "?%s" % params)
raw_resp = ufh.read()
resp = json.loads(raw_resp)
if resp["errors"]:
print "Error: "
print "\n".join(resp["errors"])
else:
paraphrases = resp["paraphrases"][:options.noptions]
print
for p in paraphrases:
print "%4f\t\t" % p[1],
print fields[0],"\033[94m",p[0],"\033[0m",fields[2]
print
if __name__ == "__main__":
main()