Skip to content

Commit

Permalink
Implement Predict, Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
kkdai committed Dec 9, 2016
1 parent 2931ac1 commit d3ae7c7
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 198 deletions.
11 changes: 9 additions & 2 deletions client.go
Expand Up @@ -25,11 +25,18 @@ func NewClient(key string) *Client {
return c
}

// Connect with API url and data, return response byte or error if http.Status is not OK
//Connect :with API url and data, return response byte or error if http.Status is not OK
func (c *Client) Connect(mode string, url string, data *bytes.Buffer, useJson bool) ([]byte, *ErrorResponse) {
client := &http.Client{}
fmt.Println(mode, url, data)
r, _ := http.NewRequest(mode, url, nil)

r := &http.Request{}
if data == nil {
r, _ = http.NewRequest(mode, url, nil)

} else {
r, _ = http.NewRequest(mode, url, data)
}

if useJson {
r.Header.Add("Content-Type", JsonContent)
Expand Down
24 changes: 21 additions & 3 deletions luis.go
Expand Up @@ -19,8 +19,26 @@ func NewLuis(key string, appid string) *Luis {
}

//IntelList :Get All Intent List of this app
func (e *Luis) IntelList() ([]byte, *ErrorResponse) {
url := getIntentListURL(e.appid)
//Retreives information about the intent models.
func (l *Luis) IntelList() ([]byte, *ErrorResponse) {
url := getIntentListURL(l.appid)
fmt.Println(url)
return e.client.Connect("GET", url, nil, true)
return l.client.Connect("GET", url, nil, true)
}

//ActionChannels :get Available Action Channels
//gets a list of all available action channels for the application
func (l *Luis) ActionChannels() ([]byte, *ErrorResponse) {
url := getActionChannels(l.appid)
fmt.Println(url)
return l.client.Connect("GET", url, nil, true)
}

//Predict :get Train Model Predictions
//gets the trained model predictions for the input examples
func (l *Luis) Predict(utterance string) ([]byte, *ErrorResponse) {
url := getPredictURL(l.appid)
data := getStringDataByteBuffer(utterance)
fmt.Println(url)
return l.client.Connect("POST", url, data, true)
}
41 changes: 37 additions & 4 deletions luis_test.go
Expand Up @@ -21,18 +21,51 @@ func init() {
}
}

func getLuis(t *testing.T) *Luis {
e := NewLuis(API_KEY, APPID)
if e == nil {
t.Error("Cannot connect to server")
}
return e
}

func TestIntentList(t *testing.T) {
if API_KEY == "" {
return
}

e := NewLuis(API_KEY, APPID)
if e == nil {
t.Error("Cannot connect to server")
}
e := getLuis(t)

res, err := e.IntelList()

if err != nil {
t.Error("Error happen on :", err.Err)
}
fmt.Println("Got response:", string(res))
result := NewIntentListResponse(res)
fmt.Println("Luis Intent Ret", result)
}

func TestActionChannels(t *testing.T) {
if API_KEY == "" {
return
}
e := getLuis(t)
res, err := e.ActionChannels()

if err != nil {
t.Error("Error happen on :", err.Err)
}
fmt.Println("Got response:", string(res))
}

func TestPredict(t *testing.T) {
if API_KEY == "" {
return
}
e := getLuis(t)
res, err := e.Predict("test string")

if err != nil {
t.Error("Error happen on :", err.Err)
}
Expand Down
6 changes: 6 additions & 0 deletions parameters.go
Expand Up @@ -36,3 +36,9 @@ func getUserDataByteBuffer(userData string) *bytes.Buffer {
return bytes.NewBuffer(byteData)

}

func getStringDataByteBuffer(userData string) *bytes.Buffer {
byteData := []byte(fmt.Sprintf(`["%s"]`, userData))
return bytes.NewBuffer(byteData)

}
210 changes: 24 additions & 186 deletions response.go
Expand Up @@ -12,199 +12,37 @@ type IntentListResponse []struct {
Type string `json:"type"`
}

//Storage all HTTP response include http.response code and error code
type ErrorResponse struct {
ErrorCode int
Err error
}

//Face Response struct
type FaceResponse []struct {
Faceid string `json:"faceId"`
Facerectangle struct {
Top int `json:"top"`
Left int `json:"left"`
Width int `json:"width"`
Height int `json:"height"`
} `json:"faceRectangle"`
Facelandmarks struct {
Pupilleft struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"pupilLeft"`
Pupilright struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"pupilRight"`
Nosetip struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"noseTip"`
Mouthleft struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"mouthLeft"`
Mouthright struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"mouthRight"`
Eyebrowleftouter struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"eyebrowLeftOuter"`
Eyebrowleftinner struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"eyebrowLeftInner"`
Eyeleftouter struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"eyeLeftOuter"`
Eyelefttop struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"eyeLeftTop"`
Eyeleftbottom struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"eyeLeftBottom"`
Eyeleftinner struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"eyeLeftInner"`
Eyebrowrightinner struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"eyebrowRightInner"`
Eyebrowrightouter struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"eyebrowRightOuter"`
Eyerightinner struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"eyeRightInner"`
Eyerighttop struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"eyeRightTop"`
Eyerightbottom struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"eyeRightBottom"`
Eyerightouter struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"eyeRightOuter"`
Noserootleft struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"noseRootLeft"`
Noserootright struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"noseRootRight"`
Noseleftalartop struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"noseLeftAlarTop"`
Noserightalartop struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"noseRightAlarTop"`
Noseleftalarouttip struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"noseLeftAlarOutTip"`
Noserightalarouttip struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"noseRightAlarOutTip"`
Upperliptop struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"upperLipTop"`
Upperlipbottom struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"upperLipBottom"`
Underliptop struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"underLipTop"`
Underlipbottom struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"underLipBottom"`
} `json:"faceLandmarks"`
Faceattributes struct {
Smile float64 `json:"smile"`
Headpose struct {
Pitch float64 `json:"pitch"`
Roll float64 `json:"roll"`
Yaw float64 `json:"yaw"`
} `json:"headPose"`
Gender string `json:"gender"`
Age float64 `json:"age"`
Facialhair struct {
Moustache float64 `json:"moustache"`
Beard float64 `json:"beard"`
Sideburns float64 `json:"sideburns"`
} `json:"facialHair"`
} `json:"faceAttributes"`
}

func NewFaceResponse(raw []byte) FaceResponse {
ret := FaceResponse{}
err := json.Unmarshal(raw, &ret)
if err != nil {
log.Println("json unmarshal err:", err)
return nil
}
return ret
}

type SimilarResponse []struct {
Faceid string `json:"faceId"`
Confidence float64 `json:"confidence"`
}

func NewSimilarResponse(raw []byte) SimilarResponse {
ret := SimilarResponse{}
//NewIntentListResponse :
func NewIntentListResponse(raw []byte) *IntentListResponse {
ret := &IntentListResponse{}
err := json.Unmarshal(raw, &ret)
if err != nil {
log.Println("json unmarshal err:", err)
return nil
}
return ret
}

type GroupResponse struct {
Groups [][]string `json:"groups"`
Messygroup []string `json:"messyGroup"`
}

func NewGroupResponse(raw []byte) *GroupResponse {
ret := new(GroupResponse)
err := json.Unmarshal(raw, ret)
if err != nil {
log.Println("json unmarshal err:", err)
return nil
return ret
}
return ret
}

type VerifyResponse struct {
IsIdentical bool `json:"isIdentical"`
Confidence float64 `json:"confidence"`
//ErrorResponse :
//Storage all HTTP response include http.response code and error code
type ErrorResponse struct {
ErrorCode int
Err error
}

func NewVerifyResponse(raw []byte) VerifyResponse {
ret := VerifyResponse{}
err := json.Unmarshal(raw, &ret)
if err != nil {
log.Println("json unmarshal err:", err)
return ret
}
return ret
//ActionChannelsResponse :
type ActionChannelsResponse []struct {
Name string `json:"Name"`
Version int `json:"Version"`
SupportURI string `json:"SupportUri"`
Actions []struct {
Name string `json:"Name"`
Parameters []struct {
Name string `json:"Name"`
DataType string `json:"DataType"`
PlaceHolderText string `json:"PlaceHolderText"`
Required bool `json:"Required"`
} `json:"Parameters"`
SupportedAuthenticationTypes []int `json:"SupportedAuthenticationTypes"`
} `json:"Actions"`
}
20 changes: 17 additions & 3 deletions url.go
@@ -1,10 +1,24 @@
package luis

const (
LUIS_URL string = "https://api.projectoxford.ai/luis/v1.0/prog/apps/"
LUIS_API_INTENTS string = "intents"
//LuisURL :Basic URL
LuisURL string = "https://api.projectoxford.ai/luis/v1.0/prog/apps/"
//LuisAPIIntents :API Intent List
LuisAPIIntents string = "intents"
//LuisAPIActionChannels :API Action Channels
LuisAPIActionChannels string = "actionChannels"
//LuisAPIPredict :API Predict
LuisAPIPredict string = "predict"
)

func getIntentListURL(apid string) string {
return LUIS_URL + apid + "/" + LUIS_API_INTENTS
return LuisURL + apid + "/" + LuisAPIIntents
}

func getActionChannels(apid string) string {
return LuisURL + apid + "/" + LuisAPIActionChannels
}

func getPredictURL(apid string) string {
return LuisURL + apid + "/" + LuisAPIPredict
}

0 comments on commit d3ae7c7

Please sign in to comment.