用圖靈機器人實現的兩個機器人對話聊天

uoou發表於2015-11-18

這是使用圖靈機器人提供的API來實現的兩個機器人對話,挺有意思的,感興趣的朋友也可以去官網註冊一個賬號http://www.tuling123.com/

下面是具體的實現程式碼:

public class Test extends Random {
    /**
     * 
     */
    private static final long serialVersionUID = 8120549762361392055L;

    @SuppressWarnings("unused")
    private Long seed;
    private Boolean haveNextNextGaussian;

    synchronized public void setSeed(long seed) {
            this.seed = (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1);
            haveNextNextGaussian = false;
    }

    private String[] userIds = new String[] { "aMuFfwmIa6fsdFfCS87ipA4ljAEKok",
                    "bMuFfwmIa6fsdFfCS87ipA4ljAEKok" };
    private String[] localRebort = new String[] { "aRebort", "bRebort" };
    private Integer needRunTime;
    private Long startTime = 0L;
    private Long endTime = 0L;

    public static void main(String[] args) throws UnsupportedEncodingException {
            Test test = new Test();
            test.runner(test,"你好");
    }

    public void runner(final Test test,final String firstText) {
            Random random = new Random();
            final Integer firstSpeed = random.nextInt(2);
            needRunTime = (Integer) random.nextInt(1 * 60 * 1000 + 1);
            String shouldTime = getTimeStr(needRunTime);
            System.out.println("本次聊天只限" + shouldTime + ",時間到了就得熄燈了!");
            startTime = System.currentTimeMillis();
            try {
                    test.rebort2rebort(test, firstText, firstSpeed);
            } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            }
            /*final Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                            try {
                                    if (index == 0) {
                                                    test.rebort2rebort(test, firstText, firstSpeed);
                                            index = 1;
                                    } else {
                                            test.rebort2rebort(test, localString, localIndex);
                                    }
                                    timer.cancel();
                                    System.out.println("好了,熄燈了,早點睡吧,別聊天了!");
                            } catch (UnsupportedEncodingException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                            }
                    }
            },0,1000);*/
    }

    private static String getTimeStr(Integer time) {
            time = time / 1000;
            Integer second = 0;
            Integer minute = 0;
            if (time % 60 >= 0) {
                    second = time - time / 60 * 60;
                    time = time / 60;
                    if (time % 60 >= 0) {
                            minute = time - time / 60 * 60;
                            time = time / 60;
                    }
            }
                    return (time == 0L ? "" : (time + "小時")) + (minute==0?"": (minute + "分鐘")) + second
                                    + "秒";
    }

    public void rebort2rebort(final Test test, String text, Integer localIndex)
                    throws UnsupportedEncodingException {
            ExpressDoService expressService = new ExpressDoService();
            String respContent;
            String bTalk;
            respContent = expressService.getRebortTalk(text, userIds[localIndex]);
            System.out.println(localRebort[localIndex] + ">>>Said>>>To>>>"
                            + localRebort[(localIndex + 1) / 2] + text);
            BaseRebortResp baseRebortResp = JSONUtil.jsonToBean(respContent,
                            BaseRebortResp.class);
            bTalk = baseRebortResp.getText();
            localIndex = (localIndex + 1) % 2;
            endTime = System.currentTimeMillis();
            if((endTime-startTime-needRunTime)>=0){
                    System.out.println("好了,熄燈了,早點睡吧,別聊天了!");
                    System.exit(0);
            }
            test.rebort2rebort(test, bTalk, localIndex);
    }

    public Integer getNeedRunTime() {
            return needRunTime;
    }

    public void setNeedRunTime(Integer needRunTime) {
            this.needRunTime = needRunTime;
    }
}

enter image description here

相關文章