Skip to content

Instantly share code, notes, and snippets.

@oguna
Created August 26, 2021 23:29
Show Gist options
  • Select an option

  • Save oguna/2b8164608c6e2c61feaad8a0594efc32 to your computer and use it in GitHub Desktop.

Select an option

Save oguna/2b8164608c6e2c61feaad8a0594efc32 to your computer and use it in GitHub Desktop.
音声認識
<template>
<h1>音声認識</h1>
<p>{{confidence.toFixed(2)}} - {{transcript}}</p>
<ol>
<li v-for="script,i in scripts" :key="i">
{{script}}
</li>
</ol>
</template>
<script lang="ts">
import {defineComponent} from 'vue'
export default defineComponent({
data() {
return {
scripts: [] as string[],
transcript: '' as string,
confidence: 0
}
},
methods: {
setupSpeechRecognition() {
const recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.lang = 'ja';
recognition.interimResults = true;
recognition.maxAlternatives = 1;
recognition.start();
// 結果の受け取りとハンドリング
recognition.onresult = (event) => {
this.transcript = event.results[0][0].transcript;
this.confidence = event.results[0][0].confidence
if (event.results[0].isFinal) {
recognition.stop();
this.scripts.push(this.transcript)
this.transcript = ''
this.setupSpeechRecognition()
}
}
recognition.onspeechend = function() {
console.log('ended')
recognition.stop();
}
// 異常系
recognition.onnomatch = function(event) {
console.log('I didnt recognise that color.')
}
recognition.onerror = function(event) {
console.log('Error occurred in recognition: ' + event.error);
}
}
},
mounted() {
this.setupSpeechRecognition()
}
})
</script>
<style scoped>
a {
color: #42b983;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment