-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
178 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package alice | ||
|
||
import ( | ||
"encoding/json" | ||
"strconv" | ||
) | ||
|
||
type Entity struct { | ||
Tokens struct { | ||
Start int `json:"start"` | ||
End int `json:"end"` | ||
} | ||
Type string `json:"type"` | ||
Value interface{} `json:"value"` | ||
} | ||
|
||
type YandexFio struct { | ||
FirstName string | ||
PatronymicName string | ||
LastName string | ||
} | ||
|
||
func (e *Entity) YandexFio() *YandexFio { | ||
if e.Type != "YANDEX.FIO" { | ||
return nil | ||
} | ||
v, _ := e.Value.(map[string]interface{}) | ||
if v == nil { | ||
return nil | ||
} | ||
|
||
f, _ := v["first_name"].(string) | ||
p, _ := v["patronymic_name"].(string) | ||
l, _ := v["last_name"].(string) | ||
|
||
return &YandexFio{ | ||
FirstName: f, | ||
PatronymicName: p, | ||
LastName: l, | ||
} | ||
} | ||
|
||
type YandexGeo struct { | ||
Country string | ||
City string | ||
Street string | ||
HouseNumber string | ||
Airport string | ||
} | ||
|
||
func (e *Entity) YandexGeo() *YandexGeo { | ||
if e.Type != "YANDEX.GEO" { | ||
return nil | ||
} | ||
v, _ := e.Value.(map[string]interface{}) | ||
if v == nil { | ||
return nil | ||
} | ||
|
||
co, _ := v["country"].(string) | ||
ci, _ := v["city"].(string) | ||
s, _ := v["street"].(string) | ||
h, _ := v["house_number"].(string) | ||
a, _ := v["airport"].(string) | ||
|
||
return &YandexGeo{ | ||
Country: co, | ||
City: ci, | ||
Street: s, | ||
HouseNumber: h, | ||
Airport: a, | ||
} | ||
} | ||
|
||
type YandexDateTime struct { | ||
Year int | ||
YearIsRelative bool | ||
Month int | ||
MonthIsRelative bool | ||
Day int | ||
DayIsRelative bool | ||
Hour int | ||
HourIsRelative bool | ||
Minute int | ||
MinuteIsRelative bool | ||
} | ||
|
||
func (e *Entity) YandexDateTime() *YandexDateTime { | ||
if e.Type != "YANDEX.DATETIME" { | ||
return nil | ||
} | ||
v, _ := e.Value.(map[string]interface{}) | ||
if v == nil { | ||
return nil | ||
} | ||
|
||
keys := []string{"year", "month", "day", "hour", "minute"} | ||
|
||
// extract absolute values | ||
abs := make(map[string]int) | ||
for _, k := range keys { | ||
switch v := v[k].(type) { | ||
case json.Number: | ||
i64, _ := v.Int64() | ||
abs[k] = int(i64) | ||
case float64: | ||
abs[k] = int(v) | ||
} | ||
} | ||
|
||
// extract relative flags | ||
rel := make(map[string]bool) | ||
for _, k := range keys { | ||
rel[k], _ = v[k+"_is_relative"].(bool) | ||
} | ||
|
||
return &YandexDateTime{ | ||
Year: abs["year"], | ||
YearIsRelative: rel["year"], | ||
Month: abs["month"], | ||
MonthIsRelative: rel["month"], | ||
Day: abs["day"], | ||
DayIsRelative: rel["day"], | ||
Hour: abs["hour"], | ||
HourIsRelative: rel["hour"], | ||
Minute: abs["minute"], | ||
MinuteIsRelative: rel["minute"], | ||
} | ||
} | ||
|
||
type YandexNumber struct { | ||
json.Number | ||
} | ||
|
||
func (e *Entity) YandexNumber() *YandexNumber { | ||
if e.Type != "YANDEX.NUMBER" { | ||
return nil | ||
} | ||
|
||
switch v := e.Value.(type) { | ||
case json.Number: | ||
return &YandexNumber{v} | ||
case float64: | ||
return &YandexNumber{json.Number(strconv.FormatFloat(v, 'f', -1, 64))} | ||
default: | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters