使用Cloud Speech API和DialogFlow进行流式识别标点错误

时间:2019-02-02 22:04:12

标签: node.js dialogflow google-cloud-speech

我已经创建了具有流语音识别功能的NodeJS DialogFlow客户端。这对我来说一直很好,除了Cloud Speech API每当我说“句号”时都会错误地打断我的请求。例如,我会问一个类似的问题:"when does first period end",它将更正为"when does first. End",这可能很难解决。

有没有办法告诉Cloud Speech API停止转换它,还是有办法在将请求发送到DialogFlow之前探索替代方案?

我禁用了enableAutomaticPunctuation,但是当我规定标点符号(例如“逗号”或“句号”)时,它仍在添加标点符号。

这是我的DialogFlow客户端代码:

const projectId = 'foo';
const sessionId = 'bar'

const languageCode = 'en-US';

const pump = require('pump');
const through2 = require('through2')

const record = require('node-record-lpcm16');

// Instantiate a DialogFlow client.
const dialogflow = require('dialogflow');
const sessionClient = new dialogflow.SessionsClient();

// The encoding of the audio file, e.g. 'AUDIO_ENCODING_LINEAR16'
const encoding = 'AUDIO_ENCODING_LINEAR16';

// The sample rate of the audio file in hertz, e.g. 16000
const sampleRateHertz = 16000;

// The BCP-47 language code to use, e.g. 'en-US'

let sessionPath = sessionClient.sessionPath(projectId, sessionId);

const initialStreamRequest = {
  session: sessionPath,
  queryParams: {
    session: sessionClient.sessionPath(projectId, sessionId),
  },
  queryInput: {
    audioConfig: {
      audioEncoding: encoding,
      maxAlternatives: 4,
      sampleRateHertz: sampleRateHertz,
      languageCode: languageCode,
    },
    singleUtterance: true,
  },
};

// Create a stream for the streaming request.
const detectStream = sessionClient
  .streamingDetectIntent()
  .on('error', console.error)
  .on('data', data => {
    if (data.recognitionResult) {

      console.log(data);

      if(data.recognitionResult.isFinal) {

        record.stop();

      }

    } else {

      logQueryResult(sessionClient, data.queryResult);

    }
  });

// Write the initial stream request to config for audio input.
detectStream.write(initialStreamRequest);

const recordingAudio = record
  .start({
    sampleRateHertz: sampleRateHertz,
    threshold: 0,
    // Other options, see https://www.npmjs.com/package/node-record-lpcm16#options
    verbose: false,
    recordProgram: 'rec', // Try also "arecord" or "sox"
    silence: '10.0'
  }).on('error', console.error);

//Streams the microphone to it
pump(
  recordingAudio,
  // Format the audio stream into the request format.
  through2.obj((obj, _, next) => {
    next(null, {inputAudio: obj});
  }),
  detectStream
);

function logQueryResult(sessionClient, result) {
  // Imports the Dialogflow library
  const dialogflow = require('dialogflow');

  // Instantiates a context client
  const contextClient = new dialogflow.ContextsClient();

  if (result) {

    console.log(result.Alternatives);

    console.log(`  Query: ${result.queryText}`);
    console.log(`  Response: ${result.fulfillmentText}`);
    if (result.intent) {
      console.log(`  Intent: ${result.intent.displayName}`);
    } else {
      console.log(`  No intent matched.`);
    }

  }

}

这是流语音请求的示例:

{ responseId: '',
  recognitionResult: { messageType: 'TRANSCRIPT', transcript: 'why', isFinal: false },
  queryResult: null,
  webhookStatus: null }
{ responseId: '',
  recognitionResult: { messageType: 'TRANSCRIPT', transcript: 'when', isFinal: false },
  queryResult: null,
  webhookStatus: null }
{ responseId: '',
  recognitionResult: 
   { messageType: 'TRANSCRIPT',
     transcript: 'when do',
     isFinal: false },
  queryResult: null,
  webhookStatus: null }
{ responseId: '',
  recognitionResult: 
   { messageType: 'TRANSCRIPT',
     transcript: 'when does',
     isFinal: false },
  queryResult: null,
  webhookStatus: null }
{ responseId: '',
  recognitionResult: 
   { messageType: 'TRANSCRIPT',
     transcript: 'when does fall',
     isFinal: false },
  queryResult: null,
  webhookStatus: null }
{ responseId: '',
  recognitionResult: 
   { messageType: 'TRANSCRIPT',
     transcript: 'when does for',
     isFinal: false },
  queryResult: null,
  webhookStatus: null }
{ responseId: '',
  recognitionResult: 
   { messageType: 'TRANSCRIPT',
     transcript: 'when does far',
     isFinal: false },
  queryResult: null,
  webhookStatus: null }
{ responseId: '',
  recognitionResult: 
   { messageType: 'TRANSCRIPT',
     transcript: 'when does first',
     isFinal: false },
  queryResult: null,
  webhookStatus: null }
{ responseId: '',
  recognitionResult: 
   { messageType: 'TRANSCRIPT',
     transcript: 'when does first',
     isFinal: false },
  queryResult: null,
  webhookStatus: null }
{ responseId: '',
  recognitionResult: 
   { messageType: 'TRANSCRIPT',
     transcript: 'when does first',
     isFinal: false },
  queryResult: null,
  webhookStatus: null }
{ responseId: '',
  recognitionResult: 
   { messageType: 'TRANSCRIPT',
     transcript: 'when does 1st Peter',
     isFinal: false },
  queryResult: null,
  webhookStatus: null }
{ responseId: '',
  recognitionResult: 
   { messageType: 'TRANSCRIPT',
     transcript: 'when does first.',
     isFinal: false },
  queryResult: null,
  webhookStatus: null }
{ responseId: '',
  recognitionResult: 
   { messageType: 'TRANSCRIPT',
     transcript: 'when does first. End',
     isFinal: false },
  queryResult: null,
  webhookStatus: null }
{ responseId: '',
  recognitionResult: 
   { messageType: 'TRANSCRIPT',
     transcript: 'when does first. End to',
     isFinal: false },
  queryResult: null,
  webhookStatus: null }
{ responseId: '',
  recognitionResult: 
   { messageType: 'TRANSCRIPT',
     transcript: 'when does first. End today',
     isFinal: false },
  queryResult: null,
  webhookStatus: null }
{ responseId: '',
  recognitionResult: 
   { messageType: 'TRANSCRIPT',
     transcript: 'when does first. End today',
     isFinal: false },
  queryResult: null,
  webhookStatus: null }
{ responseId: '',
  recognitionResult: 
   { messageType: 'TRANSCRIPT',
     transcript: 'when does first. End today',
     isFinal: false },
  queryResult: null,
  webhookStatus: null }
{ responseId: '',
  recognitionResult: 
   { messageType: 'TRANSCRIPT',
     transcript: 'when does first. End today',
     isFinal: true },
  queryResult: null,
  webhookStatus: null }
undefined
  Query: when does first. End today
  Response: There is no Period 1 on Saturdays.
  Intent: Bell Schedule

如何禁止发送字符串DialogFlow在此之前标点自动更正?多谢!

0 个答案:

没有答案