智能聊天機器人實現(源碼+解析),聊天機器人源碼
前言:
之前寫了一篇 《美女圖片採集器 (源碼+解析)》 得到了眾多朋友的支援, 發現這樣系列的教程還是挺受歡迎的, 也激勵我繼續寫下去。
也在那一篇文章中提過, 美女圖片採集只是我先前那個完整APP中的一個功能罷了, 還有其他幾個比較好玩的尚未開源, 之後有時間會逐一寫篇教程。
今天帶來的是智能聊天機器人實現(源碼+解析), 和上一篇教程一樣, 當你沒有女朋友的時候, 可以用它來打發時間。這裡的API是圖靈機器人提供的, 實現一個十分強大的機器人。
具體功能包括:
• 支援聊天對話、智能問答
• 擁有笑話、天氣、公交等豐富功能
• 支援自然語言處理及語義理解
• 數十億知識庫資料,應有盡有
運行效果:
源碼下載:
源碼已經傳到git上了, 歡迎下載學習。
下載連結: https://github.com/colin1994/tulingIOS
源碼解析:
一。仿介面
這個小demo的介面是仿的。只不過是簡化版的, 包括表情, 語音什麼的, 都省略了。
對於介面這一塊, 我這裡不多做介紹, 因為這並不是本教程主要內容。畢竟, 這個介面到自己實際項目中的時候, 肯定是需要自訂的。
這裡簡要介紹一下。
該介面分成兩部分:
1. UITableView: 顯示聊天列表, 其中, 左邊的是機器人回答, 右邊是自己的提問。
另外, 列表的每個cell, 由頭像和文字組成。 這個cell是自訂的, 詳細可以自己查看源碼。
列表添加:
//add UItableView self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height-88) style:UITableViewStylePlain]; [self.tableView registerClass:[ChartCell class] forCellReuseIdentifier:cellIdentifier]; self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone; self.tableView.allowsSelection = NO; self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"chat_bg_default.jpg"]]; self.tableView.dataSource=self; self.tableView.delegate=self; [self.view addSubview:self.tableView];
2. KeyBordVIew: 自訂的UIView, 用來顯示自訂的鍵盤視圖。
鍵盤添加:
//add keyBorad self.keyBordView=[[KeyBordVIew alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-44, self.view.frame.size.width, 44)]; self.keyBordView.delegate=self; [self.view addSubview:self.keyBordView];
另外, 鍵盤涉及彈出和收合操作操作, 這個需要在視圖載入之前, 註冊通知, 響應相關操作。
1.註冊通知
//註冊通知, 鍵盤收合, 彈出 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
2.響應操作
//鍵盤彈出響應-(void)keyboardShow:(NSNotification *)note{ CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGFloat deltaY=keyBoardRect.size.height; [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{ self.view.transform=CGAffineTransformMakeTranslation(0, -deltaY); }];}//鍵盤收合響應-(void)keyboardHide:(NSNotification *)note{ [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{ self.view.transform = CGAffineTransformIdentity; }];}
二。圖靈key擷取
用過一些第三方API的都知道, 通常我需要先註冊成為它的使用者, 才能擷取對應的key, 以便調用API。
圖靈也不例外, 你需要先註冊成為圖靈使用者, 然後有相關教程, 教你如何擷取自己的key, 以及正確的URL。這裡就不重複了。圖靈機器人官網連結
例如, 我這個demo裡面的key是:6c2cfaf7a7f088e843b550b0c5b89c26
對應的API是:http://www.tuling123.com/openapi/api?key=6c2cfaf7a7f088e843b550b0c5b89c26&&info=%@
所以, 你只要把這裡的key替換成你自己的就可以了。
三。圖靈API的使用
這裡使用了第三方網路請求庫ASI 和 json格式資料解析庫 JsonKit。
在匯入ASI的時候, 如果你的項目是ARC, 那麼, 請將對應的檔案設定成支援ARC即可。 (-fno-objc-arc)
另外, 要匯入一些架構
SystemConfiguration.framework
MobileCoreServices.framework
CFNetwork.framework
libz.dylib
接著就能利用ASI調用圖靈API,再利用jsonkit解析返回的資料了。
具體實現如下:
//每當編輯完問題後//1. 顯示自己的問題 messageType=1//2. 調用API,返回結果-(void)KeyBordView:(KeyBordVIew *)keyBoardView textFiledReturn:(UITextField *)textFiled{ //顯示自己的問題 ChartCellFrame *cellFrame=[[ChartCellFrame alloc]init]; ChartMessage *chartMessage=[[ChartMessage alloc]init]; chartMessage.icon=@"icon01.png"; chartMessage.messageType=1; chartMessage.content=textFiled.text; cellFrame.chartMessage=chartMessage; [self.cellFrames addObject:cellFrame]; [self.tableView reloadData]; //滾動到當前行 [self tableViewScrollCurrentIndexPath]; //利用使用者問題, 查詢結果 //API請求格式。 具體格式見圖靈官網 //6c2cfaf7a7f088e843b550b0c5b89c26 替換成你申請的key即可 NSString* urlString = [NSString stringWithFormat:@"http://www.tuling123.com/openapi/api?key=6c2cfaf7a7f088e843b550b0c5b89c26&&info=%@", textFiled.text]; //NSUTF8StringEncoding編碼。 避免中文錯誤urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //調用API NSURL *url = [NSURL URLWithString:urlString]; testRequest = [ASIHTTPRequest requestWithURL:url]; [testRequest setDelegate:self]; [testRequest startAsynchronous]; textFiled.text=@""; myTextField = textFiled;}#pragma mark - 返回機器人回答//調用API完畢, 返回圖靈回答結果//1. 收合鍵盤//2. 顯示回答內容- (void)requestFinished:(ASIHTTPRequest *)request{ //收合鍵盤 [myTextField resignFirstResponder]; // 當以文本形式讀取返回內容時用這個方法 // 解析返回的json資料 NSString *responseString = [request responseString]; self.testDic = [responseString objectFromJSONString]; self.testArr = [testDic objectForKey:@"text"]; //顯示回答內容 ChartCellFrame *cellFrame=[[ChartCellFrame alloc]init]; ChartMessage *chartMessage=[[ChartMessage alloc]init]; chartMessage.icon=@"icon02.png"; chartMessage.messageType=0; chartMessage.content=[NSString stringWithFormat:@"%@", self.testArr]; cellFrame.chartMessage=chartMessage; [self.cellFrames addObject:cellFrame]; [self.tableView reloadData]; //滾動到當前行 [self tableViewScrollCurrentIndexPath]; }// API請求失敗- (void)requestFailed:(ASIHTTPRequest *)request{ NSError *error = [request error]; NSLog(@"error --- %@",error); UIAlertView *alert_ = [[UIAlertView alloc]initWithTitle:@"提示" message:@"無網路可用,請檢查網路狀態" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles: nil]; [alert_ show];}
學習的路上, 與君共勉
一款自動聊天機器人,比較智可以的,有原始碼更好
我自己擁有一套QQ自動回複機器人系統......是我買的.....
用PHP寫的......該機器人綁定的QQ是465749449
這幾天我的機器人功能正在維護暫時不可用,我給你一個跟我用一樣系統的機器人QQ號------869936432
一份簡單的聊天機器人原始碼,要C++編寫的
那得看樓主希望聊天機器人有哪些功能了。
一般地,首先要能分析句子,區分名詞和形容詞,能記憶集合關係
然後就是隨機性地回答問題了