如何使用一个脚本处理多个文件?

时间:2017-07-24 20:48:19

标签: node.js nuance

您好我正在使用Nuance为此进行一些关于语音的实验我正在使用节点创建请求,如下所示:

我的凭据:

var Nuance = require("nuance");
var nuance = new Nuance("mycredentials", "mypass");

然后我按如下方式发送请求:

nuance.sendDictationRequest({
    "identifier": "randomIdentifierStringHere", //The user identifier (please refer to Nuance's documentation for more info).
    "language": "es_MX", //The language code (please refer to Nuance's documentation for more info).
    "path": "12min.amr", //The path to the file you would like to send to Nuance.
    "additionalHeaders": {}, //If you'd like to supply more headers or replace the default headers, supply them here.
    "success": function(resp){ //The success callback function.
        console.log(resp);
    },
    "error": function(resp){ //The error callback function - returns the response from Nuance that you can debug.
        console.log("An error was occurred.");
        console.log(resp);
    }
});

这种方式的问题是我需要使用几个调用来改变这个部分:

"path": "12min.amr", //The path to the file you would like to send to Nuance.

由于我在终端中执行请求,如下所示:

node call12.js

然后我在终端上得到了结果。

我试过了:

nuance.sendDictationRequest({
    "identifier": "randomIdentifierStringHere", //The user identifier (please refer to Nuance's documentation for more info).
    "language": "es_MX", //The language code (please refer to Nuance's documentation for more info).
    "path": "4min.amr", //The path to the file you would like to send to Nuance.
    "additionalHeaders": {}, //If you'd like to supply more headers or replace the default headers, supply them here.
    "success": function(resp){ //The success callback function.
        console.log(resp);
    },
    "error": function(resp){ //The error callback function - returns the response from Nuance that you can debug.
        console.log("An error was occurred.");
        console.log(resp);
    }
});


nuance.sendDictationRequest({
    "identifier": "randomIdentifierStringHere", //The user identifier (please refer to Nuance's documentation for more info).
    "language": "es_MX", //The language code (please refer to Nuance's documentation for more info).
    "path": "2min.amr ", //The path to the file you would like to send to Nuance.
    "additionalHeaders": {}, //If you'd like to supply more headers or replace the default headers, supply them here.
    "success": function(resp){ //The success callback function.
        console.log(resp);
    },
    "error": function(resp){ //The error callback function - returns the response from Nuance that you can debug.
        console.log("An error was occurred.");
        console.log(resp);
    }
});

只处理2个对话我正在重复代码,因为我不相信这是最佳方式。 为了处理我的所有文件:

2min.amr 4min.amr 8min.amr

我想知道如何创建一个for以便在一个脚本中处理更多文件,所以我真的很感谢支持来克服这个任务。

1 个答案:

答案 0 :(得分:1)

const fs = require('fs');
var fileNames = ['2min.amr', '4min.amr', '8min.amr'];

fileNames.forEach((item) => {
    nuance.sendDictationRequest({
        "identifier": "randomIdentifierStringHere", //The user identifier (please refer to Nuance's documentation for more info).
        "language": "es_MX", //The language code (please refer to Nuance's documentation for more info).
        "path": item, //The path to the file you would like to send to Nuance.
        "additionalHeaders": {}, //If you'd like to supply more headers or replace the default headers, supply them here.
        "success": function(resp){ //The success callback function.
            fs.writeFile("my_text.txt", resp, err => {
                if (err) throw err;
                console.log('The file has been saved!');
            });
        },
        "error": function(resp){ //The error callback function - returns the response from Nuance that you can debug.
            console.log("An error was occurred.");
            console.log(resp);
        }
    })
});