-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
113 lines (105 loc) · 3.66 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//グローバル変数宣言
var flag_speech = 0;
var input_text;
var answer_text;
var flag_context = 0;
//入力フキダシ生成
function input_balloon(){
var target = document.getElementById("wrapper");
var newTag = document.createElement("p");
newTag.setAttribute("class", "input_comment");
newTag.innerHTML = input_text;
target.appendChild(newTag);
}
//応答フキダシ生成
function answer_balloon(){
var target = document.getElementById("wrapper");
var newTag = document.createElement("p");
var imgTag = document.createElement("img");
newTag.setAttribute("class", "answer_comment");
imgTag.setAttribute("src", "icon.png");
imgTag.setAttribute("width", "100");
newTag.innerHTML = answer_text;
target.appendChild(imgTag);
target.appendChild(newTag);
}
//音声認識メイン
function vr_function() {
window.SpeechRecognition = window.SpeechRecognition || webkitSpeechRecognition;
var recognition = new webkitSpeechRecognition();
recognition.lang = 'ja';
recognition.interimResults = true;
recognition.continuous = true;
//エラー;
recognition.onerror = function () {
if (flag_speech == 0) vr_function();
};
//認識中断
recognition.onsoundend = function () {
console.log("停止中");
vr_function();
};
//認識成功
recognition.onresult = function (event) {
var results = event.results;
for (var i = event.resultIndex; i < results.length; i++) {
if (results[i].isFinal) {
//認識結果はグローバルで宣言
rtnString = results[i][0].transcript;
//認識結果コンソール表示
console.log("【"+rtnString+"】");
input_text=rtnString;
input_balloon();
if (flag_context == 1){
setTimeout(function(){
//応答語句を設定
answer_text="そんなことよりアナタのお名前教えてください";
//1000ミリ秒遅延して応答フキダシを表示
answer_balloon();},1000);
//音声認識再開
flag_context = 2;
vr_function();
} else if(flag_context == 2){
setTimeout(function(){
answer_text="「"+rtnString+"」さんですね!";
answer_balloon();},1000);
flag_context = 3;
vr_function();
} else if (~rtnString.indexOf("お疲れ様")) {
//応答動作
setTimeout(function(){
//応答語句を設定
answer_text="はい。お疲れ様です!";
//1000ミリ秒遅延して応答フキダシを表示
answer_balloon();},1000);
//音声認識再開
vr_function();
} else if (~rtnString.indexOf("こんにちは")) {
setTimeout(function(){
answer_text="はい。こんにちは!";
answer_balloon();},1000);
vr_function();
} else if (~rtnString.indexOf("名前は") && flag_context < 2) {
setTimeout(function(){
answer_text="私は、ミライと言います!";
answer_balloon();},1000);
flag_context = 1;
vr_function();
}else {
setTimeout(function(){
answer_text="「"+rtnString+"」って言いました?";
answer_balloon();},1000);
vr_function();
}
} else {
//認識過程をコンソール表示
console.log("[認識中] " + results[i][0].transcript);
flag_speech = 1;
}
}
}
//音声認識開始
flag_speech = 0;
console.log("認識開始");
recognition.start();
}