Skip to content

Commit

Permalink
Adapted to Dialogflow V2 API
Browse files Browse the repository at this point in the history
  • Loading branch information
Hari Krishna Akurathi committed Apr 19, 2018
1 parent 0887d39 commit 73eddce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
watson_analyser.analyse_annotations("WebApplicationsAnnotations_Watson.json", "WebApplicationsCorpus.json", "WebApplicationsAnalysis_Watson.json")

##dialogflow
dialogflow_analyser = DialogflowAnalyser("api_key")
dialogflow_analyser = DialogflowAnalyser("project_id")
dialogflow_analyser.get_annotations("WebApplicationsCorpus.json", "WebApplicationsAnnotations_Dialogflow.json")
dialogflow_analyser.analyse_annotations("WebApplicationsAnnotations_Dialogflow.json", "WebApplicationsCorpus.json", "WebApplicationsAnalysis_Dialogflow.json")

Expand Down
28 changes: 17 additions & 11 deletions nlu_analysers/dialogflow_analyser.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import subprocess
from analyser import *

class DialogflowAnalyser(Analyser):
def __init__(self, api_key):
def __init__(self, project_id):
super(DialogflowAnalyser, self).__init__()
self.api_key = api_key
self.url = "https://api.dialogflow.com/v1/query?v=20170712&sessionId=1234&lang=en&query=%s"
self.url = "https://dialogflow.googleapis.com/v2/projects/" + project_id + "/agent/sessions/1:detectIntent"

def get_annotations(self, corpus, output):
data = json.load(open(corpus))
annotations = {'results':[]}

p = subprocess.Popen(['gcloud', 'auth', 'print-access-token'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
access_token, err = p.communicate()
access_token = str(access_token).strip()
for s in data["sentences"]:
if not s["training"]: #only use test data
encoded_text = urllib.quote(s['text'])
headers = { 'Authorization' : 'Bearer %s' % self.api_key}
annotations['results'].append(requests.get(self.url % encoded_text,data={},headers=headers).json())
headers = {'Authorization':'Bearer %s' % access_token, 'Content-Type': 'application/json'}
data = {'queryInput': {'text': {'text': encoded_text, 'languageCode': 'en'}}}
r = requests.post(self.url, data=json.dumps(data), headers=headers)
annotations['results'].append(r.text)

file = open(output, "w")
file.write(json.dumps(annotations, sort_keys=False, indent=4, separators=(',', ': '), ensure_ascii=False).encode('utf-8'))
Expand All @@ -33,12 +38,13 @@ def analyse_annotations(self, annotations_file, corpus_file, output_file):

i = 0
for a in annotations["results"]:
if not a["result"]["resolvedQuery"] == gold_standard[i]["text"]:
a = json.loads(a)
if not urllib.unquote(a["queryResult"]["queryText"]).decode('utf8') == gold_standard[i]["text"]:
print "WARNING! Texts not equal"

#intent
try:
aIntent = a["result"]["metadata"]["intentName"]
aIntent = a["queryResult"]["intent"]["displayName"]
except:
aIntent = "None"
oIntent = gold_standard[i]["intent"]
Expand All @@ -57,9 +63,9 @@ def analyse_annotations(self, annotations_file, corpus_file, output_file):

#entities
try:
aEntities = a["result"]["parameters"]
aEntities = a["queryResult"]["parameters"]
except:
aEntities=[]
aEntities = {}
oEntities = gold_standard[i]["entities"]

for x in aEntities.keys():
Expand All @@ -71,7 +77,7 @@ def analyse_annotations(self, annotations_file, corpus_file, output_file):
truePos = False

for y in oEntities:
if aEntities[x][0].lower() == y["text"].lower():
if len(aEntities[x]) != 0 and aEntities[x][0].lower() == y["text"].lower():
if x == y["entity"]: #truePos
truePos = True
oEntities.remove(y)
Expand All @@ -92,4 +98,4 @@ def analyse_annotations(self, annotations_file, corpus_file, output_file):

i += 1

self.write_json(output_file, json.dumps(analysis, sort_keys=False, indent=4, separators=(',', ': '), ensure_ascii=False).encode('utf-8'))
self.write_json(output_file, json.dumps(analysis, sort_keys=False, indent=4, separators=(',', ': '), ensure_ascii=False).encode('utf-8'))

0 comments on commit 73eddce

Please sign in to comment.