Search code examples
node.jsnuance

How to process several files with one script?


Hello I am using Nuance to make some experiments about speech to text for this I am using node to create the request as follows:

My credentials:

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

Then I send the request as follows:

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);
    }
});

The problem for this way is that I need to use several calls changing this part:

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

Since I execute the request in terminal as follows:

node call12.js

And then I get the result on the terminal.

I tried:

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);
    }
});

to process only 2 conversations I am repeating code since I don't believe that this is the optimal way. In order to process all of my files:

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

I would like to know how to create a for in order to process more files in one script, so I really would like to appreciate support to overcome this task.


Solution

  • 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);
            }
        })
    });