natural icon indicating copy to clipboard operation
natural copied to clipboard

Error when saving classifier: "TypeError: Converting circular structure to JSON"

Open simonmesmith opened this issue 8 years ago • 3 comments

I'm working through the documentation but unable to overcome this issue.

I try this:

var natural = require('natural');
classifier = new natural.BayesClassifier();
classifier.addDocument('i am long qqqq', 'buy');
classifier.addDocument('buy the q\'s', 'buy');
classifier.addDocument('short gold', 'sell');
classifier.addDocument('sell gold', 'sell');
classifier.train();
classifier.save('classifier.json', function(err, classifier) {});

I get this error: TypeError: Converting circular structure to JSON I've investigated and it's an issue with JSON.stringify, but I cannot figure out how to overcome it.

simonmesmith avatar Apr 24 '17 17:04 simonmesmith

I also have this issue, haven't had any luck solving as of yet...

drunkplato avatar Apr 28 '17 23:04 drunkplato

For me this works in latest version

s-a avatar Jul 03 '17 20:07 s-a

I also have this issue.

I've investigated and it's an issue with JSON.stringify, but I cannot figure out how to overcome it.

Is it? The error message refers to properties in the object being bound via each other, something that is hard to resolve for a formatter.

The error refers to this code:

var data = JSON.stringify(this);

For me, the problem occurred only when I attempted to promisify the .save method, and this happens because if you do:

const { promisify } = require('util')

async function run() {
  // training code here
  const saveToJSON = promisify(classifier.save)

  await saveToJSON()
}

The value/scope of this inside of the .save() method changes.

mtimofiiv avatar Jul 21 '17 10:07 mtimofiiv