olami官方java sdk源码用jdk1.7编过经验(修改源码支持jdk1.7)

版权声明:博主原创文章,欢迎大家转载! https://blog.csdn.net/happycxz/article/details/78179611

注:以下代码是基于olami官方github(olami-java-client-sdk):https://github.com/olami-developers/olami-java-client-sdk在2017年8月30日版本基础上的修改,主要改动了三个文件。

olami-java-client-sdk\pom.xml

改动:注掉了line 172行,原因:jdk1.7版本好像编译java doc时会报错。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>ai.olami</groupId>
    <artifactId>olami-java-client-sdk</artifactId>
    <version>1.0.1</version>
    <packaging>pom</packaging>

    <name>OLAMI Java Client SDK</name>
    <url>http://olami.ai</url>
    <description>OLAMI APIs Java client library and samples</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jdk.version>1.7</jdk.version>
    </properties>

    <licenses>
        <license>
            <name>The Apache Software License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>
    <developers>
        <developer>
            <name>olami</name>
            <email>[email protected]</email>
        </developer>
    </developers>
    <scm>
        <connection>scm:[email protected]:olami-developers/olami-java-client-sdk.git</connection>
        <developerConnection>scm:[email protected]:olami-developers/olami-java-client-sdk.git</developerConnection>
        <url>https://github.com/olami-developers/olami-java-client-sdk</url>
    </scm>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>2.8.0</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <modules>
        <module>lib</module>
    </modules>

    <profiles>
        <profile>
            <id>all</id>
            <modules>
                <module>examples/text-input-example</module>
                <module>examples/speech-input-example</module>
                <module>examples/dump-nli-results-example</module>
                <module>examples/async-text-chatbot-example</module>
            </modules>
        </profile>

        <profile>
            <id>release-sign-bintray</id>
            <distributionManagement>
                <repository>
                    <id>bintray-olami-developers-api-client</id>
                    <name>olami-developers-api-client</name>
                    <url>https://api.bintray.com/maven/olami-developers/api-client/olami-java-client-sdk/;publish=1</url>
                </repository>
            </distributionManagement>
            <build>
                <plugins>
                    <!-- 
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>1.5</version>
                        <executions>
                            <execution>
                                <id>sign-artifacts</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                     -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <version>3.0.1</version>
                        <executions>
                            <execution>
                                <id>attach-sources</id>
                                <goals>
                                    <goal>jar-no-fork</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.6.1</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>3.0.0</version>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <appendAssemblyId>false</appendAssemblyId>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>   
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>clean</phase>
                        <configuration>
                            <tasks>
                                <delete dir="bin"/>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.10.4</version>
                <configuration>
                    <excludePackageNames>org.*</excludePackageNames>
                    <!--additionalparam>-Xdoclint:none</additionalparam-->
                    <encoding>${chartset.UTF8}</encoding>
                    <aggregate>true</aggregate>
                    <charset>${chartset.UTF8}</charset>
                    <docencoding>${chartset.UTF8}</docencoding>
                </configuration>
                <executions>  
                    <execution>
                        <id>attach-javadocs</id>  
                        <goals>  
                            <goal>jar</goal>  
                        </goals>  
                    </execution>  
                </executions>  
            </plugin>
         </plugins>
    </build>

</project>

olami-java-client-sdk\examples\async-text-chatbot-example\pom.xml

改动:maven的pom.xml中jdk.version由1.8改为1.7,在jdk1.7环境编译。

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>

    <groupId>ai.olami.example</groupId>
    <artifactId>async-text-chatbot-example</artifactId>
    <version>20170708</version>
    <packaging>jar</packaging>

    <name>OLAMI Java Client Examples: Async Text Chatbot</name>
    <url>http://olami.ai</url>

    <parent>
        <groupId>ai.olami</groupId>
        <artifactId>olami-java-client-sdk</artifactId>
        <version>1.0.1</version>
        <relativePath>../../pom.xml</relativePath>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jdk.version>1.7</jdk.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>ai.olami</groupId>
            <artifactId>olami-java-client</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
        <dependency>
            <groupId>ai.olami.example</groupId>
            <artifactId>dump-nli-results-example</artifactId>
            <version>20170708</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
            </plugin>
            <!-- Copy files to the 'bin' folder of the SDK root path -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <configuration>
                            <tasks>
                                <copy todir="../../bin/examples/${project.artifactId}">
                                    <fileset dir="${project.build.directory}">
                                        <!-- Copy packaged jar files -->
                                        <include name="*${project.artifactId}.jar"/>
                                    </fileset>
                                </copy>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- End of copy -->
        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.6.1</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <finalName>${project.artifactId}</finalName>
                        <archive>
                            <manifest>
                                <mainClass>ai.olami.example.AsyncTextChatbotExample</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

olami-java-client-sdk\examples\async-text-chatbot-example\src\main\java\ai\olami\example\AsyncTextChatbotExample.java

改动:sample里有一个AsyncTextChatbotExample是依赖于jdk1.8的CompletableFuture的,这里保证sample原有功能,将代码修改成同步的了。

/*
    Copyright 2017, VIA Technologies, Inc. & OLAMI Team.

    http://olami.ai

    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.
*/

package ai.olami.example;

import java.util.Scanner;

import ai.olami.cloudService.APIConfiguration;
import ai.olami.cloudService.APIResponse;
import ai.olami.cloudService.TextRecognizer;
import ai.olami.nli.NLIResult;

public class AsyncTextChatbotExample {

    // * Replace your APP KEY with this variable.
    private static String appKey = "*****your-app-key*****";

    // * Replace your APP SECRET with this variable.
    private static String appSecret = "*****your-app-secret*****";

    // * Replace the localize option you want with this variable.
    // * - Use LOCALIZE_OPTION_SIMPLIFIED_CHINESE for China
    // * - Use LOCALIZE_OPTION_TRADITIONAL_CHINESE for Taiwan 
    private static int localizeOption = APIConfiguration.LOCALIZE_OPTION_SIMPLIFIED_CHINESE;
//  private static int localizeOption = APIConfiguration.LOCALIZE_OPTION_TRADITIONAL_CHINESE;

    public static void main(String[] args) throws Exception {
        if (args.length == 3) {
            initByInputArgs(args);
        } else if (args.length > 0) {
            printUsageAndExit();
        }

        // * Step 1: Configure your key and localize option.
        APIConfiguration config = new APIConfiguration(appKey, appSecret, localizeOption);

        // * Step 2: Create the text recognizer.
        TextRecognizer recoginzer = new TextRecognizer(config);

        System.out.format("\nTell me your name: ");
        Scanner reader = new Scanner(System.in);

        boolean fristTalk = true;       
        while (reader.hasNext()) {
            if (fristTalk) {
                String userName = reader.nextLine();
                if (userName.isEmpty()) {
                    userName = " YOU ";
                }
                // Setup end user information.
                recoginzer.setEndUserIdentifier(userName);

                fristTalk = false;
                System.out.format("\nHi! %s\n", userName);
                System.out.format("\nType to say something or 'bye' to exit:\n");
            }

            String whatUserSays = reader.nextLine();
            if (whatUserSays.toLowerCase().equals("bye")) {
                System.out.format("\n[ OLAMI Robot ] Says: Bye!\n\n");
                reader.close();
                break;
            } else {
                System.out.format("\n[ %s ] Says: %s\n",
                        recoginzer.getEndUserIdentifier(),
                        whatUserSays);
            }

            // * Send text
            APIResponse wsResponse = recoginzer.requestNLI(whatUserSays); 
            //System.out.println("\nOriginal Response : " + wsResponse.toString());
            //System.out.println("\n---------- dump ----------\n");
            //System.out.println(jsonDump.toJson(wsResponse));
            //System.out.println("\n--------------------------\n");
            // Check request status.
            if (wsResponse.ok() && wsResponse.hasData()) {
                NLIResult[] nliResults = wsResponse.getData().getNLIResults();
                // Get the reply content.
                if (nliResults[0].hasDescObject()) {
                    String reply = nliResults[0].getDescObject().getReplyAnswer();
                    if (reply.isEmpty()) {
                        System.out.format("\n[ OLAMI Robot ] Says: ...\n");
                    } else {
                        // Show the reply.
                        System.out.format(
                                "\n[ OLAMI Robot ] Says: %s", reply);
                        // Show IDS data.
                        if (nliResults[0].isFromIDS() 
                                && nliResults[0].hasDataObjects()) {
                            System.out.println("\n");
                            DumpIDSDataExample.dumpIDSData(nliResults[0]);
                            System.out.println("\n");
                        }
                    }
                    System.out.format(" (Say 'bye' to exit)\n");
                }
            }

        }
    }

    private static void printUsageAndExit() {
        System.out.println("\n Usage:");
        System.out.println("\t args[0]: your_app_key");
        System.out.println("\t args[1]: your_app_secret");
        System.out.println("\t args[2]: localize_option=[0|1]");
        System.out.println("\n");
        System.exit(0);
    }

    private static void initByInputArgs(String[] args) {
        appKey = args[0];
        appSecret = args[1];
        localizeOption = Integer.parseInt(args[2]);
    }

}

修改后的代码打包下载点击此处:https://gitee.com/happycxz/olami-java-client-sdk_jdk1.7

猜你喜欢

转载自blog.csdn.net/happycxz/article/details/78179611
今日推荐