天天看點

如何內建圖靈機器人,簡單實作語音聊天

圖靈機器人有三個基本功能:

(1)語音識别:将語音識别成相應的文本。

(2)語義了解:将文本識别成領域相關的語義結果。

(3)語音合成 : 将文本轉化成語音讀出。

要內建圖靈機器人,首先要進行圖靈官網的注冊,這裡就一筆帶過,按照步驟進行注冊即可。

然後建立自己的機器人,

如何內建圖靈機器人,簡單實作語音聊天

注意這裡我們選擇的是自定義機器人,(qq,微信等機器人我隻實作了一下qq機器人,很簡單,可以自己下載下傳文檔根據步驟走就ok)

如何內建圖靈機器人,簡單實作語音聊天

填好上面的資訊,在這裡下載下傳iOS SDK和內建文檔。走到這裡我們的機器人就建立完成。

因為圖;圖靈機器人的的語音識别等使用的是百度語音,是以我們還需要去百度平台進行建立項目擷取權限拿到百度的AppID、APIKey、Secret Key。具體請 參考 http://yuyin.baidu.com/

然後進行建立項目內建。

一、首先要導入依賴的Framework:

1. SystemConfiguration.framework 2. Foundation.framework

3. AVFoundation.framework

4. GLKit.framework

5. OpenGLES.framework 6. libz.1.dylib

7. Security.framework

8. CFNetwork.framework 9. CoreLocation.framework

二、第三方庫

IOS SDK 依賴以下第三方庫(SDK 包中已提供,請內建到應用工程中)

1. OpenUDID

2. JSONKit

3. TTTAttributedLabel

4. Core AudioUtility(蘋果 Audio 庫)

對于采用 ARC 記憶體管理方式的工程,需要利用 Non-ARC 方式表姨 OpenUDID 和 JSONKit(對相應檔案添加 Compiler Flags 為-fno-objc-arc)

三、其他事項。

因為 SDK 包中采用 Objective C++實作,是以需要保證工程中引用靜态庫頭檔案的實作 檔案的擴充名必須為.mm。

四、iOS9的适配問題

1、首先是http的通路。

在plist檔案中添加:

<key>NSAppTransportSecurity</key> <dict>
<key>NSAllowsArbitraryLoads</key>
<true/> </dict>
           

2、BITCODE 問題

由于底層的百度 SDK 編譯時采用 ENABLE_BITCODE 模式,是以基于圖靈 SDK 的應用程式也不能采用 ENABLE_BITCODE 模式。如下圖,将bitcode設定為NO。

如何內建圖靈機器人,簡單實作語音聊天

五、其他的一些配置

1、在Build Setting -> Build Active Architecture Only 設定為No

2、Build Setting -> Build Options -> Debug Information Format 設定為DWARF with DSYM File

3、Build Setting -> Build Options -> Enable Testability 設定為NO

4、Build Setting ->Linking -> Other Linker Flags 添加 -all_load

注:以上配置是我在內建的時候報錯而總結的一些配置。

六、導入SDK

将下載下傳的sdk包打開找到SDK Package這個檔案夾。此檔案夾包含三個檔案夾,一個是頭檔案,一個是.a 檔案包,一個是上面第二條所說的依賴的第三方庫。

如何內建圖靈機器人,簡單實作語音聊天
如何內建圖靈機器人,簡單實作語音聊天

這裡三個.a檔案隻需要導入最後一個即可。

下面進行撸代碼:

//
//  ViewController.m
//  Robot1
//
//  Created by XF on 16/8/11.
//  Copyright © 2016年 xf. All rights reserved.
//

#import "ViewController.h"
#import "TRRVoiceRecognitionManager.h"
#import "UserDefine.h"
#import "GlobalNetWorking.h"
#import "TRRTuringAPIConfig.h"
#import "TRRTuringRequestManager.h"
#import "TRRSpeechSythesizer.h"
@interface ViewController ()<TRRVoiceRecognitionManagerDelegate>

@property (strong, nonatomic) TRRVoiceRecognitionManager *sharedInstance;

@property (nonatomic, strong) TRRSpeechSythesizer *sythesizer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUILayout];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    _sharedInstance = [TRRVoiceRecognitionManager sharedInstance];
    [_sharedInstance setApiKey:BaiduAPIKey secretKey:BaiduSecretKey];
    _sharedInstance.delegate = self;
    NSArray *array = @[@()];
    _sharedInstance.recognitionPropertyList = array;
    self.sythesizer = [[TRRSpeechSythesizer alloc] initWithAPIKey:BaiduAPIKey secretKey:BaiduSecretKey];
}
#pragma mark - **************** 進行UI布局
-(void)setUILayout{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(, , , );
    button.center = self.view.center;
    [button setTitle:@"點選說話" forState:UIControlStateNormal];
    [button setTitle:@"正在說話" forState:UIControlStateHighlighted];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor brownColor] forState:UIControlStateHighlighted];
    UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTouchWith:)];
    [button addGestureRecognizer:longpress];
    button.layer.cornerRadius = ;
    button.layer.borderWidth = ;
    button.layer.borderColor = [UIColor blueColor].CGColor;
    [self.view addSubview:button];
}
#pragma mark - **************** 長按手勢
-(void)longTouchWith:(UILongPressGestureRecognizer *)longPress{
    if (longPress.state == UIGestureRecognizerStateBegan) {
        [_sharedInstance startVoiceRecognition];
    }else if(longPress.state == UIGestureRecognizerStateEnded){
        [_sharedInstance stopRecognize];
    }

}
#pragma mark - **************** 語音識别結果
- (void)onRecognitionResult:(NSString *)result {
    NSLog(@"result = %@", result);
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:RobotAPIKey forKey:@"key"];
    [dic setObject:result forKey:@"info"];
    [dic setObject:@"123456" forKey:@"userid"];
    [self netWorkingWith:dic];
}
#pragma mark ---- 語音識别錯誤
- (void)onRecognitionError:(NSString *)errStr {
    NSLog(@"Error = %@", errStr);
}

- (void)onStartRecognize {
    NSLog(@"開始說話");
}

- (void)onSpeechStart {
    NSLog(@"檢測到已說話");
}

- (void)onSpeechEnd {
    NSLog(@"檢測到已停止說話");

}
#pragma mark - **************** 網絡請求
-(void)netWorkingWith:(NSDictionary *)dic{
    [GlobalNetWorking networkWithUrl:@"http://www.tuling123.com/openapi/api" andParametersDic:dic andSuccess:^(id rootObject, id datasObject, bool isSuccess) {
        //這裡擷取到機器人回報的回答
        NSString *text = rootObject[@"text"];
        NSLog(@"%@",text);
        //進行語音合成
        [self.sythesizer start: text];
    } andFailure:^(NSError *error) {
        [self.sythesizer start:@"網絡請求出錯!"];
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
           

demo 下載下傳位址:http://download.csdn.net/detail/qq_34195670/9602252

github位址:https://github.com/goingmyway1/TuLingRobot

以上如有錯誤,請留言指出,不勝感激。

繼續閱讀