首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google speech-to-text API示例代码不会运行

Google speech-to-text API示例代码不会运行
EN

Stack Overflow用户
提问于 2019-11-14 16:46:16
回答 3查看 147关注 0票数 0

我一直在不断地重读说明(设置项目,使用我的服务帐户密钥将环境变量设置为JSON文件的文件路径,安装/初始化gcloud等)。但是我根本不能运行示例代码,我也不知道为什么。示例代码如下:

代码语言:javascript
复制
// Imports the Google Cloud client library
const speech = require('@google-cloud/speech');

// Creates a client
const client = new speech.SpeechClient();

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
const gcsUri = '.resources/audio.raw';
const encoding = 'LINEAR16';
const sampleRateHertz = 16000;
const languageCode = 'en-US';

const config = {
  encoding: encoding,
  sampleRateHertz: sampleRateHertz,
  languageCode: languageCode,
};

const audio = {
  uri: gcsUri,
};

const request = {
  config: config,
  audio: audio,
};

// Detects speech in the audio file. This creates a recognition job that you
// can wait for now, or get its result later.
const [operation] = await client.longRunningRecognize(request);
// Get a Promise representation of the final result of the job
const [response] = await operation.promise();
const transcription = response.results
  .map(result => result.alternatives[0].transcript)
  .join('\n');
console.log(`Transcription: ${transcription}`);

终端显示以下错误:

代码语言:javascript
复制
const [operation] = await client.longRunningRecognize(request);
                      ^^^^^^

SyntaxError: Unexpected identifier
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)

我不明白为什么它是一个意外的标识符。不是创建了const客户端吗?

EN

回答 3

Stack Overflow用户

发布于 2019-11-14 16:54:49

await用于异步函数,让我们将您的代码放入一个异步函数中

代码语言:javascript
复制
// Imports the Google Cloud client library
const speech = require('@google-cloud/speech');

// Creates a client
const client = new speech.SpeechClient();

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
const gcsUri = '.resources/audio.raw';
const encoding = 'LINEAR16';
const sampleRateHertz = 16000;
const languageCode = 'en-US';

const config = {
  encoding: encoding,
  sampleRateHertz: sampleRateHertz,
  languageCode: languageCode,
};

const audio = {
  uri: gcsUri,
};

const request = {
  config: config,
  audio: audio,
};
async function main () {
// Detects speech in the audio file. This creates a recognition job that you
// can wait for now, or get its result later.
const [operation] = await client.longRunningRecognize(request);
// Get a Promise representation of the final result of the job
const [response] = await operation.promise();
const transcription = response.results
  .map(result => result.alternatives[0].transcript)
  .join('\n');
console.log(`Transcription: ${transcription}`);
}
main();
票数 1
EN

Stack Overflow用户

发布于 2019-11-14 16:56:54

await关键字只能在异步函数中使用。

您可以将部分代码包装在promise函数中,然后调用该promise:

代码语言:javascript
复制
const detectSpeach = async () => {
  // Detects speech in the audio file. This creates a recognition job that you
  // can wait for now, or get its result later.
  const [operation] = await client.longRunningRecognize(request);
  // Get a Promise representation of the final result of the job
  const [response] = await operation.promise();
  const transcription = response.results
    .map(result => result.alternatives[0].transcript)
    .join('\n');
  console.log(`Transcription: ${transcription}`);
};

detectSpeach();

更多信息:https://javascript.info/async-await

票数 0
EN

Stack Overflow用户

发布于 2019-11-15 09:57:43

代码语言:javascript
复制
const [operation] = await client.longRunningRecognize(request);
                  ^^^^^^
SyntaxError: Unexpected identifier

我不明白为什么它是一个意外的标识符。不是创建了const client吗?

答案是,是的,const client是初始化的,但在普通的JavaScript脚本中,await不被视为关键字。在async函数(或生成器函数)之外,await通常是一个有效的标识符,因此await后跟client的序列不是一个有效的表达式,因为client是一个意外的标识符。

要将await用作运算符,必须在async函数或生成器中进行编码。在新代码中使用await作为标识符可能是不明智的。

背景:

,否则

  • ES6 (ECMAScript 2015)在ECMAScript ES5.1的上下文中引入了await作为未来保留字,但提供了await作为保留字

我对这种拼凑混乱的理解是,awaitasync都是通过修改JavaScript解析器来检测语法错误(async functionawait identifierfor await ... of)并在某些(但不是所有)上下文中进一步处理它们来检测的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58852637

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档