2017-07-24 74 views
0

你好我使用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 

我想知道如何創建一個用於要想在一個腳本來處理更多的文件,所以我真的想欣賞支持,以克服這一任務。

回答

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

@ neo33,使用'fs'內置node.js模塊來處理文件系統。這裏是你需要傳遞你的響應的'fs.writeFile(fileName,data,callback)'函數。 https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback –

+0

@ neo33,檢查它 –

+0

@ neo33,哦,對不起,我的錯。以錯誤的方式編輯。但你明白了!祝你好運! –

相關問題