LanguageModel.js
function(dataset) {

		// calculate counts for equation (3):
		var mapWordToTotalCount = {};
		var totalNumberOfWordsInDataset = 0;
		for (var i in dataset) {
			var datum = dataset[i];
			var totalPerDatum = 0;
			
			// for each input sentence, count the total number of words in it:
			for (var word in datum) {
				mapWordToTotalCount[word] |= 0;
				mapWordToTotalCount[word] += datum[word];
				totalPerDatum += datum[word];
			}
			datum["_total"] = totalPerDatum;
			totalNumberOfWordsInDataset += totalPerDatum;
		}
		mapWordToTotalCount["_total"] = totalNumberOfWordsInDataset;
		
		this.dataset = dataset;
		this.mapWordToTotalCount = mapWordToTotalCount;
		
		// calculate smoothing factor for equation (3):
		var mapWordToSmoothingFactor = {};
		for (var word in mapWordToTotalCount) {
			mapWordToSmoothingFactor[word] = 
				(1-this.smoothingCoefficient) * this.mapWordToTotalCount[word] / this.mapWordToTotalCount["_total"];
		}
		this.mapWordToSmoothingFactor = mapWordToSmoothingFactor;
		
		this.globalSmoothingFactor = (1/totalNumberOfWordsInDataset)  // a global smoother, for totally unseen words.
	}
function(dataset) {

		// calculate counts for equation (3):
		var mapWordToTotalCount = {};
		var totalNumberOfWordsInDataset = 0;
		for (var i in dataset) {
			var datum = dataset[i];
			var totalPerDatum = 0;
			
			// for each input sentence, count the total number of words in it:
			for (var word in datum) {
				mapWordToTotalCount[word] |= 0;
				mapWordToTotalCount[word] += datum[word];
				totalPerDatum += datum[word];
			}
			datum["_total"] = totalPerDatum;
			totalNumberOfWordsInDataset += totalPerDatum;
		}
		mapWordToTotalCount["_total"] = totalNumberOfWordsInDataset;
		
		this.dataset = dataset;
		this.mapWordToTotalCount = mapWordToTotalCount;
		
		// calculate smoothing factor for equation (3):
		var mapWordToSmoothingFactor = {};
		for (var word in mapWordToTotalCount) {
			mapWordToSmoothingFactor[word] = 
				(1-this.smoothingCoefficient) * this.mapWordToTotalCount[word] / this.mapWordToTotalCount["_total"];
		}
		this.mapWordToSmoothingFactor = mapWordToSmoothingFactor;
		
		this.globalSmoothingFactor = (1/totalNumberOfWordsInDataset)  // a global smoother, for totally unseen words.
	}
function ( dataset ) { // calculate counts for equation (3): var mapWordToTotalCount = { } ; var totalNumberOfWordsInDataset = 0 ; for ( var i in dataset ) { var datum = dataset [ i ] ; var totalPerDatum = 0 ; // for each input sentence, count the total number of words in it: for ( var word in datum ) { mapWordToTotalCount [ word ] |= 0 ; mapWordToTotalCount [ word ] += datum [ word ] ; totalPerDatum += datum [ word ] ; } datum [ "_total" ] = totalPerDatum ; totalNumberOfWordsInDataset += totalPerDatum ; } mapWordToTotalCount [ "_total" ] = totalNumberOfWordsInDataset ; this . dataset = dataset ; this . mapWordToTotalCount = mapWordToTotalCount ; // calculate smoothing factor for equation (3): var mapWordToSmoothingFactor = { } ; for ( var word in mapWordToTotalCount ) { mapWordToSmoothingFactor [ word ] = ( 1 - this . smoothingCoefficient ) * this . mapWordToTotalCount [ word ] / this . mapWordToTotalCount [ "_total" ] ; } this . mapWordToSmoothingFactor = mapWordToSmoothingFactor ; this . globalSmoothingFactor = ( 1 / totalNumberOfWordsInDataset ) // a global smoother, for totally unseen words. }
Train the language with all the given documents.

@param dataset
an array with hashes of the format:
{word1:count1, word2:count2,...}
each object represents the a sentence (it should be tokenized in advance).
Train the language with all the given documents .