GithubHelp home page GithubHelp logo

viveksinha / wit4j Goto Github PK

View Code? Open in Web Editor NEW

This project forked from leontibrechko/wit4j

0.0 2.0 0.0 290 KB

Java library to communicate with Wit.ai bot using Wit.ai HTTP API.

License: Apache License 2.0

Java 100.00%

wit4j's Introduction

Wit4j

Java library to communicate with Wit.ai bot using Wit.ai HTTP API.

Code example

public class WitTest {
    private static Map<String, IAction> actions = new HashMap<>();

    static {
        actions.put("merge", (sessionId, context, httpResponse) -> {
            JsonNode entitiesObj = httpResponse.get("entities");
            if (entitiesObj == null) {
                throw new IllegalArgumentException("No 'entities' object was found in json responce file.");
            }

            ObjectNode entities = new ObjectNode(JsonNodeFactory.instance);

            Iterator<String> fieldNames = entitiesObj.fieldNames();
            String nextField;
            while (fieldNames.hasNext()) {
                nextField = fieldNames.next();
                entities.set(nextField, findFirstEntityValue(entitiesObj, nextField));
            }

            context.updateContext(entities);
        });

        actions.put("say", (sessionId, context, httpResponse) ->
                System.out.println("Bot: " + httpResponse.get("msg").asText()));

        actions.put("fetchForecast", (sessionId, context, httpResponse) ->
                context.getContext().put("forecastResult", "sunny"));
    }

    public static JsonNode findFirstEntityValue(JsonNode entities, String entityName) {
        boolean flag = !entities.isNull() && !entities.get(entityName).isNull()
                && entities.get(entityName).isArray() && entities.get(entityName).size() > 0
                && !entities.get(entityName).get(0).isNull();

        if (!flag) {
            throw new IllegalArgumentException("Invalid entity name or entities object");
        }

        JsonNode entityValues = entities.get(entityName).get(0);
        if (entityValues.get("value").isObject()) {
            return entityValues.get("value").get("value");
        } else {
            return entityValues.get("value");
        }
    }

    public static void main(String[] args) throws IOException, IllegalAccessException {
        Wit wit = new Wit("someTokenString", actions);
        wit.interactByConsole(HttpCommunicationType.CONVERSE);
    }
}

Motivation

The reason for creating this library is to provide Java developers with base abstraction and some implementation of Wit.ai HTTP API in order to comfortably create bot applications in Java programming language.

Installation

    <dependencies>
        <dependency>
            <groupId>com.liavontsibrechka</groupId>
            <artifactId>wit4j</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>wit4j-mvn-repo</id>
            <url>https://raw.github.com/leontibrechko/wit4j/mvn-repo/</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </snapshots>
        </repository>
    </repositories>

Link for .jar file: http://www.liavontsibrechka.com/wit4j.jar

API Reference

Java DOCs link: http://www.liavontsibrechka.com/apidocs/index.html

License

Copyright 2016 Liavontsi Brechka

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

wit4j's People

Contributors

leontibrechko avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.