標(biāo)題: 利用Python實(shí)現(xiàn)的聊天機(jī)器人源碼 [打印本頁]

作者: 草原狼11    時(shí)間: 2018-4-27 17:05
標(biāo)題: 利用Python實(shí)現(xiàn)的聊天機(jī)器人源碼
利用Python實(shí)現(xiàn)的聊天機(jī)器人源碼如下:
  1. #Jinko Robot
  2. import json;
  3. import urllib.request;
  4. import urllib.parse;

  5. class JinkoRobot:
  6.        
  7.         __answer = '';

  8.         def __init__(self):
  9.                 pass;

  10.         #傾聽話語
  11.         def listenFor(self, string):
  12.                 self.__answer = self.thinking(string);

  13.         # 思考著
  14.         def thinking(self, string):
  15.                 says = urllib.parse.quote_plus(string);
  16.                 f = urllib.request.urlopen("http://www.tuling123.com/openapi/api?key=4bc32d41c10be18627438ae45eb839ac&info=" + says);
  17.                 json_str = f.read();
  18.                 thinkdata = json.loads(json_str.decode('utf-8'));
  19.                 f.close();
  20.                
  21.                 if(thinkdata['code'] > 40000 and thinkdata['code'] < 40010):
  22.                         return "今天Jinko被你問得有點(diǎn)累了, 過會(huì)再問吧!";

  23.                 if(thinkdata['code'] == 200000):
  24.                         return thinkdata['text'] + ", 猛戳這里>>" +  thinkdata['url'];

  25.                 if(thinkdata['code'] == 302000) :
  26.                         info = thinkdata['text'];

  27.                         for content in thinkdata['list']:
  28.                                 info += "\n\n>" + content['article'] \
  29.                                                 + "  來源于" + content['source'] \
  30.                                                 + "  詳細(xì)信息請(qǐng)猛戳這里>>" + content['detailurl'];
  31.                        
  32.                         return info;

  33.                 if(thinkdata['code'] == 305000):
  34.                         info = thinkdata['text'];

  35.                         for key in thinkdata['list']:
  36.                                 info += "\n\n>" + key + ": 車次>" + content['trainnum'] \
  37.                                                 + "  從" + content['start'] + "到" + content['terminal'] \
  38.                                                 + "  發(fā)車時(shí)間:" + content['starttime'] \
  39.                                                 + "  到達(dá)時(shí)間:" + content['endtime'] \
  40.                                                 + "  詳細(xì)信息請(qǐng)猛戳這里>>" + content['detailurl'];
  41.                        
  42.                         return info;

  43.                 return thinkdata['text'];

  44.         #和你交流回答
  45.         def answer(self):
  46.                 return self.__answer;
復(fù)制代碼

所有資料51hei提供下載:
ChatWithRobot.zip (4.14 KB, 下載次數(shù): 22)



作者: q191185266    時(shí)間: 2018-8-16 19:18
老哥源代碼能發(fā)郵箱嗎?我的郵箱191185266@qq.com




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1