Recast.AI official SDK for Scala
Scala
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
misc
project
src
.gitignore
README.md
build.sbt

README.md

Recast.AI - SDK-Scala

alt text

Recast.AI official SDK in Scala.

Synospis

This module is a Scala interface to the Recast.AI API. It allows you to make request to your bots

Installation

Via sbt

Add those lines into your build.sbt file

resolvers += Resolver.bintrayRepo("recast-ai", "generic")

libraryDependencies ++= Seq(
    "ai.recast" %% "sdk_scala" % "2.0.0"
  )

Usage

Module

import recast.client._

object HelloBot {
  def main(args: Array[String]): Unit = {
    val client = new Client(YOUR_TOKEN)

    try {
      val response = client.textRequest(YOUR_TEXT)
      // Do your code
      response.intent() match {
        case Some("hello-greetings") => println("Hello, world")
        case _ => println("I didn't understand, sorry !")
      }
    } catch {
      // handle error
      case _ => println("Something went wrong...")
    }
  }
}

Specs

Classes

This package contains 4 classes, as follow:

  • Client is the client allowing you to make requests to the Recast.AI API.
  • Response contains the response from Recast.AI
  • Intent represents an intent found by Recast.AI in your user's input.
  • RecastError is the error thrown by the module

Client

The Client can be instanciated with a token and a language (both optional)

val client = new Client(YOUR_TOKEN, YOUR_LANGUAGE)

Your tokens:

alt text

Copy paste your request access token from your bot's settings.

Your language

var client = new Client(YOUR_TOKEN, YOUR_LANGUAGE)

The language is a lowercase 639-1 isocode.

Text Request

textRequest(text, options = { token: YOUR_TOKEN, language: YOUR_LANGUAGE })

If you pass a token or a language in the options parameter, it will override your default client language or token.

import recast.client.Client

object TestBot {
  def main(args: Array[String]): Unit = {
    val client = new Client(YOUR_TOKEN)

    try {
      var response = client.textRequest(YOUR_TEXT)
    } catch {
      // handle error
    }
    // do your code

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.

File Request

fileRequest(file, options = { token: YOUR_TOKEN, language: YOUR_LANGUAGE })

If you pass a token or a language in the options parameter, it will override your default client language or token.

file format: .wav

  try {
    client.fileRequest(YOUR_FILE_NAME)
  } catch {
    // Handle error
  }
  // Do your code

If a language is provided: Your bot's primary language is used for processing as we do not provide language detection for speech.

If no 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

Response

The Response is generated after a call to either fileRequest or textRequest.

Get the first detected intent

Method Params Return
intent() Option: the first detected intent
val response = client.textRequest("Give me a recipe with asparagus. And tomatoes.")

match response.intent() {
  case None => println("Mmmh sorry, I did't get it")
  case Some(x) println("Roger that")
}

Get one entity

Method Params Return
get(name) name: String Option[Map[String, JsValue]]: the first Entity matched
val response = client.textRequest("What's the weather ?")

match response.get("location") {
  case None => println("I need to know where you live.")
  case Some(x) println("Roger that")
}

Get all entities matching name

Method Params Return
all(name) name: String Option[List[Map[String, JsValue]]]: all the Entities matched
val response = client.textRequest("What's the weather ?")

match response.all("location") {
  case None => println("I need to know where you live.")
  case Some(x) println("Roger that")
}

Act helper

Method Params Return
isAssert() Boolean: wheither or not the act is an assertion
isCommand() Boolean: wheither or not the act is a command
isWhQuery() Boolean: wheither or not the act is a wh-query
isYnQuery() Boolean: wheither or not the act is a yn-query

Type helper

Method Params Return
isAbbreviation() Boolean: wheither or not the sentence is asking for an abbreviation
isEntity() Boolean: wheither or not the sentence is asking for an entity
isDescription() Boolean: wheither or not the sentence is asking for a description
isHuman() Boolean: wheither or not the sentence is asking for a human
isLocation() Boolean: wheither or not the sentence is asking for a location
isNumber() Boolean: wheither or not the sentence is asking for a number

Sentiment helper

Method Params Return
isVPositive() Boolean: wheither or not the sentiment is very positive
isPositive() Boolean: wheither or not the sentiment is positive
isNeutral() Boolean: wheither or not the sentiment is neutral
isNegative() Boolean: wheither or not the sentiment is negative
isVNegative() Boolean: wheither or not the sentiment is very negative

Attributes

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 List[Intent]: all the detected 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

Intent

The Intent class represents an intent detected when a call to the Recast.AI is performed either by textRequest or by fileRequest.

Attributes Type
slug String: the slug of the intent detected
confidence Float: the confidence with which the intent is detected

RecastError

The Recast.AI Error is thrown when receiving a non-200 response from Recast.AI.

As it inherits from Error, it implements the default Error methods.

More

You can view the whole API reference at man.recast.ai.

Author

Jérôme Houdan, jerome.houdan@recast.ai

You can follow us on Twitter at @recastai for updates and releases.

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.