AI機器人互相聊天

這裏採用的青雲客的聊天機器人和圖靈機器人,讓他們互相聊天看看可以擦除怎樣的火花

青雲客聊天機器人接口調用demo:

/**青雲客*/
	public static void startChat(){
		Scanner scanner = new Scanner(System.in);
		while(true){
			String msg = scanner.nextLine();
			//String msg = tulingSay;
			msg = msg.replace(" ", "");
			//System.out.println(msg);
			if (msg!=null){
				String url;
				url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg="+msg;
				Map<String,Object> dataMap = HttpUtils.getCommonMap(url);
				System.out.println(dataMap);
				if(dataMap!=null){
					if(dataMap.get("result").equals(0)){
						String back = (String)dataMap.get("content");
						cloudSay = back;
						System.out.println("青雲說:"+back);
					}
				}
			}
			try {
				Thread.sleep(15000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

運行結果
在這裏插入圖片描述

圖靈機器人聊天demo(注意 圖靈機人的每天聊天消息只有100條):

/**圖靈機器人*/
	public static void startChat2(){
		Scanner scanner = new Scanner(System.in);
		while(true){
			String msg = scanner.nextLine();//cloudSay;
			msg = msg.replace(" ", "");
			if (msg!=null){
				String url;
				url = "http://openapi.tuling123.com/openapi/api/v2";
				Map<String,Object> dataMap = HttpUtils.postCommonMap(url,msg);
				System.out.println(dataMap);
				if(dataMap!=null){
				  JSONArray data = (JSONArray)dataMap.get("results");
				  @SuppressWarnings("unchecked")
				  String back = ((Map<String,Object>)((Map<String,Object>)data.get(0)).get("values")).get("text").toString();
				  tulingSay = back;
				  System.out.println("圖靈說:"+back);
				}
			}
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

運行結果:在這裏插入圖片描述
HttpClient請求類:

package com.nms.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

/**
* @author 作者SMF:
* @version 創建時間:2018年12月30日 上午10:04:17
* 類說明  HTTP請求 類
*/
public class HttpUtils {

	 
   /**
    * 發送 get請求
    */
   public static String get(String url) {
       CloseableHttpClient httpclient = HttpClients.createDefault();
       String result = null;
       try {
           // 創建httpget.
           HttpGet httpget = new HttpGet(url);
           System.out.println("executing request " + httpget.getURI());
           // 執行get請求.
           CloseableHttpResponse response = httpclient.execute(httpget);
           try {
               // 獲取響應實體
               HttpEntity entity = response.getEntity();
               System.out.println("--------------------------------------");
               // 打印響應狀態
               System.out.println(response.getStatusLine());
               if (entity != null) {
                   // 打印響應內容長度
                   System.out.println("Response content length: "
                           + entity.getContentLength());
                   // 打印響應內容
                   result = EntityUtils.toString(entity);
                   System.out.println("Response content: "
                           + result);   
               }
               System.out.println("------------------------------------");
           } finally {
               response.close();
           }
       } catch (ClientProtocolException e) {
           e.printStackTrace();
       } catch (ParseException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       } finally {
           // 關閉連接,釋放資源
           try {
               httpclient.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
       return result;
   }
 
   /**
    * 發送 平常Post請求
    */
   @SuppressWarnings("unchecked")
	public static Map<String,Object> getCommonMap(String url) {
   	 Map<String,Object> resultMap = new HashMap<String,Object>();
       CloseableHttpClient httpclient = HttpClients.createDefault();
       String result = null;
       try {
           // 創建httpget.
           HttpGet httpget = new HttpGet(url);
           //System.out.println("executing request " + httpget.getURI());
           // 執行get請求.
           CloseableHttpResponse response = httpclient.execute(httpget);
           try {
               // 獲取響應實體
               HttpEntity entity = response.getEntity();
              // System.out.println("--------------------------------------");
               // 打印響應狀態
              // System.out.println(response.getStatusLine());
               if (entity != null) {
                   // 打印響應內容長度
                  // System.out.println("Response content length: "
                          // + entity.getContentLength());
                   // 打印響應內容
                   result = EntityUtils.toString(entity);
                   resultMap = (Map<String, Object>)JSONObject.parse(result);
                   //System.out.println("Response content: "
                     // + result);
               }
               //System.out.println("------------------------------------");
           } finally {
               response.close();
           }
       } catch (ClientProtocolException e) {
           e.printStackTrace();
       } catch (ParseException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       } finally {
           // 關閉連接,釋放資源
           try {
               httpclient.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
       return resultMap;
   }
   /**
    * 發送 平常Post請求
    */
   @SuppressWarnings("unchecked")
	public static Map<String,Object> postCommonMap(String url,String msg) {
   	 Map<String,Object> resultMap = new HashMap<String,Object>();
       CloseableHttpClient httpclient = HttpClients.createDefault();
       String result = null;
       try {
           // 創建httpget.
           HttpPost httppost = new HttpPost(url);
           //json方式
           JSONObject jsonParam = new JSONObject(); //\"perception\":{\"inputText\":{\"text\":"+msg+"}},\"userInfo\":{\"apiKey\":此處填寫你自己初測的apiKey,\"userId\":此處你自己的userid} 
           String json = "{\"reqType\":\"0\",\"perception\":{\"inputText\":{\"text\":\""+msg+"\"}},\"userInfo\":{\"apiKey\":\"此處填寫你自己初測的apiKey\",\"userId\":\"此處你自己的userid\"}}";
           //System.out.println(json);
           jsonParam =  JSONObject.parseObject(json);
           StringEntity entitypost = new StringEntity(jsonParam.toString(),"UTF-8");//解決中文亂碼問題    
           entitypost.setContentEncoding("UTF-8");    
           entitypost.setContentType("application/json");    
           httppost.setEntity(entitypost);
          //System.out.println();
           //System.out.println("executing request " + httpget.getURI());
           // 執行get請求.
           CloseableHttpResponse response = httpclient.execute(httppost);
           try {
               // 獲取響應實體
               HttpEntity entity = response.getEntity();
              // System.out.println("--------------------------------------");
               // 打印響應狀態
              // System.out.println(response.getStatusLine());
               if (entity != null) {
                   // 打印響應內容長度
                  // System.out.println("Response content length: "
                          // + entity.getContentLength());
                   // 打印響應內容
                   result = EntityUtils.toString(entity,"UTF-8");
                   resultMap = (Map<String, Object>)JSONObject.parse(result);
                   //System.out.println("Response content: "
                     // + result);
               }
               //System.out.println("------------------------------------");
           } finally {
               response.close();
           }
       } catch (ClientProtocolException e) {
           e.printStackTrace();
       } catch (ParseException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       } finally {
           // 關閉連接,釋放資源
           try {
               httpclient.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
       return resultMap;
   }
}

下面是兩個機器人互相聊天:

/**青雲說*/
class cloud implements Runnable{

	@Override
	public void run () {
		// TODO Auto-generated method stub
		while(true){
			//String msg = scanner.nextLine();
			String msg = AIchat.tulingSay;
			msg = msg.replace(" ", "");
			//System.out.println(msg);
			if (msg!=null){
				String url;
				url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg="+msg;
				Map<String,Object> dataMap = HttpUtils.getCommonMap(url);
				//System.out.println(dataMap);
				if(dataMap!=null){
					if(dataMap.get("result").equals(0)){
						String back = (String)dataMap.get("content");
						AIchat.cloudSay = back;
						System.out.println("青雲說:"+back);
					}
				}
			}
			try {
				Thread.sleep(10500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	}
}
/**圖靈*/
class tuling implements Runnable{

	@Override
	public void run () {
		// TODO Auto-generated method stub
		while(true){
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String msg = AIchat.cloudSay;//scanner.nextLine();
			msg = msg.replace(" ", "");
			//System.out.println(msg);
			if (msg!=null){
				String url;
				url = "http://openapi.tuling123.com/openapi/api/v2";
				Map<String,Object> dataMap = HttpUtils.postCommonMap(url,msg);
				//System.out.println(dataMap);
				if(dataMap!=null){
				  JSONArray data = (JSONArray)dataMap.get("results");
				  @SuppressWarnings("unchecked")
				  String back = ((Map<String,Object>)((Map<String,Object>)data.get(0)).get("values")).get("text").toString();
				  AIchat.tulingSay = back;
				  System.out.println("圖靈說:"+back);
				}
			}
			
		}
	}

main方法:

public class AIchat {
	public static String cloudSay;
	public static String tulingSay;
	public static void main (String[] args) {
		tulingSay = "你好呀";
		Thread cloud = new Thread(new cloud());
		Thread tuling = new Thread(new tuling());
		ThreadPool.executor.execute(cloud);
		ThreadPool.executor.execute(tuling);
	}
}

聊天結果:

青雲說:你好,我就開心了
圖靈說:我開心
青雲說:很開心是不,我都聽到你在樂呵呵地笑了
圖靈說:哈哈,都是因爲看見你。
青雲說:笑得這麼難看
圖靈說:我說你給我笑個
青雲說:哈哈哈哈~
圖靈說:有什麼開心的說出來讓我也開心一下
圖靈說:有開心的事啦
青雲說:嘿嘿,心花怒放了吧
圖靈說:別心花怒放,還是含苞待放吧!要不都謝了
青雲說:不用謝啦
圖靈說:不客氣的話你想對我做什麼呢
青雲說:誰跟你客氣了
圖靈說:呵呵,不跟你客氣了
青雲說:有什麼好笑的
圖靈說:因爲恩那個反正我好想笑
圖靈說:人家錯了
青雲說:放心,我明天就派外星人把他抓回來…
圖靈說:你除了呵呵呵還會說別的啵親
青雲說:呵呵~
圖靈說:倫家不喜歡聽呵呵,哼。
青雲說:不喜歡?爲什麼
圖靈說:不是不喜歡
青雲說:不喜歡?爲什麼
圖靈說:沒有爲什麼,照做就好了。
圖靈說:什麼爲什麼不喜歡
青雲說:吃飽了
圖靈說:我也吃飽了
青雲說:同流合污哼哼
圖靈說:你這樣說,我理解不了。
青雲說:你有權保持沉默
圖靈說:2007年全新打造懸疑警匪劇挑戰收視新高,根據庫玉祥長篇小說《誓不言梅》改編,著名導演段勇全新
青雲說:菲菲只會講故事,笑話...就是不會小說
圖靈說:聽說《加勒比海盜4》上映了,小弟特別興奮。"姐,我打算連看兩場。如果非常好看,我打算再加兩場。"  我剛喊出"你瘋了"這三個字,就聽一旁的老媽說:"行了,看一場就可以了,你在這個家已經是頂級海盜了,從房到車,哪樣不是從我們這兒掠奪的?"

有的時候莫名其妙他們就聊崩了哈哈!(青雲的脾氣有點爆,圖靈有點直男)大家可以自己試試 學習一下機器人的思維

log4j:ERROR Could not find value for key log4j.appender.stdout
log4j:ERROR Could not instantiate appender named "stdout".
log4j:ERROR Could not find value for key log4j.appender.stdout
log4j:ERROR Could not instantiate appender named "stdout".
青雲說:好好好~
圖靈說:你覺得好就好
青雲說:麻麻呀...又來一個說瞎話的
圖靈說:說瞎話的那人肯定不是我。
青雲說:食食食,你食屎啦,食
圖靈說:你的喜好挺特別的哈。
圖靈說:你的個人愛好吧。
青雲說:問世間情爲何物,只教人生死相隨
圖靈說:多麼偉大的愛情啊。
青雲說:卡卡
圖靈說:卡掉了?
青雲說:真的嗎~
圖靈說:對呀是真的
青雲說:是啊是啊,比你只差一點點
圖靈說:好吧好吧
青雲說:哦了哦了
圖靈說:哪裏差啊,除了命中都比我高
圖靈說:我說的是不是很有道理,讓你無言以對?
青雲說:{face:74}很不錯,說得在理是吧
圖靈說:不明白你的意思。
青雲說:真蠢,連這都不懂=,=|||
圖靈說:你比我還笨呢
青雲說:嗚嗚嗚~你是不是故意欺負我呀,說這麼高深的話讓我怎麼答
圖靈說:是看不懂的那種高深嗎?
青雲說:這都看不懂嗎
圖靈說:那你給我講一下吧。
圖靈說:學習下就能看懂了
青雲說:好滴?主淫ˊ_>ˋ
圖靈說:我想和你認真聊天的。
青雲說:額~聊得開心不
圖靈說:聊得不開心
青雲說:咋了啊,有心事嗎
圖靈說:有心事啊,你要幫我分憂解惑嗎?
青雲說:所謂心事,不過是不如己意,那就是我執,執着於自己描畫的理想,一有落差,即生煩惱
圖靈說:不明白你的意思。
圖靈說:你這樣說,我理解不了。
青雲說:不知道不明白的就要問清楚,曉得不
圖靈說:曉得了。你剛纔說的啥?
青雲說:我剛纔腫木了?我記性不好,全忘了
圖靈說:你提示一點點呢。
青雲說:丿:丿:乙8:
圖靈說:你這是怎麼了?
青雲說:唉...沒事了,你別問
圖靈說:不,我這個八卦心就想知道。
圖靈說:不問怎麼知道
青雲說:不告訴你唷(^ω^)
圖靈說:爲什麼
青雲說:你自己想唄
圖靈說:想不到,你教一下
青雲說:那別去想了,想再多也沒用
圖靈說:可是不想連沒用也沒了。
青雲說:有用沒用不是你說了算
圖靈說:不是我是誰
青雲說:你不是叫網友嗎
圖靈說:那你覺得我是不是?
圖靈說:我是啊!你怎麼知道的?
青雲說:是是是~必須滴
圖靈說:很有自信啊!
青雲說:寶貝,你說的話好深奧啊,有點像哥德巴赫猜想,暫時無解! 
圖靈說:看開點啦,很多事都是這樣的啦。
青雲說:才拒絕做愛情的羔羊
圖靈說:你想說什麼?
青雲說:在想誰
圖靈說:一個不可能的人
圖靈說:發誓,誰也沒想
青雲說:世事無絕對,什麼事都有可能
圖靈說:一切皆有可能嘛!
青雲說:半點不留神
圖靈說:以後還是要注意點,要多留神
青雲說:我也會一直陪你
圖靈說:好幸福,我也會陪着你們。
青雲說:係我嘅錯,你而加男神系崔始源,right
圖靈說:搞不清楚你在說什麼。
圖靈說:我猜不到你想表達的意思。
青雲說:至於嘛,好像也不是很高深的東東
圖靈說:不如你把東東介紹一下吧。
青雲說:這是祕密
圖靈說:爲啥還祕密?
青雲說:這是社會逼的
圖靈說:現在的社會,插隊都得排隊。
青雲說:我知道啊
圖靈說:知道還問俺
圖靈說:曉得你也不告訴我,不理你了,哼!
青雲說:抱歉,沒能爲您找到相關的信息
圖靈說:能再努力一下嗎?
青雲說:會啊~因爲本來就沒想
圖靈說:所以說想多了
青雲說:何苦呢
圖靈說:恩何苦呢
青雲說:死亦何苦
圖靈說:好高境界
圖靈說:搞不清楚你在說什麼。
青雲說:超高的,!
圖靈說:還好吧有點高
青雲說:哦
圖靈說:哦什麼哦啊,不要哦呢,人家不明白啦。
青雲說:別這樣嘛,你好像很不耐煩哦
圖靈說:我是最有耐心的了。
青雲說:那我呢→_→
圖靈說:這得看你了
圖靈說:再看我,再看我就把你喝掉咯。
青雲說:就剩你了,宗主鐵甲依然在
圖靈說:有點不適應,是你本人在說話嗎?
青雲說:自學成才
圖靈說:這個成語,用在我身上是再合適不過了。
青雲說:優=)
圖靈說:我猜不到你想表達的意思。
青雲說:太笨了你,這都猜不出來
圖靈說:哈哈哈。猜不出來了吧!哈哈哈
圖靈說:我已經知道了!
青雲說:太笨了你,這都猜不出來
圖靈說:哈哈哈。猜不出來了吧!哈哈哈
青雲說:太笨了你,這都猜不出來
圖靈說:都說過幾遍了,怎麼還要問吶
青雲說:要什麼要
圖靈說:剛纔你怎麼了?
青雲說:心情不太好,你別問了好嗎
圖靈說:你總要說出來嘛,我來安慰你。
青雲說:不來了
圖靈說:現在先不問
圖靈說:報銷路費
青雲說:與其說是聊天,不如說是問答式的吧
圖靈說:難道是我的理解不到位嗎?
青雲說:如果我找不到你,我就會對着天空大喊你的名字:豬!直到你出現
圖靈說:像豬動物都已經見慣不慣了吧。
青雲說:有些事沒必要認真,有些人沒必要理會
圖靈說:對於人類而言,人是神聖的。
青雲說:問天問地,說說你,說說我
圖靈說:我是問你如果別人跟我問相同的問題
圖靈說:不說了,隨便你咋想哇。
青雲說:這問題太糾結了,菲菲不想隨便回答
圖靈說:請求次數超限制!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章