文章首发于github.io 2018-08-04 12:43:20

google翻译的强大,就像我们公司的slogan : "让语言无国界,让世人心相通"

友情提示: google相关的服务都是需要翻墙的,不然在大陆无法访问

写这篇文章的目的就是为了整合一下这方面的资料, 大概是因为被墙,网上关于这方面的资料很少,有些是过于老旧,甚至是不适用的,所以就不得不做个笔记就当作是探探路。

google语音识别

  1. google注册相关的API_KEY, 地址: https://cloud.google.com/speech-to-text/docs/samples
  2. 下载demo https://github.com/GoogleCloudPlatform/ios-docs-samples/tree/master/speech/Objective-C/Speech-REST-Nonstreaming
  3. 录制语音文件存于沙盒, 上传语音文件到 https://speech.googleapis.com/v1/speech:recognize?key=xxxxx

核心代码:

  NSString *service = @"https://speech.googleapis.com/v1/speech:recognize";
service = [service stringByAppendingString:@"?key="];
service = [service stringByAppendingString:API_KEY]; NSData *audioData = [NSData dataWithContentsOfFile:[self soundFilePath]];//沙盒里的录音文件 具体看google提供的demo
NSDictionary *configRequest = @{@"encoding":@"LINEAR16",
@"sampleRateHertz":@(SAMPLE_RATE),
@"languageCode":@"zh-cn",
@"maxAlternatives":@30};
NSDictionary *audioRequest = @{@"content":[audioData base64EncodedStringWithOptions:0]};
NSDictionary *requestDictionary = @{@"config":configRequest,
@"audio":audioRequest};
NSError *error;
NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestDictionary
options:0
error:&error]; NSString *path = service;
NSURL *URL = [NSURL URLWithString:path];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
// if your API key has a bundle ID restriction, specify the bundle ID like this:
[request addValue:[[NSBundle mainBundle] bundleIdentifier] forHTTPHeaderField:@"X-Ios-Bundle-Identifier"];
NSString *contentType = @"application/json";
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:requestData];
[request setHTTPMethod:@"POST"]; NSURLSessionTask *task =
[[NSURLSession sharedSession]
dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
dispatch_async(dispatch_get_main_queue(),
^{
NSString *stringResult = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
_textView.text = stringResult;
NSLog(@"RESULT: %@ ", stringResult);
});
}];
[task resume];

google 文本翻译

文本翻译和语音识别类似,都需要API_KEY,同样是POST请求

/**
MARK:- 文本互转 @param text 文本
@param targetCode 翻译的对应语言代码
@param callBack 回调结果json字符串
*/
- (void)translateOriginText:(NSString *)text
targetCode:(NSString *)targetCode
callBack:(void(^)(NSString *objStr))callBack{
NSString *urlStr = [NSString stringWithFormat:@"https://translation.googleapis.com/language/translate/v2?key=%@",API_KEY];
NSURL *URL = [NSURL URLWithString: urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request addValue:[[NSBundle mainBundle] bundleIdentifier] forHTTPHeaderField:@"X-Ios-Bundle-Identifier"];
NSString *contentType = @"application/json";
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
// q: 原文本 tagret: 目标语言的代码 zh-cn,en-us...等
NSDictionary *requestDictionary = @{ @"q" : text, @"target" : targetCode };
NSError *error;
NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestDictionary options:0 error:&error];
[request setHTTPBody:requestData];
[request setHTTPMethod:@"POST"];
NSURLSessionTask *task =
[[NSURLSession sharedSession]
dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
dispatch_async(dispatch_get_main_queue(),
^{
NSString *stringResult = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"RESULT: %@ ", stringResult);
!callBack? : callBack(stringResult);
});
}];
[task resume];
}

google 语音合成

  • 合成是免费的 , 然后是这个接口 http://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&ttsspeed=1
  • 参数组成
//基地址
BASE_URL = @"http://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&ttsspeed=1" //拼接规则和get请求类似
total //标准为100个字符为一组 然后total是总共多少组
idx //下标,第几组
textlen //所有文字的计数,就是这个字符串的长度
q //合成的文字内容,中文的话需要转编码utf-8最为合适
tl //语言,跟识别的语言一致

主要参数介绍:

ie: 输入参数编码 input-encode 一般为utf-8

oe: 输入参数编码 output-encode 一般为utf-8

ttsspeed: 合成的语速

tl: 目标语言代码 target language

hl: 当地语言代码 home language

url编码前:

http://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&ttsspeed=1&total=1&idx=1&textlen=6&q=某某某是傻逼&tl=zh-cn

url编码后:

http://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&ttsspeed=1&total=1&idx=1&textlen=6&q=%E9%99%88%E8%89%AF%E8%89%AF%E6%98%AF%E5%82%BB%E9%80%BC&tl=zh-cn

核心代码

///MARK:- 文本 转 语音在线合成
- (void)synthesisAudioWithTranslateWithText:(NSString *)text{
NSString *baseUrl = @"http://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&ttsspeed=1";
//url编码
NSCharacterSet *encodeUrlSet = [NSCharacterSet URLQueryAllowedCharacterSet];
NSString *encodeContent = [text stringByAddingPercentEncodingWithAllowedCharacters:encodeUrlSet];
NSString *urlStr = [NSString stringWithFormat:@"%@&total=1&idx=1&textlen=%ld&q=%@&tl=%@",baseUrl,text.length,encodeContent,self.targetCode];
NSURL *url = [NSURL URLWithString: urlStr];
//播放拼接的链接
UIWebView *web = [[UIWebView alloc] init];
web.hidden = YES;
[self.view addSubview: web];
NSURLRequest *req = [[NSURLRequest alloc] initWithURL:url];
[web loadRequest: req];
}

