您当前的位置: 首页 >  unity

CoderZ1010

暂无认证

  • 3浏览

    0关注

    168博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity 接入有道智云AI - 文本翻译

CoderZ1010 发布时间:2021-11-06 08:53:29 ,浏览量:3

接入接口前首先需要申请应用ID和应用秘钥,登录有道智云AI开放平台,创建应用,获取应用ID和秘钥。

定义接口响应类数据结构,接口实际返回内容和官方文档有点出入,大概是文档未更新吧。

以下是官方文档给出的说明:

以下是本人测试获取到的响应结构:

定义该数据结构:

[Serializable]
public class Response
{
    /// 
    /// 单词校验后的结果 主要校验字母大小写、单词前含符号、中文简繁体
    /// 
    public string[] returnPhrase;
    public string query;
    /// 
    /// 错误返回码
    /// 
    public string errorCode;
    /// 
    /// 源语言和目标语言
    /// 
    public string l;
    /// 
    /// 翻译结果发音地址
    /// 
    public string tSpeakUrl;
    /// 
    /// 网络释义 不一定存在
    /// 
    public Web[] web;
    public string requestId;
    /// 
    /// 翻译结果
    /// 
    public string[] translation;
    public URL dict;
    public URL webdict;
    /// 
    /// 词义
    /// 
    public Basic basic;
    public bool isWord;
    /// 
    /// 源语言发音地址
    /// 
    public string speakUrl;
}
[Serializable]
public class Web
{
    public string key;
    public string[] value;
}
[Serializable]
public class URL
{
    public string url;
}
[Serializable]
public class Basic
{
    public string phonetic;
    public string[] explains;
}

封装接口:

public class YoudaoTranslator
{
    //应用ID和应用秘钥 通过在平台创建应用获取
    private static readonly string appKey = "**********";
    private static readonly string appSecret = "********************";

    /// 
    /// 将英文翻译为中文
    /// 
    /// 待翻译的文本
    /// 回调函数
    public static void EnglishToChinese(string content, Action callback)
    {
        Translate(content, "en", "zh-CHS", callback);
    }
    /// 
    /// 将中文翻译为英文
    /// 
    /// 待翻译的文本
    /// 回调函数
    public static void ChineseToEnglish(string content, Action callback)
    {
        Translate(content, "zh-CHS", "en", callback);
    }
    /// 
    /// 翻译
    /// 中文zh-CHS 英文en 日文ja 韩文ko 法文fr 德文de 俄文ru
    /// 其它语言查阅官方文档
    /// 
    /// 待翻译的文本
    /// 源语言
    /// 目标语言
    /// 回调函数
    public static void Translate(string content, string from, string to, Action callback)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://openapi.youdao.com/api");
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        //当前UTC时间戳(秒)
        string curtime = ((long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds / 1000).ToString();
        //UUID 唯一通用识别码
        string salt = DateTime.Now.Millisecond.ToString();
        string input = content == null ? null : content.Length  0) callback.Invoke(response.translation[0]); break;
                case 102: Debug.LogError($"不支持的语言类型"); break;
                case 103: Debug.LogError($"翻译文本过长"); break;
                case 108: Debug.LogError($"应用ID无效 注册账号登录后台创建应用和实例并完成绑定 可获得应用ID和应用密钥等信息"); break;
                case 113: Debug.LogError($"待翻译文本不能为空"); break;
                //其它错误代码含义查阅官方文档
                default: Debug.LogError($"翻译失败 错误代码[{errorCode}]"); break;
            }
        }
    }
}

测试:

public class Foo : MonoBehaviour
{
    private void Start()
    {
        YoudaoTranslator.EnglishToChinese("Hello everyone.", s => Debug.Log(s));
        YoudaoTranslator.ChineseToEnglish("测试", s => Debug.Log(s));
    }
}

关注
打赏
1653184800
查看更多评论
立即登录/注册

微信扫码登录

0.0691s