-
Notifications
You must be signed in to change notification settings - Fork 1
/
Models.scala
212 lines (187 loc) · 7.05 KB
/
Models.scala
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package com.scalawit
import org.joda.time._
import play.api.libs.json._
import play.api.libs.functional.syntax._
import org.joda.time.format.ISODateTimeFormat
case class WitContext(
states: Option[Seq[String]] = None,
referenceTime: Option[DateTime] = Some(new DateTime()),
timezone: Option[String] = None,
entities: Option[Seq[WitEntityDescription]] = None) extends Printable[WitContext] {
override def getMappable = implicitly[Mappable[WitContext]]
override def getLabel: String = "Context"
}
object WitContext {
implicit val writesWitContext: Writes[WitContext] = (
(__ \ 'states).writeNullable[Seq[String]] and
(__ \ 'reference_time).writeNullable[DateTime] and
(__ \ 'timezone).writeNullable[String] and
(__ \ 'entities).writeNullable[Seq[WitEntityDescription]]
)(unlift(WitContext.unapply _))
}
case class WitMessage(
msgId: String,
msgBody: String,
outcome: Option[WitOutcome]) extends Printable[WitMessage] {
override def getMappable = implicitly[Mappable[WitMessage]]
override def getLabel: String = "Message"
}
object WitMessage {
implicit val readsWitMessage: Reads[WitMessage] = (
(__ \ 'msg_id).read[String] and
(__ \ 'msg_body).read[String] and
(__ \ 'outcome).readNullable[WitOutcome]
)(WitMessage.apply _)
}
case class WitOutcome(
intent: String,
entities: Seq[WitEntity],
confidence: Double) extends Printable[WitOutcome] {
override def getMappable = implicitly[Mappable[WitOutcome]]
override def getLabel: String = "Outcome"
}
object WitOutcome {
implicit val readsWitOutcome: Reads[WitOutcome] = (
(__ \ 'intent).read[String] and
(__ \ 'entities).read[Map[String,PartialWitEntities]].map(processPartialEntities _) and
(__ \ 'confidence).read[Double]
)(WitOutcome.apply _)
private def processPartialEntities(data: Map[String,PartialWitEntities]): Seq[WitEntity] = {
data.flatMap { case (id, partial) =>
partial.entities map { _.toWitEntity(id) }
}.toSeq
}
}
case class WitIntent(
id: String,
name: String,
doc: String,
metadata: Option[String],
entities: Option[Seq[WitEntity]],
expression: Option[Seq[WitExpression]]) extends Printable[WitIntent] {
override def getMappable = implicitly[Mappable[WitIntent]]
override def getLabel: String = "Intent"
}
object WitIntent {
implicit val readsWitIntent: Reads[WitIntent] = (
(__ \ 'id).read[String] and
(__ \ 'name).read[String] and
(__ \ 'doc).read[String] and
(__ \ 'metadata).readNullable[String] and
(__ \ 'entities).readNullable[Seq[WitEntity]] and
(__ \ 'expressions).readNullable[Seq[WitExpression]]
)(WitIntent.apply _)
}
case class WitEntity(
id: String,
role: Option[String],
value: Option[String],
from: Option[DateTime],
to: Option[DateTime],
start: Option[Int],
end: Option[Int],
body: Option[String],
metadata: Option[String],
suggested: Option[Boolean]) extends Printable[WitEntity] {
override def getMappable = implicitly[Mappable[WitEntity]]
override def getLabel: String = "Entity"
}
object WitEntity {
implicit val readsWitEntity: Reads[WitEntity] = (
((__ \ 'wisp).read[String] orElse (__ \ 'id).read[String]) and
__.read[PartialWitEntity])((id, partialEntity) => partialEntity.toWitEntity(id))
}
case class WitEntityDescription(
id: String,
doc: String,
values: Seq[WitEntityValue],
builtin: Boolean = false) extends Printable[WitEntityDescription] {
override def getMappable = implicitly[Mappable[WitEntityDescription]]
override def getLabel: String = "Description"
}
object WitEntityDescription {
implicit val readsWitEntityDescription: Reads[WitEntityDescription] = (
(__ \ 'id).read[String] and
(__ \ 'doc).read[String] and
(__ \ 'values).read[Seq[WitEntityValue]] and
((__ \ 'builtin).read[Boolean] orElse DummyRead[Boolean](false))
)(WitEntityDescription.apply _)
implicit val writesWitEntityDescription: Writes[WitEntityDescription] = (
(__ \ 'id).write[String] and
(__ \ 'doc).write[String] and
(__ \ 'values).write[Seq[WitEntityValue]]
)(desc => (desc.id, desc.doc, desc.values))
}
case class WitEntityValue(
value: String,
expressions: Seq[String],
metadata: Option[String]) extends Printable[WitEntityValue] {
override def getMappable = implicitly[Mappable[WitEntityValue]]
override def getLabel: String = "Entity"
}
object WitEntityValue {
implicit val formatWitEntityValue: Format[WitEntityValue] = (
(__ \ 'value).format[String] and
(__ \ 'expressions).format[Seq[String]] and
(__ \ 'metadata).formatNullable[String]
)(WitEntityValue.apply _, unlift(WitEntityValue.unapply _))
}
case class WitExpression(
id: String,
body: String,
entities: Seq[WitEntity]) extends Printable[WitExpression] {
override def getMappable = implicitly[Mappable[WitExpression]]
override def getLabel: String = "Expression"
}
object WitExpression {
implicit val readsWitExpression: Reads[WitExpression] = (
(__ \ 'id).read[String] and
(__ \ 'body).read[String] and
(__ \ 'entities).read[Seq[WitEntity]]
)(WitExpression.apply _)
}
case class WitExpressionDescription(expression: String) extends Printable[WitExpressionDescription] {
override def getMappable = implicitly[Mappable[WitExpressionDescription]]
override def getLabel: String = "Expression"
}
object WitExpressionDescription {
implicit val writesWitExpressionDescription = Json.writes[WitExpressionDescription] // only works because the field name is "expression"
}
/* The data structures below are only used during parsing */
private case class PartialWitEntities(entities: Seq[PartialWitEntity])
private object PartialWitEntities {
implicit val readsPartialWitEntities: Reads[PartialWitEntities] =
(__.read[Seq[PartialWitEntity]] or __.read[PartialWitEntity].map(Seq(_))).map(PartialWitEntities.apply _)
}
private case class PartialWitEntity(
value: Option[String],
role: Option[String],
from: Option[DateTime],
to: Option[DateTime],
start: Option[Int],
end: Option[Int],
body: Option[String],
metadata: Option[String],
suggested: Option[Boolean]) {
def toWitEntity(id: String) = WitEntity(id, role, value, from, to, start, end, body, metadata, suggested)
}
private object PartialWitEntity {
implicit def readsDateTime: Reads[DateTime] = __.read[String].map(ISODateTimeFormat.dateTime().parseDateTime(_))
implicit val readsPartialWitEntity: Reads[PartialWitEntity] = (
((__ \ 'value).readNullable[String] orElse DummyReadOpt[String]()) and
(__ \ 'role).readNullable[String] and
((__ \ 'value \ 'from).readNullable[DateTime] orElse DummyReadOpt[DateTime]()) and
((__ \ 'value \ 'to).readNullable[DateTime] orElse DummyReadOpt[DateTime]()) and
(__ \ 'start).readNullable[Int] and
(__ \ 'end).readNullable[Int] and
(__ \ 'body).readNullable[String] and
(__ \ 'metadata).readNullable[String] and
(__ \ 'suggested).readNullable[Boolean]
)(PartialWitEntity.apply _)
}
private case class DummyRead[T](default: T) extends Reads[T] {
def reads(json: JsValue) = JsSuccess(default)
}
private case class DummyReadOpt[T]() extends Reads[Option[T]] {
def reads(json: JsValue) = JsSuccess(None)
}