主流的几种语言的代码对照表:

国家 languageCode

集成google翻译的小tips的更多相关文章

  1. 使用jQuery集成Google翻译

    利用jQuery,轻松将google翻译集成到你的web应用中. 1. [代码][JavaScript]代码     ​1<script src="Scripts/Translator ...

  2. Google搜索引擎使用小技巧

    相信大家都知道,利用Google等搜索引擎进行信息查证是翻译过程中十分重要的一环.事实上,掌握信息搜索的技巧和方法,不仅对翻译工作大有帮助, 在网络信息时代,学会充分利用搜索引擎,在很多情况下都可以达 ...

  3. delphi 利用HTTP的POST方法做个在线翻译的小工具 good

    最近做了一个英汉小翻译的东东,用的是VC,ADO + Access访问数据库,单词数据库是从金山打字通2002弄来的.后来想了想,想再加个在线翻译的功能,记得经常使用GOOGLE翻译网站的在线翻译,也 ...

  4. Python调用Google翻译

    出自:http://blog.csdn.net/zhaoyl03/article/details/8830806 最近想动手做一个文档自动下载器,需要模拟浏览器的行为.虽然感觉思路上没有困难,但在技术 ...

  5. 破解google翻译API全过程

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/6554340.html 前言 google的翻译不得不承认它 ...

  6. Google翻译PDF文档

    Google翻译PDF文档 翻译软件虽多如牛毛,但有关整段/全文翻译,堪用的软件极少, 涉及专业技术的文献.胜任翻译工作的人力稀缺.少不了project师讴心沥血. 由于多是PDF格式.即使要翻译个概 ...

  7. Android 应用程序集成Google 登录及二次封装

    谷歌登录API:  https://developers.google.com/identity/sign-in/android/ 1.注册并且登录google网站 https://accounts. ...

  8. HTML meta锚点跳转 小tips

    小tips meta锚点跳转 http://www.zhangxinxu.com/wordpress/2015/03/meta-http-equiv-refresh-content/

  9. 添加了有道生词本的 chrome google翻译扩展和有道翻译扩展

    在chrome发布项目,需要先花美金认证,还得要美国ID,无奈. 直接上源码,需手动导入. 原始项目源码并未开源,个人是从chrome本地文件里拿出来的,拓展来的,侵删(本来想着自已写一个,业余时间, ...

随机推荐

  1. [HNOI2003]操作系统 优先队列用法

    题:https://www.cometoj.com/problem/1046 #include<bits/stdc++.h> using namespace std; typedef lo ...

  2. sql语句查询去除重复语句的结果集

    返回operation表中time列的唯一值 语句1 select  time  from  operation   group  by  time   having count(time) > ...

  3. 牛客-Forsaken的数列(Treap)

    题目传送门 sol:第一次看题还真信了是用线段树来做,但是没什么想法,看了题解发现是我不会的Treap,然后花了几天时间学习了一下并补掉题目 无旋Treap #include <bits/std ...

  4. 通俗易懂JSONP讲解

    原文地址:http://www.cnblogs.com/dowinning/archive/2012/04/19/json-jsonp-jquery.html JSON的格式或者叫规则: JSON能够 ...

  5. Linux系统使用IBMV3700存储(可用)

    第一步:在存储上添加主机,主机采用ISCSI连接,此时需要主机的iqn 主机上获取IQN的命令如下: cat /etc/iscsi/initiatorname.iscsi 此时添加的主机状态处于脱机的 ...

  6. deeplearning.ai 序列模型 Week 1 RNN(Recurrent Neural Network)

    1. Notations 循环序列模型的输入和输出都是时间序列.$x^{(i)<t>}$表示第$i$个输入样本的第$t$个元素,$T_x^{(i)}$表示输入的第$i$个样本的元素个数:$ ...

  7. axios 传对象(JavaBean)到后台

    //user对象 let user = JSON.stringify({ userAccountNumber: own.userName, userPassword: own.userPassword ...

  8. Tuning xgboost in R:Part 1

    第一次调整Boosting算法的参数可能是一个非常艰难的任务.有很多参数可供选择,调整不同的参数会有不同的结果产生.最好的调参可能是取决于数据.每当我得到一个新的数据集,我都会学到一些新的东西.对分类 ...

  9. Java Design Patterns(2)

    1.Factory Design pattern 工厂设计模式的优点 (1)工厂设计模式提供了接口而不是实现的代码方法. (2)工厂模式从客户端代码中删除实际实现类的实例化.工厂模式使我们的代码更健壮 ...

  10. 转:zabbix 更改maps图标

    更改Zabbix map图标 Zabbix的maps用来图形化显示监控设备的拓扑图,并且以不同的标记显示故障事件,通过该图表很直观的显示设备的整体情况.系统默认的图标比较简陋,如图十一所示.通过更改系 ...