Google语音识别器 - 定义2种可选语言

时间:2018-02-25 19:28:38

标签: javascript google-api speech-recognition

我使用Google Speech Recognizer API开发了基于JavaScript的Web应用程序。  主要语言应该是希伯来语,并且应该帮助医生编写医学诊断。 问题是,如果我说英语中的医学单词如C.T或糖尿病,用专业语言应该用英语说,它用希伯来语写英文单词。 我的问题是,如果有任何选项来定义多语言选项或定义语言优先级,当它检测到不熟悉的单词时,它将尝试第二个选择????

请帮帮我谢谢!

这是我的JavaScript代码:

var langs =
[['Afrikaans',       ['af-ZA']],
 ['Hebrew',          ['he-IL']],
 ['Bahasa Melayu',   ['ms-MY']],
 ['Català',          ['ca-ES']],
 ['Čeština',         ['cs-CZ']],
 ['Deutsch',         ['de-DE']],
 ['English',         ['en-AU', 'Australia'],
                     ['en-CA', 'Canada'],
                     ['en-IN', 'India'],
                     ['en-NZ', 'New Zealand'],
                     ['en-ZA', 'South Africa'],
                     ['en-GB', 'United Kingdom'],
                     ['en-US', 'United States']],
 ['Español',         ['es-AR', 'Argentina'],
                     ['es-BO', 'Bolivia'],
                     ['es-CL', 'Chile'],
                     ['es-CO', 'Colombia'],
                     ['es-CR', 'Costa Rica'],
                     ['es-EC', 'Ecuador'],
                     ['es-SV', 'El Salvador'],
                     ['es-ES', 'España'],
                     ['es-US', 'Estados Unidos'],
                     ['es-GT', 'Guatemala'],
                     ['es-HN', 'Honduras'],
                     ['es-MX', 'México'],
                     ['es-NI', 'Nicaragua'],
                     ['es-PA', 'Panamá'],
                     ['es-PY', 'Paraguay'],
                     ['es-PE', 'Perú'],
                     ['es-PR', 'Puerto Rico'],
                     ['es-DO', 'República Dominicana'],
                     ['es-UY', 'Uruguay'],
                     ['es-VE', 'Venezuela']],
 ['Euskara',         ['eu-ES']],
 ['Français',        ['fr-FR']],
 ['Galego',          ['gl-ES']],
 ['Hrvatski',        ['hr_HR']],
 ['IsiZulu',         ['zu-ZA']],
 ['Íslenska',        ['is-IS']],
 ['Italiano',        ['it-IT', 'Italia'],
                     ['it-CH', 'Svizzera']],
 ['Magyar',          ['hu-HU']],
 ['Nederlands',      ['nl-NL']],
 ['Norsk bokmål',    ['nb-NO']],
 ['Polski',          ['pl-PL']],
 ['Português',       ['pt-BR', 'Brasil'],
                     ['pt-PT', 'Portugal']],
 ['Română',          ['ro-RO']],
 ['Slovenčina',      ['sk-SK']],
 ['Suomi',           ['fi-FI']],
 ['Svenska',         ['sv-SE']],
 ['Türkçe',          ['tr-TR']],
 ['български',       ['bg-BG']],
 ['Pусский',         ['ru-RU']],
 ['Српски',          ['sr-RS']],
 ['한국어',            ['ko-KR']],
 ['中文',             ['cmn-Hans-CN', '普通话 (中国大陆)'],
                     ['cmn-Hans-HK', '普通话 (香港)'],
                     ['cmn-Hant-TW', '中文 (台灣)'],
                     ['yue-Hant-HK', '粵語 (香港)']],
 ['日本語',           ['ja-JP']],
 ['Lingua latīna',   ['la']]];
