Skip to content

Commit

Permalink
Updated comments and Readme.md.
Browse files Browse the repository at this point in the history
Added test2.js for random seed check.
  • Loading branch information
primaryobjects committed Jul 28, 2016
1 parent 7a79b75 commit c71dba8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
9 changes: 9 additions & 0 deletions Readme.md
Expand Up @@ -106,6 +106,15 @@ exports.stop_words = [
];
```

## Setting a Random Seed

A specific random seed can be used to compute the same terms and probabilities during subsequent runs. You can specify the random seed, as follows:

```javascript
// Use the random seed 123.
result = lda(documents, 2, 5, null, null, null, 123);
```

## Author

Kory Becker
Expand Down
4 changes: 2 additions & 2 deletions lib/lda.js
Expand Up @@ -276,12 +276,12 @@ var lda = new function() {
}

this.getRandom = function() {
// generage a pseudo-random number using a seed to ensure reproducable results.
if (this.RANDOM_SEED) {
// generate a pseudo-random number using a seed to ensure reproducable results.
var x = Math.sin(this.RANDOM_SEED++) * 1000000;
return x - Math.floor(x);
// use standard random algorithm.
} else {
// use standard random algorithm.
return Math.random();
}
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "lda",
"version": "0.1.7",
"version": "0.1.8",
"description": "LDA topic modeling for node.js.",
"author": {
"name": "Kory Becker",
Expand Down Expand Up @@ -42,6 +42,6 @@
"document",
"words"
],
"_id": "lda@0.1.7",
"_id": "lda@0.1.8",
"_from": "lda"
}
52 changes: 52 additions & 0 deletions test2.js
@@ -0,0 +1,52 @@
var lda = require('./lib/lda');

var text = 'Cats are small. Dogs are big. Cats like to chase mice. Dogs like to eat bones.';
var documents = text.match( /[^\.!\?]+[\.!\?]+/g );
var seed = 123;

// Test using random seed for reproducible results.
var isOk = true;
var results = [];
for (var i = 0; i < 10; i++) {
results.push(lda(documents, 3, 6, null, null, null, 123));
}

// Compare each result in the set to ensure matching terms and probability values.
for (var i in results) {
var result = results[i];

for (var j in result) {
var row1 = result[j];

for (var k in row1) {
var term1 = row1[k];

// Compare this term1 to the term1 in all other results.
for (var l in results) {
var result2 = results[l];

// Take same index row as first result.
var row2 = result2[j];
var term2 = row2[k];

//console.log(term1.term + ' (' + term1.probability + ') ' + term2.term + ' (' + term2.probability + ')');

if (term1.term != term2.term || term1.probability != term2.probability) {
console.log('Failed match! Term ' + term1.term + ' (' + term1.probability + ') != ' + term2.term + ' (' + term2.probability + ')');
isOk = false;
}
}
}
}
}

if ((results[0][0][0].probability != 0.18 || results[0][0][1].probability != 0.15 || results[0][0][2].probability != 0.12) ||
(results[0][1][0].probability != 0.2 || results[0][1][1].probability != 0.15 || results[0][1][2].probability != 0.12))
{
console.log('Failed expected values for random seed ' + seed + '.')
isOk = false;
}

if (isOk) {
console.log('\nRandom seed (' + seed + ') OK.');
}

0 comments on commit c71dba8

Please sign in to comment.