-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqluis.cpp
More file actions
126 lines (99 loc) · 5.35 KB
/
qluis.cpp
File metadata and controls
126 lines (99 loc) · 5.35 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
#include "qluis.h"
#include "qluisresult.h"
#include "qluisaction.h"
#include "WebClient/webclient.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>
const QString QLuis::Endpoint ="https://api.projectoxford.ai/luis/v1/application?id=%1&subscription-key=%2&q=%3";
QLuis::QLuis(QString ModelID, QString SubscriptionKey)
{
this->subscription_key = SubscriptionKey;
this->model_id = ModelID;
}
QLuisResult* QLuis::Call(QString query)
{
QLuisResult* result = new QLuisResult();
WebClient* wc = new WebClient();
wc->SetUserAgent("QLuis 1.0");
bool http_result = wc->get( this->Endpoint.arg(this->model_id).arg(this->subscription_key).arg(query) );
if( http_result )
{
QString content = wc->content();
QJsonObject json;
// qDebug() << content;
QJsonDocument settdoc= QJsonDocument::fromJson(content.toUtf8());
if( settdoc.isNull() || settdoc.isEmpty() ) {
qDebug() << "Malformed config Json!"<<"\nLet you check Json validity\n\n";
return NULL;
}
json = settdoc.object();
if( !json.value("query").isNull() && !json.value("query").isUndefined() && json.value("query").isString() ) {
result->Query = json.value("query").toString();
}
if( !json.value("intents").isNull() && !json.value("intents").isUndefined() && json.value("intents").isArray() ) {
QJsonArray intents = json.value("intents").toArray();
for( int i=0 ; i < intents.size(); i++ )
{
QJsonObject value = intents[i].toObject();
QLuisIntent intent;
intent.Intent = value.take("intent").toString();
intent.Score = value.take("score").toDouble();
QJsonValue value_actions = value.take("actions");
if( !value_actions.isNull() && !value_actions.isUndefined() && value_actions.isArray() ) {
QJsonArray actions = value_actions.toArray();
for( int j=0 ; j < actions.size(); j++ )
{
QJsonObject value_action = actions[j].toObject();
QLuisAction action;
action.Triggered = value_action.take("triggered").toBool();
action.Name = value_action.take("name").toString();
if( !value_action.value("parameters").isNull() && !value_action.value("parameters").isUndefined() && value_action.value("parameters").isArray() )
{
QJsonArray array_actions_parameters = value_action.value("parameters").toArray();
for( int k=0 ; k < array_actions_parameters.size(); k++ )
{
QJsonObject value_actions_parameters = array_actions_parameters[k].toObject();
QLuisActionParameter parameter;
parameter.Name = value_actions_parameters.take("name").toString();
parameter.Required = value_actions_parameters.take("required").toBool();
if( !value_actions_parameters.value("value").isNull() && !value_actions_parameters.value("value").isUndefined() && value_actions_parameters.value("value").isArray() ) {
QJsonArray array_actions_parameters_values = value_actions_parameters.value("value").toArray();
for( int h=0 ; h < array_actions_parameters_values.size(); h++ )
{
QJsonObject value_actions_parameters_value = array_actions_parameters_values[h].toObject();
QLuisActionParameterValue value;
value.Entity = value_actions_parameters_value.take("entity").toString();
value.Type = value_actions_parameters_value.take("type").toString();
value.Score = value_actions_parameters_value.take("score").toDouble();
parameter.Value.append(value);
}
}
action.Parameters.append(parameter);
}
}
intent.Actions.push_back(action);
}
}
result->Intents.push_back(intent);
}
}
if( !json.value("entities").isNull() && !json.value("entities").isUndefined() && json.value("entities").isArray() ) {
QJsonArray array = json.value("entities").toArray();
for( int i=0 ; i < array.size(); i++ )
{
QJsonObject value = array[i].toObject();
QLuisEntity entity;
entity.Entity = value.take("entity").toString();
entity.Type = value.take("type").toString();
entity.StartIndex = value.take("startIndex").toInt();
entity.EndIndex = value.take("endIndex").toInt();
entity.Score = value.take("score").toDouble();
result->Entities.push_back(entity);
}
}
}
delete wc;
return result;
}