// retrieve example documents from 
// http://www.investegate.co.uk/ArticlePrint.aspx?id=201511201730025235G

// uk share price info from
//http://www.google.com/finance/info?q=NASDAQ%3aGOOG
//https://www.google.co.uk/finance/info?q=UKX%3aRBS

// [ { "id": "14323395" ,"t" : "RBS" ,"e" : "LON" ,"l" : "311.35" ,"l_fix" : "311.35" ,"l_cur" : "GBX311.35" ,"s": "0" ,
//"ltt":"4:50p.m. GMT" ,"lt" : "Nov 20, 4:50p.m. GMT" ,"lt_dts" : "2015-11-20T16:50:39Z" ,"c" : "-3.85" ,"c_fix" : "-3.85" ,
//"cp" : "-1.22" ,"cp_fix" : "-1.22" ,"ccol" : "chr" ,"pcls_fix" : "315.2" } ]

// 1. Test text
// 2. Obtain current share price
// 3. Obtain closing share price
// 4. Retrain text

//var readline = require('readline');

var fs = require('fs');
var natural = require('natural');

var bayes = new natural.BayesClassifier();

var filename;
var classification;
var data;

if (process.argv.length>2) {
	filename = process.argv[2];
	data = fs.readFileSync(filename,'utf8');

	natural.BayesClassifier.load('classifier.json', null, function(err, classifier) {
		bayes = classifier;
		if (process.argv.length>3) {
			classification = process.argv[3];
			bayes.addDocument(data,classification);
			bayes.train();
			bayes.save('classifier.json', function(err, bayes) {
				// the classifier is saved to the classifier.json file
			});
		}
		else {
			console.log('Classification: '+bayes.classify(data));
		}
	});
}
else {
	console.log('Usage: '+process.argv[1]+' filename [classification]');
}
@@ -27,17 +27,16 @@
 nts from
- 
 %0A// http
@@ -766,66 +766,32 @@
 );%0A%0A
-var bayes = new natural.BayesClassifier();%0A%0Avar filename;%0A
+function main(bayes) %7B%0A%09
 var 
@@ -810,19 +810,9 @@
 on;%0A
-var data;%0A%0A
+%09
 if (
@@ -835,22 +835,29 @@
 gth%3E
-2
+3
 ) %7B%0A%09
-filename
+%09classification
  = p
@@ -872,84 +872,83 @@
 rgv%5B
-2
+3
 %5D;%0A%09
-data = fs.readFileSync(filename,'utf8');%0A%0A%09natural.BayesClassifier.load
+%09bayes.addDocument(data,classification);%0A%09%09bayes.train();%0A%09%09bayes.save
 ('cl
@@ -966,14 +966,8 @@
 on',
- null,
  fun
@@ -981,46 +981,269 @@
 rr, 
-classifier) %7B%0A%09%09bayes = classifier;%0A%09%09
+bayes) %7B%0A%09%09%09// the classifier is saved to the classifier.json file%0A%09%09%7D);%0A%09%7D%0A%09else %7B%0A%09%09try %7B%0A%09%09%09classification = bayes.classify(data);%0A%09%09%09console.log('Classification: '+classification);%0A%09%09%7D%0A%09%09catch (err) %7B%0A%09%09%09console.log(err);%0A%09%09%7D%0A%09%7D%0A%7D%0A%0Avar filename;%0Avar data;%0A%0A
 if (
@@ -1266,30 +1266,22 @@
 gth%3E
-3
+2
 ) %7B%0A%09
-%09%09classification
+filename
  = p
@@ -1296,46 +1296,85 @@
 rgv%5B
-3
+2
 %5D;%0A%09
-%09%09bayes.addDocument(data,
+data = fs.readFileSync(filename,'utf8');%0A%0A%09try %7B%0A%09%09fs.statSync('
 classifi
 cati
@@ -1373,48 +1373,49 @@
 sifi
-cati
+er.js
 on
+'
 );%0A%09%09
-%09bayes.train();%0A%09%09%09bayes.save
+natural.BayesClassifier.load
 ('cl
@@ -1421,32 +1421,38 @@
 lassifier.json',
+ null,
  function(err, b
@@ -1454,162 +1454,138 @@
 rr, 
-bayes) %7B%0A%09%09%09%09// the classifier is saved to the classifier.json file%0A%09
+classifier) %7B%0A%09%09%09main(classifier);%0A
 %09%09%7D);%0A
-%09
 %09%7D%0A%09
-%09else %7B%0A%09%09%09console.log('Classification: '+b
+catch (err) %7B%0A%09%09// json file doesn't exist%0A%09%09main(new natural.B
 ayes
-.c
+C
 lassif
-y(data
+ier(
 ));%0A
-%09
 %09%7D%0A
-%09%7D);%0A
 %7D%0Ael

Fix save/load json logic