· 7 years ago · Jul 27, 2018, 09:08 PM
1import requests
2import json
3import fnmatch
4import os
5import sys
6
7z = True
8try: color = sys.stdout.shell
9except AttributeError: z = False
10
11# User token, required for API access
12oauth_token = '00ae7c405db245a496eca2a0b805cf8e'
13
14# API request data: Language and text to be analyzed
15user_language = "eng"
16api_mode = "ecommerce"
17output_mode = "dialogflow"
18
19
20for file in os.listdir("."):
21 if fnmatch.fnmatchcase(file, "*_input.utterances.txt"):
22 with open(file, "r+") as f:
23 f.readline()
24 t = f.tell()
25 user_sentences = [f.readline().rstrip("\n")]
26 intent = user_sentences[0].rstrip("?")
27 output_json = []
28 print(intent,end=" ")
29
30 for sentence in user_sentences:
31 e = 200
32
33 # Building the POST request to rewriting analysis endpoint
34 endpoint = "https://svc02.api.bitext.com/variants/"
35 headers = {"Authorization": "bearer " + oauth_token, "Content-Type": "application/json"}
36 header = {"Authorization": "bearer " + oauth_token}
37 params = {"language": user_language, "mode": api_mode, "text": sentence, "intent": intent,
38 "politeness": False, "negation": False, "all_numbers": False, "output": output_mode}
39
40 # Sending the POST request
41 res = requests.post(endpoint, headers=headers, data=json.dumps(params))
42
43 # Processing the result of the POST request
44 post_result = json.loads(res.text).get("success") # Success of the request
45 post_result_code = res.status_code # Error code, if applicable
46 post_msg = json.loads(res.text).get("message") # Error message, if applicable
47 action_id = json.loads(res.text).get("resultid") # Identifier to request the analysis results
48
49 # 401 is the error code corresponding to an invalid token
50 if post_result_code == 401:
51 print("Your authentication token is not correct\n")
52
53 if (post_result):
54
55 # GET request loop, using the response identifier returned in the POST answer
56 analysis = None
57 while analysis == None:
58 res = requests.get(endpoint + action_id + "/", headers=headers)
59 if res.status_code == 500:
60 e = 500
61 break
62 if res.status_code == 200:
63 analysis = json.loads(res.text)
64
65 if z:
66 if e==200:
67 color.write("200\n","STRING")
68 else:
69 color.write("500\n","COMMENT")
70 else:
71 print(e)
72
73 if e==500:
74 f.seek(0)
75 g = f.readline().strip()
76 os.remove(g.replace("_input","")+".convo.txt")
77 break
78 if "trainingPhrases" in analysis:
79 # The list will have more than one index if the text is composed of different sentences.
80 for item in analysis["trainingPhrases"]:
81 output_json.append(item)
82
83 if e==500:break
84
85 s = ""
86 f.seek(t)
87 for a in output_json:
88 s = ""
89 b = str(a)
90 c = b.count("'text': '")
91 for i in range(c):
92 t = b[:]
93 s += b.split("'text': '",1)[1].split("'",1)[0]
94 b = t.split("'text': '",1)[1].split("'",1)[1]
95 if c != 0:
96 if a == output_json[-1]:
97 f.write(s.capitalize())
98 else:
99 f.write(s.capitalize()+"\n")
100
101 f.truncate()