关于OLAMI(具体指南)
仅仅简略说下OSL(OLAMI 语法描述语言)。OSL由grammar、rule、slot、template四个元素组成。
1.grammar:即语法,用OSL描述自然语言的形式,用来匹配语料,
- “[ ]” 语法规则符号表示方括号中的内容是可选的;
- “|” 表示 “或是” 的关系,左右两边的内容可以二选一,例如
[你|您]表示 “你” 或是 “您” 两者皆可之意; - “( )” 表示括号中的内容是一个整体。
2.rule:用来保存一个由多个同义词组成的集合,同义词间用|隔开,在grammar中用<>调用rule。
3.slot:如果要查询城市天气,定义grammar时不可能穷举所有的城市名称,这时就可以定义一个slot,通过设置slot的规则,就可以用slot来提取语音输入的城市信息,例如:<city>的天气,就可以按照规则,从语音中自动提取城市名称,这个grammar就可以匹配所有的“XXX的天气”。
4.template:略。
除了OSL,还需要在OLAMI平台添加应用,在应用中使用自定义的模块,具体过程见具体指南。
游戏中脚本的设置
OLAMI提供了SpeechApiSample.cs和NluApiSample.cs两个脚本,把他们直接放置在unity的scripts文件夹下就可以了。
想要将输入的语音通过我们在OLAMI中设置的模块进行“翻译”,需要用到VoiceService这个脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Ai.Olami.Example;
public class VoiceService {
string url = "http://cn.olami.ai/cloudservice/api";
string key = "bfd34f6956cb4003adb56461e443611d";
string secret = "c20b4ab170144f45abfb19189bce0ab6";
NluApiSample nluApi;
SpeechApiSample speechApi;
private static VoiceService instance;
public static VoiceService GetInstance() {
if (instance == null) {
instance = new VoiceService();
}
return instance;
}
// Use this for initialization
private VoiceService () {
nluApi = new NluApiSample ();
nluApi.SetLocalization (url);
nluApi.SetAuthorization (key, secret);
speechApi = new SpeechApiSample ();
speechApi.SetLocalization (url);
speechApi.SetAuthorization (key, secret);
}
public string sendText(string text) {
return nluApi.GetRecognitionResult ("nli", text);
}
public string sendSpeech(byte[] audio) {
string result = speechApi.SendAudio("asr", "nli,seg", true, false, audio);
if (!result.ToLower ().Contains ("error")) {
System.Threading.Thread.Sleep (1000);
while (true) {
result = speechApi.GetRecognitionResult ("asr", "nli,seg");
if (!result.ToLower ().Contains ("\"final\":true")) {
if (result.ToLower ().Contains ("error"))
break;
System.Threading.Thread.Sleep (2000);
} else {
break;
}
}
}
return result;
}
}
脚本中所用到的Key和Secret就是App的Key和Secret。
olami接口支持的是wav格式的PCM录音,用WavUtility脚本来做格式转换:
using UnityEngine;
using System.Text;
using System.IO;
using System;
/// <summary>
/// WAV utility for recording and audio playback functions in Unity.
/// Version: 1.0 alpha 1
///
/// - Use "ToAudioClip" method for loading wav file / bytes.
/// Loads .wav (PCM uncompressed) files at 8,16,24 and 32 bits and converts data to Unity's AudioClip.
///
/// - Use "FromAudioClip" method for saving wav file / bytes.
/// Converts an AudioClip's float data into wav byte array at 16 bit.
/// </summary>
/// <remarks>
/// For documentation and usage examples: https://github.com/deadlyfingers/UnityWav
/// </remarks>
public class WavUtility
{
// Force save as 16-bit .wav
const int BlockSize_16Bit = 2;
/// <summary>
/// Load PCM format *.wav audio file (using Unity's Application data path) and convert to AudioClip.
/// </summary>
/// <returns>The AudioClip.</returns>
/// <param name="filePath">Local file path to .wav file</param>
public static AudioClip ToAudioClip (string filePath)
{
if (!filePath.StartsWith (Application.persistentDataPath) && !filePath.StartsWith (Application.dataPath)) {
Debug.LogWarning ("This only supports files that are stored using Unity's Application data path. \nTo load bundled resources use 'Resources.Load(\"filename\") typeof(AudioClip)' method. \nhttps://docs.unity3d.com/ScriptReference/Resources.Load.html");
return null;
}
b

最低0.47元/天 解锁文章
6202

被折叠的 条评论
为什么被折叠?



