-
Notifications
You must be signed in to change notification settings - Fork 4
/
Utterance.cs
81 lines (70 loc) · 2.46 KB
/
Utterance.cs
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
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Starlight {
public class Utterance {
private string _query;
public String Query {
get {
return _query;
}
set {
_query = value.ToLower();
}
}
public List<Intent> Intents { get; set; }
public Intent TopScoringIntent {
get {
return GetTopScoringIntent();
}
}
public Entity Entity { get; set; }
public Utterance() {
Intents = new List<Intent>();
Entity = new Entity();
}
private Intent GetTopScoringIntent() {
Intent topScoringIntent = new Intent();
float maxValue = 0;
foreach (Intent intent in Intents) {
if (intent.Score > maxValue) {
topScoringIntent = intent;
maxValue = intent.Score;
}
}
return topScoringIntent;
}
public JObject GetResponse() {
JObject json =
new JObject(
new JProperty("query", Query),
new JProperty("intents",
new JArray(
(from intent in Intents
orderby intent.Score descending
select new JObject(
new JProperty("intent", intent.Name),
new JProperty("score", intent.Score)
)).Take(3)
)
)
);
if (Entity != null) {
json.Add(new JProperty("entities",
new JObject(
new JProperty("entity", Entity.EntityText),
new JProperty("type", Entity.Type),
new JProperty("startIndex", Entity.StartIndex),
new JProperty("endIndex", Entity.EndIndex),
new JProperty("date", Entity.DateTime?.ToString("yyyy-MM-dd")),
new JProperty("time", Entity.DateTime?.ToString("hh:mm tt"))
)
)
);
}
return json;
}
}
}