我正在尝试在我的 React 网站中实现语音到文本识别,并且我正在使用 npm 中的
react-speech-recognition
包。我正在使用他们在此处的包描述中指定的确切代码:npm这就是我想对它说的话,这是航空术语:
Cleared to enter the CTR, not above 1500 feet, join and report on a right downwind runway 19, QNH 1018, squak 2732
这是我得到的回复:
please to enter the city are not above 15 feet heart penetrate join and report on a ride on the wind blown away 9 theme
该软件包利用浏览器的 Web 语音 API 的 语音识别接口。 React 库的 API 允许您通过调用
SpeechRecognition
方法来获取底层
getRecognition()
对象。
底层 SpeechRecognition 对象的 API 允许使用 JSpeech Grammar Format 添加语法。 这是一个例子。因此从理论上讲,您可以提供有关您希望在应用程序中听到的单词的更多信息,从而提高性能。
但也有一些注意事项,包括:
允许您创建自定义语音模型、自定义语法等。当然,它们还会根据使用情况向您收费,如果您使用自定义,它们会收取更多费用。
const {
transcript,
finalTranscript,
listening,
resetTranscript,
browserSupportsSpeechRecognition } = useSpeechRecognition({
lang: "en-IN", // Set the language to Indian English
interimResults: true, // Get partial results
continuous: true, // Enable continuous recognition
maxAlternatives: 5, // Set the number of alternative transcriptions
});
SpeechRecognition.startListening({ continuous: true, language: 'en-IN' }); // Set the language to Indian English
const {
transcript,
finalTranscript,
listening,
resetTranscript,
browserSupportsSpeechRecognition } = useSpeechRecognition({
lang: "en-IN", // Set the language to Indian English
interimResults: true, // Get partial results
continuous: true, // Enable continuous recognition
maxAlternatives: 5, // Set the number of alternative transcriptions
abortController: new AbortController(), // Create a new AbortController instance});
const [abortController, setAbortController] = useState(new
AbortController());
const handleStopRecording = () => {
stopRecording();
setAbortController(new AbortController()); // Create a new AbortController instance};
const stopRecording = () => {
setRecordingStatus("inactive");
abortController.abort(); // Abort the speech recognition
SpeechRecognition.stopListening();
// rest of the logic
}
const stopRecording = useCallback(() => {...,[abortController]}
const startRecording = useCallback(() => {...,[abortController]}
const handleStartRecording = () => {
startRecording();
setAbortController(new AbortController()); // Create a new AbortController instance
};
const handleStopRecording = () => {
stopRecording();
setAbortController(new AbortController()); // Create a new AbortController instance
};