for (var i = 0; i < langs.length; i++) {
  select_language.options[i] = new Option(langs[i][0], i);
}
select_language.selectedIndex = 1;
updateCountry();
select_dialect.selectedIndex = 1;
showInfo('info_start');
function updateCountry() {
  for (var i = select_dialect.options.length - 1; i >= 0; i--) {
    select_dialect.remove(i);
  }
  var list = langs[select_language.selectedIndex];
  for (var i = 1; i < list.length; i++) {
    select_dialect.options.add(new Option(list[i][1], list[i][0]));
  }
  select_dialect.style.visibility = list[1].length == 1 ? 'hidden' : 'visible';
}
var create_email = false;
var final_transcript = '';
var recognizing = false;
var ignore_onend;
var start_timestamp;
if (!('webkitSpeechRecognition' in window)) {

    console.log("before upgrade")
  upgrade();
} else {
  start_button.style.display = 'inline-block';
  var recognition = new webkitSpeechRecognition();
  recognition.continuous = true;
  recognition.interimResults = true;
  recognition.onstart = function() {
    recognizing = true;
    showInfo('info_speak_now');
    console.log("webkitSpeechRecognition")
    start_img.src = '/static/Mic_MicroPhone_Mute.PNG';
    start_pic=document.getElementById("start_img")
    start_pic.style.height="59px";
    start_pic.style.width="43px";
  };
  recognition.onerror = function(event) {
    if (event.error == 'no-speech') {
        console.log("event error");
      start_img.src = '/static/mic.png';
      showInfo('info_no_speech');
      ignore_onend = true;
    }
    if (event.error == 'audio-capture') {
            console.log("event caputre");
      start_img.src = '/static/mic.png';
      showInfo('info_no_microphone');
      ignore_onend = true;
    }
    if (event.error == 'not-allowed') {
            console.log("even not allowed");
      if (event.timeStamp - start_timestamp < 100) {
        showInfo('info_blocked');
      } else {
        showInfo('info_denied');
      }
      ignore_onend = true;
    }
  };
  recognition.onend = function() {
    recognizing = false;
    if (ignore_onend) {
      return;
    }
    console.log("init mic image");
    start_img.src = '/static/mic.png';
    if (!final_transcript) {
      showInfo('info_start');
      return;
    }
    showInfo('');

//    if (window.getSelection) {
//      window.getSelection().removeAllRanges();
//      var range = document.createRange();
//      range.selectNode(document.getElementById('final_span'));
//      window.getSelection().addRange(range);
//    }
    if (create_email) {
      create_email = false;
      createEmail();
    }
  };
  recognition.onresult = function(event) {
    var interim_transcript = '';
    for (var i = event.resultIndex; i < event.results.length; ++i) {
      if (event.results[i].isFinal) {
      console.log("Final!!!!!!!!!!!!")
      console.log(final_span.value+event.results[i][0].transcript);
        final_transcript = final_span.value+event.results[i][0].transcript;

        final_span.value=final_transcript;
      } else {
        interim_transcript += event.results[i][0].transcript;
      }
    }


    final_transcript = capitalize(final_transcript);
      console.log("interim_transcript :");
    console.log(interim_transcript);
      console.log("final_transcript :");
    console.log(final_transcript);

//    final_span.value = linebreak(interim_transcript);
    if (final_transcript) {

    console.log("final transcript true");
//    final_span.value= linebreak(final_transcript);
    }
    if (final_transcript || interim_transcript) {
      showButtons('inline-block');
    }
  };
}
function upgrade() {
  start_button.style.visibility = 'hidden';
  showInfo('info_upgrade');
}
var two_line = /\n\n/g;
var one_line = /\n/g;
function linebreak(s) {
  return s.replace(two_line, '<p></p>').replace(one_line, '<br>');
}
var first_char = /\S/;
function capitalize(s) {
  return s.replace(first_char, function(m) { return m.toUpperCase(); });
}
function createEmail() {
  var n = final_transcript.indexOf('\n');
  if (n < 0 || n >= 80) {
    n = 40 + final_transcript.substring(40).indexOf(' ');
  }
  var subject = encodeURI(final_transcript.substring(0, n));
  var body = encodeURI(final_transcript.substring(n + 1));
  textarea_value=document.getElementById("final_span").value;

  window.location.href = 'mailto:?subject=' + "Puzzle-Soft Telemedicine - Speech Recognizer  " + '&body=' + textarea_value;
}
function copyButton() {
  if (recognizing) {
    recognizing = false;
    recognition.stop();
  }
  copy_button.style.display = 'none';
  copy_info.style.display = 'inline-block';
  showInfo('');
}
function emailButton() {
  if (recognizing) {
    create_email = true;
    recognizing = false;
    recognition.stop();
  } else {
    createEmail();
  }
  email_button.style.display = 'none';
  email_info.style.display = 'inline-block';
  showInfo('');
}
function startButton(event) {
  if (recognizing) {
    recognition.stop();
      console.log("StartButton if")
    return;
  }
  console.log("StartButton else")
  final_transcript = '';
  recognition.lang = select_dialect.value;
  recognition.start();
  ignore_onend = false;
//  final_span.innerHTML = '';
//  interim_span.innerHTML = '';
  start_img.src = '/static/mic.png';

  showInfo('info_allow');
  showButtons('none');
  start_timestamp = event.timeStamp;
}
function showInfo(s) {
  if (s) {
    for (var child = info.firstChild; child; child = child.nextSibling) {
      if (child.style) {
        child.style.display = child.id == s ? 'inline' : 'none';
      }
    }
    info.style.visibility = 'visible';
  } else {
    info.style.visibility = 'hidden';
  }
}
var current_style;
function showButtons(style) {
  if (style == current_style) {
    return;
  }
  current_style = style;
  copy_button.style.display = style;
  email_button.style.display = style;
  copy_info.style.display = 'none';
  email_info.style.display = 'none';
}

function ClearText() {
console.log("clear");
text_area_value=document.getElementById("final_span");
console.log(text_area_value);
console.log(text_area_value.value);
text_area_value.value='';
}
//var givevalue = function (my_key) {
//    return dict[my_key];
//
//    }
//
//js_json={'אדרנלypr': 'Olanzapine Teva', 'זיפאדהרה': 'Zypadhera', 'אומפראזול': 'Omeprazole', 'אומפרדקס': 'Omepradex', 'לוסק': 'Losec', 'אומפריקס': 'Omeprix', 'אומפרדקס Z': 'Omepradex Z', 'אומפרה': 'Omepra', 'אונדאנסטרון': 'Ondansetron', 'זופרן': 'Zofran', 'אודנטרון': 'Odnatron', 'אונדאנסטרון - פרזניוס': 'Ondansetron - Fresenius', 'אונדנסטרון אינובמד': 'Ondansetron Inovamed'}
//function replaceTextAreaDict() {
//textarea_result=document.getElementById("final_span");
//    textarea_words=textarea_result.split(' ') ;
//    for (var i = 0; i < textarea_result.length; i++) {
//
//    }
//
//
//
//    givevalue()
//
//}
//
//replaceTextAreaDict();

1 个答案:

答案 0 :(得分:0)

你不能混合语言。

语音识别大致包含3部分 - &gt;声学模型,语言模型和字典。

声学模型是数据训练包含音频信号与语音之间关系的结果

字典包含单词及其发音方式,例如,单词TOP在一般语音识别字典上发音为“T AH P”。

语言模型是用于创建句子的单词之间的连接,例如单词“I”与“am”相关联,因此语音识别器很少(或从不)给出“我是”或“我是”的结果。

每种语言都有自己的声学模型(语音),词典(单词)和语言模型(句子),所以我们可以将它们混合起来。

问题是:它还可以吗?

答案是:是的!

您可以使用许多工具构建自己的语言(在本例中为医学语言),我已经尝试过一种名为CMU Sphinx / Pocket Sphinx的工具。您可以构建自己的模型,训练它,并从中制作一本字典。这将是很多工作,但你可以配置语音识别所需的任何东西。

任何平台实施的链接:https://github.com/cmusphinx