Recast.AI official SDK in Swift.
If you are working with Swift 2.0+ use this version : ~> 2.1.1 !
This pod is a Swift interface to the Recast.AI API. It allows you to make requests to your bots.
- iOS 10.0+
- Xcode 8.0+
RecastAI is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "RecastAI"
To run the example project, clone the repo, and run pod install
from the Example directory first.
This class handles everything. Create a RecastAIClient object and init it with your token. The RecastAIClient can also be instanciated with a language (optional).
import RecastAI
class ViewController: UIViewController
{
//Vars
var bot : RecastAIClient?
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.bot = RecastAIClient(token : "YOUR_TOKEN")
self.bot = RecastAIClient(token : "YOUR_TOKEN", language: "YOUR_LANGUAGE")
}
}
This module contains 3 classes, as follows:
- Client is the client allowing you to make requests.
- Response contains the response from Recast.AI.
- Intent represents an Intent of the response
Don't hesitate to dive into the code, it's commented ;)
The Client can be instanciated with a token and a language (both optional).
let bot = RecastAIClient(token : "YOUR_TOKEN", language: "YOU_LANGUAGE")
Your tokens:
Copy paste your request access token from your bot's settings.
Your language
let bot = RecastAIClient(token : "YOUR_TOKEN", language: "en")
The language is a lowercase 639-1 isocode.
The textRequest
method allows you to make a request to the Recast.AI API. The textRequest
method is to make a Text request and takes a text as a String
as parameter and a language as a String
or a Token
as optional parameters. You also need to provide a successHandle
and a failureHandle
functions that will be called when either the request is done or the request fails.
If you pass a token or a language in the options parameter, it will override your default client language or token.
/**
Make text request to Recast.AI API
*/
func makeRequest()
{
//Call makeRequest with string parameter to make a text request
self.bot?.textRequest(<#T##request: String##String#>, successHandler: <#T##(Response) -> Void#>, failureHandle: <#T##(Error) -> Void#>)
}
If a language is provided: the language you've given is used for processing if your bot has expressions for it, else your bot's primary language is used.
If no language is provided: the language of the text is detected and is used for processing if your bot has expressions for it, else your bot's primary language is used for processing.
In order to make a File Request you need to implement two methods you need to pass a file as a parameter. You also need to provide a successHandle
and a failureHandle
functions that will be called when either the request is done or the request fails.
If you pass a token or a language in the options parameter, it will override your default client language or token.
file format: .wav
/**
Make File request to Recast.AI API
*/
func makeFileRequest()
{
if (!(self.requestTextField.text?.isEmpty)!)
{
let url = URL(string: self.requestTextField.text!)!
//Call makeRequest with string parameter to make a text request
self.bot?.fileRequest(<#T##audioFileURL: URL##URL#>, successHandler: <#T##(Response) -> Void#>, failureHandle: <#T##(Error) -> Void#>)
}
}
The Response is generated after a call to either fileRequest or textRequest.
Method | Params | Return |
---|---|---|
intent() | Object: the first detected intent |
/**
Method called when the request was successful
- parameter response: the response returned from the Recast API
- returns: void
*/
func recastRequestDone(_ response : Response)
{
let intent = response.intent()
print(intent.slug)
}
Method | Params | Return |
---|---|---|
get(name) | name: String | Entity: the first Entity matched |
/**
Method called when the request was successful
- parameter response: the response returned from the Recast API
- returns: void
*/
func recastRequestDone(_ response : Response)
{
let location = response.get('location')
}
Method | Params | Return |
---|---|---|
all(name) | name: String | Array[Entity]: all the Entities matched |
/**
Method called when the request was successful
- parameter response: the response returned from the Recast API
- returns: void
*/
func recastRequestDone(_ response : Response)
{
let locations = response.all('location')
}
Method | Params | Return |
---|---|---|
isAssert() | Bool: wheither or not the act is an assertion | |
isCommand() | Bool: wheither or not the act is a command | |
isWhQuery() | Bool: wheither or not the act is a wh-query | |
isYnQuery() | Bool: wheither or not the act is a yn-query |
Method | Params | Return |
---|---|---|
isAbbreviation() | Bool: wheither or not the sentence is asking for an abbreviation | |
isEntity() | Bool: wheither or not the sentence is asking for an entity | |
isDescription() | Bool: wheither or not the sentence is asking for a description | |
isHuman() | Bool: wheither or not the sentence is asking for a human | |
isLocation() | Bool: wheither or not the sentence is asking for a location | |
isNumber() | Bool: wheither or not the sentence is asking for a number |
Method | Params | Return |
---|---|---|
isVPositive() | Bool: wheither or not the sentiment is very positive | |
isPositive() | Bool: wheither or not the sentiment is positive | |
isNeutral() | Bool: wheither or not the sentiment is neutral | |
isNegative() | Bool: wheither or not the sentiment is negative | |
isVNegative() | Bool: wheither or not the sentiment is very negative |
Each of the following methods corresponds to a Response attribute
Attributes | Type |
---|---|
raw | String: the raw unparsed json response |
type | String: the type of the processed sentence |
act | String: the act of the processed sentence |
sentiment | String: the sentiment of the processed sentence |
source | String: the user input |
intents | Array[object]: all the matched intents |
status | String: the status of the response |
version | String: the version of the json |
timestamp | String: the timestamp at the end of the processing |
Each of the following methods corresponds to a Response attribute
Attributes | Description |
---|---|
name | String: the name of the entity |
raw | String: the unparsed json value of the entity |
In addition to those methods, more attributes are generated depending of the nature of the entity. The full list can be found there: man.recast.ai
/**
Method called when the request was successful
- parameter response: the response returned from the Recast API
- returns: void
*/
func recastRequestDone(_ response : Response)
{
let location = response.get('location')
print(location["latitude"])
}
We will call recastRequestError
with Error as parameter.
For more information about Recast Errors check out our man#error
You can view the whole API reference at man.recast.ai
PE Lieb, pierre-edouard.lieb@recast.ai, @pedward_lieb
You can follow us on Twitter at @recastai for updates and releases.
RecastAI is available under the MIT license.
Copyright (c) [2016] Recast.AI
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.