Skip to content

Commit

Permalink
Refactor to adhere to strict jsdoc style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 11, 2015
1 parent 435db28 commit 4afe520
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 83 deletions.
10 changes: 3 additions & 7 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

/**
/*
* Dependencies.
*/

Expand All @@ -10,7 +10,7 @@ var fixtures,
franc = require('./');
fixtures = require('./test/fixtures.json');

/**
/*
* Optional dependencies.
*/

Expand Down Expand Up @@ -53,7 +53,6 @@ if (hasException) {
* @param {string} fixture
* @return {string} - Most probable language.
*/

function guessLanguage(fixture) {
var result;

Expand All @@ -70,7 +69,6 @@ function guessLanguage(fixture) {
* @param {string} fixture
* @return {string} - Most probable language.
*/

function languageDetect(fixture) {
var result;

Expand All @@ -85,7 +83,6 @@ function languageDetect(fixture) {
* @param {string} fixture
* @return {string} - Most probable language.
*/

function vac(fixture) {
return Object.keys(Vac.detect(fixture, 1))[0];
}
Expand All @@ -95,14 +92,13 @@ function vac(fixture) {
*
* @param {function(string)} callback
*/

function eachFixture(callback) {
Object.keys(fixtures).forEach(function (language) {
callback(fixtures[language]);
});
}

/**
/*
* Get fixture count.
*/

Expand Down
16 changes: 10 additions & 6 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
'use strict';

/**
/*
* Dependencies.
*/

Expand All @@ -11,15 +11,15 @@ var franc,
pack = require('./package.json');
franc = require('./');

/**
/*
* Arguments.
*/

var argv;

argv = process.argv.slice(2);

/**
/*
* Command.
*/

Expand All @@ -28,9 +28,8 @@ var command;
command = Object.keys(pack.bin)[0];

/**
* Help.
* Log help.
*/

function help() {
console.log([
'',
Expand Down Expand Up @@ -67,14 +66,19 @@ function help() {
].join('\n ') + '\n');
}

/**
/*
* Program.
*/

var index,
blacklist,
whitelist;

/**
* Log the language for `value`.
*
* @param {string} value
*/
function detect(value) {
console.log(franc(value, {
'whitelist': whitelist,
Expand Down
38 changes: 14 additions & 24 deletions lib/franc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ var data,
utilities,
expressions;

/**
/*
* Load `trigram-utils`.
*/

utilities = require('trigram-utils');

/**
/*
* Load `expressions` (regular expressions matching
* scripts).
*/

expressions = require('./expressions.js');

/**
/*
* Load `data` (trigram information per language,
* per script).
*/

data = require('./data.json');

/**
/*
* Construct trigram dictionaries.
*/

Expand Down Expand Up @@ -59,19 +59,19 @@ var MAX_LENGTH,
MIN_LENGTH,
MAX_DIFFERENCE;

/**
/*
* Maximum sample length.
*/

MAX_LENGTH = 2048;

/**
/*
* Minimum sample length.
*/

MIN_LENGTH = 10;

/**
/*
* The maximum distance to add when a given trigram does
* not exist in a trigram dictionary.
*/
Expand All @@ -88,7 +88,6 @@ MAX_DIFFERENCE = 300;
* @param {{1: number}} a
* @param {{1: number}} b
*/

function sort(a, b) {
return a[1] - b[1];
}
Expand All @@ -107,7 +106,6 @@ function sort(a, b) {
* @return {Object.<string, Object>} - Filtered array of
* languages.
*/

function filterLanguages(languages, whitelist, blacklist) {
var filteredLanguages,
language;
Expand Down Expand Up @@ -143,7 +141,6 @@ function filterLanguages(languages, whitelist, blacklist) {
* containing weighted trigrams.
* @return {number} - The distance between the two.
*/

function getDistance(trigrams, model) {
var distance,
index,
Expand Down Expand Up @@ -183,7 +180,6 @@ function getDistance(trigrams, model) {
* @return {Array.<Array.<string, number>>} An array
* containing language--distance tuples.
*/

function getDistances(trigrams, languages, options) {
var distances,
whitelist,
Expand Down Expand Up @@ -212,7 +208,6 @@ function getDistances(trigrams, languages, options) {
* @param {RegExp} expression
* @return {number} Float between 0 and 1.
*/

function getOccurrence(value, expression) {
var count;

Expand All @@ -227,10 +222,9 @@ function getOccurrence(value, expression) {
*
* @param {string} value
* @param {Object.<string, RegExp>} scripts
* @return {{0: string, 1: number} Top script and its
* @return {Array} Top script and its
* occurrence percentage.
*/

function getTopScript(value, scripts) {
var topCount,
topScript,
Expand Down Expand Up @@ -260,7 +254,6 @@ function getTopScript(value, scripts) {
* @return {Array.<Array.<string, number>>} - Normalized
* distances.
*/

function normalize(value, distances) {
var max,
min,
Expand All @@ -285,12 +278,11 @@ function normalize(value, distances) {
* Create a single tuple as a list of tuples from a given
* language code.
*
* @param {Array.<string, number>} An single
* @param {string} language - A single
* language--distance tuple.
* @return {Array.<Array.<string, number>>} An array
* containing a single language--distance.
*/

function singleLanguageTuples(language) {
return [[language, 1]];
}
Expand All @@ -303,7 +295,6 @@ function singleLanguageTuples(language) {
* @return {Array.<Array.<string, number>>} An array
* containing language--distance tuples.
*/

function detectAll(value, options) {
var script;

Expand All @@ -313,22 +304,22 @@ function detectAll(value, options) {

value = value.substr(0, MAX_LENGTH);

/**
/*
* Get the script which characters occur the most
* in `value`.
*/

script = getTopScript(value, expressions);

/**
/*
* One languages exists for the most-used script.
*/

if (!(script[0] in data)) {
return singleLanguageTuples(script[0]);
}

/**
/*
* Get all distances for a given script, and
* normalize the distance values.
*/
Expand All @@ -344,18 +335,17 @@ function detectAll(value, options) {
* @param {string} value - The value to test.
* @return {string} The most probable language.
*/

function detect(value, options) {
return detectAll(value, options)[0][0];
}

/**
/*
* Expose `detectAll` on `detect`.
*/

detect.all = detectAll;

/**
/*
* Expose `detect`.
*/

Expand Down
10 changes: 5 additions & 5 deletions script/build-fixtures.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

/**
/*
* Dependencies.
*/

Expand All @@ -12,7 +12,7 @@ support = require('../data/support');
customFixtures = require('../data/custom-fixtures');
udhr = require('udhr').json();

/**
/*
* The minimum number of speakers to be included in
* `franc`: 1,000,000.
*/
Expand Down Expand Up @@ -42,7 +42,7 @@ if (THRESHOLD < 1e5) {
);
}

/**
/*
* Get fixtures from UDHR preambles and notes.
*/

Expand Down Expand Up @@ -81,15 +81,15 @@ support.forEach(function (language) {
data.push(fixture);
});

/**
/*
* Add a fixture for `und`: Undetermined languages.
*/

data.und = '';

data = JSON.stringify(data, 0, 2);

/**
/*
* Write.
*/

Expand Down
Loading

0 comments on commit 4afe520

Please sign in to comment.