Skip to content

Instantly share code, notes, and snippets.

@Kesin11
Created August 27, 2013 07:30
Show Gist options
  • Save Kesin11/6350669 to your computer and use it in GitHub Desktop.
Save Kesin11/6350669 to your computer and use it in GitHub Desktop.
Web Speech API (Greater than Chrome 25 only)
<html>
<head>
<title>Web Speech API</title>
<meta charset="utf-8">
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
<input type="button" value="音声認識開始" onclick="startRecognition();"/>
<input type="button" value="音声認識終了" onclick="recognition.stop();"/>
連続認識<input id="continuous" type="checkbox">
中間結果の表示<input id="interim" type="checkbox">
<div id="state">停止中</div>
<div id="recognizedText"></div>
<div id="recognizedDetail"></div>
</body>
</html>
<script>
var recognition = new webkitSpeechRecognition();
recognition.lang = "ja-JP";
recognition.maxAlternatives = 10;
//認識開始+設定の変更
function startRecognition(){
//連続認識
if($("#continuous").prop("checked") == true) recognition.continuous = true;
else recognition.continuous = false;
//中間結果の表示
if($("#interim").prop("checked") == true) recognition.interimResults = true;
else recognition.interimResults = false;
recognition.start();
}
//話し声の認識中
recognition.onsoundstart = function(){
$("#state").text("認識中");
};
//マッチする認識が無い
recognition.onnomatch = function(){
$("#recognizedText").text("もう一度試してください");
};
//エラー
recognition.onerror= function(){
$("#recognizedText").text("エラー");
};
//話し声の認識終了
recognition.onsoundend = function(){
$("#state").text("停止中");
};
//認識が終了したときのイベント
recognition.onresult = function(event){
var results = event.results;
for (var i = event.resultIndex; i<results.length; i++){
//認識の最終結果
if(results[i].isFinal){
$("#recognizedText").text(results[i][0].transcript);
}
//認識の中間結果
else{
$("#recognizedText").text(results[i][0].transcript);
}
}
//トップ10の認識仮説の表示
$("#recognizedDetail").empty();
for (var i = event.resultIndex; i<results.length; i++){
if(results[i].isFinal){
for (var j = 0; j<recognition.maxAlternatives; j++){
$("#recognizedDetail").append("<p>" + "ランク" + j + " " + results[i][j].transcript +
": " + results[i][j].confidence +
"</p>");
}
}
}
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment