Home > front end >  How to dynamically add items to a function that takes in a list of objects
How to dynamically add items to a function that takes in a list of objects

Time:12-18

so I'm using the 'limdu' ML library (https://www.npmjs.com/package/limdu). In it, I am trying to use the .trainBatch() function. Train batch takes in a list of objects, with each object having an input (being another object) and an output, being an array of strings. An example looks like this:

intentClassifier.trainBatch([
  { input: { I: 1, want: 1, an: 1, apple: 1 }, output: ["WANT", "APPLE"] },
  { input: { I: 1, want: 1, a: 1, banana: 1 }, output: ["BANANA", "WANT"] },
  { input: { I: 1, want: 1, chips: 1 }, output: "CHIPS" },
]);

Now, I'm trying to pass in dynamic information into the trainBatch function. I have a csv file where each row has the 'input' and 'output' to be passed in. The parsing of the .csv can be seen below:

var trainingData = {}
fs.createReadStream('data.csv')
  .pipe(csv.parse({ headers: true }))
  .on('error', error => console.error(error))
  .on('data', row => {
    trainingData[row.input] = [row.output]
  })
  .on('end', rowCount => console.log(trainingData));

At this point, training data is an object where all the inputs are keys and the outputs are the values of corresponding string arrays. My question is how do I add these inputs and outputs inside of the .trainBatch() function? Any help is much appreciated, I'm really lost.

CodePudding user response:

You are building an object, when the function you are calling expects an array. Perhaps you should build an array instead, and push each row in the array? Then, on end, simply pass the constructed array to trainBatch

let trainingData = [];

fs.createReadStream('data.csv')
  .pipe(csv.parse({ headers: true }))
  .on('error', error => console.error(error))
  .on('data', row => {
    trainingData.push({input:row.input, output:row.output})
  })
  .on('end', rowCount => trainBatch(trainingData));
  • Related