Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
How to dynamically add items to a function that takes in a list of objects

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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));
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!