標籤:人工 ons 通用 res ctf key details tar lam
原文地址
http://blog.csdn.net/dfman1978/article/details/78468318
前言
目前使用語音輸入越來越普遍。語音輸入不僅高效,而且更加自然,符合人類的輸入方式。在這裡開發了一個匯率換算的 APP,展示了如何使用語音輸入和語音辨識。
APP簡介
這個 APP可以識別使用者的輸入,然後返回結果,並使用自然語言讀出來。
使用者可以點擊話筒表徵圖,進行語音輸入:“1元人民幣兌換多少美元”。或者說“350泰銖兌換多少人民幣”等等。結果也會通過語音讀出來
功能實現
語音辨識和語義理解都是通過Olami平台來實現的。這個需要去Olami平台進行註冊,然後就可以免費試用了。官網的地址https://olami.ai,網站上有豐富的教學資源具體的使用可以查看相關的文檔。OLAMI是由威盛電子(上海)有限公司人工智慧軟體研發團隊推出的一個人工智慧軟體開發平台,提供包括自然語音互動技術在內的全方位人機互動解決方案,覆蓋了眾多垂直領域的語義通用情境。
語音的讀取是使用了蘋果的AVSpeechSynthesis庫進行的。這個庫的使用也很簡單。這個庫是在AVFoundation中
由於 Olami平台已經對匯率的計算結果進行了內建計算,所以代碼裡處理的就比較少了。代碼的主要是處理傳過來的json資料
- (void)onResult:(NSData *)result { NSError *error; __weak typeof(self) weakSelf = self; NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers error:&error]; if (error) { NSSLog(@"error is %@",error.localizedDescription); }else{ NSString *jsonStr=[[NSString alloc]initWithData:result encoding:NSUTF8StringEncoding]; NSLog(@"jsonStr is %@",jsonStr); NSString *ok = [dic objectForKey:@"status"]; if ([ok isEqualToString:@"ok"]) { NSDictionary *dicData = [dic objectForKey:@"data"]; NSDictionary *asr = [dicData objectForKey:@"asr"]; if (asr) {//如果asr不為空白,說明目前是語音輸入 [weakSelf processASR:asr]; } NSDictionary *nli = [[dicData objectForKey:@"nli"] objectAtIndex:0]; NSDictionary *desc = [nli objectForKey:@"desc_obj"]; int status = [[desc objectForKey:@"status"] intValue]; if (status != 0) {// 0 說明狀態正常,非零為狀態不正常或者result為空白 [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"noresult" object:nil userInfo:nil]]; }else{ NSDictionary *semantic = [[nli objectForKey:@"semantic"] objectAtIndex:0]; NSString *result = [desc objectForKey:@"result"]; [self.delegate onResult:result]; } }else{ } } }
結果是放在 result欄位中
{ "data": { "asr": { "result": "三百九十八港幣是多少人民幣", "speech_status": 0, "final": true, "status": 0 }, "nli": [ { "desc_obj": { "result": "您要尋找的資訊:398港幣兌換338.22040人民幣", "source_currency": "398港幣", "status": 0 }, "data_obj": [ { "target_currency": "338.22040人民幣" } ], "type": "exchangerate" } ] }, "status": "ok"}
把這個欄位中的值傳出來就行了
具體的代碼可以參考 GitHub
https://github.com/lym-ay/RateDemo
匯率換算自然語言理解功能IOS DEMO