An open API service indexing awesome lists of open source software.

https://github.com/simplisticated/swiftypredictor

Swift API for Yandex.Predictor service.
https://github.com/simplisticated/swiftypredictor

cloud-computing prediction suggestion-engine swift text

Last synced: about 1 month ago
JSON representation

Swift API for Yandex.Predictor service.

Awesome Lists containing this project

README

        


SwiftyPredictor






# At a Glance

`SwiftyPredictor` simplifies work with [Yandex.Predictor](https://tech.yandex.ru/predictor) service in iOS.

## How To Get Started

- Copy content of `Source` folder to your project.

or

- Use `SwiftyPredictor` cocoapod.

## Requirements

* iOS 9.0 and later
* Xcode 8 and later

## Usage

To initialize predictor, simply write something like this:

```swift
let predictor = Predictor(APIKey: "some_api_key")
```

As you noticed, constructor requires API key. If you still don't have it, obtain new API key [here](https://tech.yandex.ru/keys/get/?service=pdct) (you'll need to authorize with Yandex account).

Now you can make requests for text suggestions:

```swift
predictor.requestSuggestions(forQuery: "how to ", inLanguage: .english, withLimit: 10) { (suggestions, error) in
for suggestion in suggestions {
print(suggestion.text)
}

if error != nil {
print("Error: \(error!)")
}
}
```

The example above will print suggestions for phrase `how to `:
- `get`
- `make`
- `use`
- `buy`
- `do`

You can change language by its identifier or predefined name:
- `.english`
- `.russian`
- `.custom(identifier: "es")` - Spanish language
- etc.

If you want to receive full list of supported languages, use `availableLanguages` method:

```swift
predictor.availableLanguages { (languages, error) in
for language in languages {
print(language.identifier)
}
}
```

All asynchronous requests made by `Predictor` instance are cancellable so you can stop them when it's needed:

```swift
/*
* Obtain reference to request instance.
*/

let request = predictor.requestSuggestions(forQuery: "how to ", inLanguage: .english, withLimit: 10) { (suggestions, error) in
// Do something with suggestions here...
}

/*
* Cancel request when needed.
*/

request.cancel()
```

## License

`SwiftyPredictor` is available under the MIT license. See the [LICENSE](./LICENSE) file for more info.