Skip to content

Commit

Permalink
Really, nearly there?
Browse files Browse the repository at this point in the history
  • Loading branch information
jhugman committed Mar 31, 2015
1 parent cfd24ba commit a9bd0fe
Show file tree
Hide file tree
Showing 4 changed files with 3,487 additions and 10 deletions.
8 changes: 5 additions & 3 deletions lib/is-nonsense.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

var _ = require('underscore');

var trigrams = require('./trigrams-data'),
bigrams = require('./bigrams-data'),
onegrams = require('./1grams-data');
var ngrams = require('./new-ngrams.js');

var trigrams = ngrams.trigrams,
bigrams = ngrams.bigrams,
onegrams = ngrams.monograms;

function calculateNgramScore(string, n, regex, ngramData) {
// abcdef
Expand Down
60 changes: 60 additions & 0 deletions lib/make-new-ngrams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';

var _ = require('underscore'),
fs = require('fs');

var string = fs.readFileSync(__dirname + '/../resources/pride-and-prejudice.txt').toString();

function calculateNgrams(string, n, regex) {
var ngramData = {};
// abcdef
//
var testString = string;
for (var i=0, max = string.length - n + 1; i < max; i++) {

if (testString.match(regex)) {
var ngram = testString.substring(0, n).toLowerCase();
var ngramScore = ngramData[ngram];


if (ngramScore === undefined) {
ngramScore = 1;
} else {
ngramScore ++;
}

ngramData[ngram] = ngramScore;
}

// make the test string shorter.
testString = testString.substring(1);
}

return ngramData;
}

// var monograms = calculateNgrams(string, 1, /^[a-z ]{1}/i);
// var bigrams = calculateNgrams(string, 2, /^[a-z]{2}/i);
// var trigrams = calculateNgrams(string, 3, /^[a-z]{3}/i);
// console.log('module.exports = ');
// console.log({
// monograms: monograms,
// bigrams: bigrams,
// trigrams: trigrams,
// });
// console.log(';');

module.exports = function (string) {
var monograms = calculateNgrams(string, 1, /^[a-z ]{1}/i);

var letters = _.chain(monograms)
.keys()
.sortBy(function (letter) {
return monograms[letter];
})
.value();

console.log('monograms ' + monograms);
console.log('letters ' + letters);
return letters;
}
Loading

0 comments on commit a9bd0fe

Please sign in to comment.