Plato on Github
Report Home
node_modules\atropa-TextAnalyzer\src\atropa-TextAnalyzer.js
Maintainability
71.42
Lines of code
170
Difficulty
28.43
Estimated Errors
0.79
Function weight
By Complexity
By SLOC
/// <reference path="../../docs/vsdoc/OpenLayersAll.js"/> var atropa = require('atropa-header'); atropa.string = require('atropa-string').string; atropa.arrays = require('atropa-arrays').arrays; atropa.setAsOptionalArg = require('atropa-setAsOptionalArg').setAsOptionalArg; /*jslint indent: 4, maxerr: 50, white: true, browser: true, devel: true, plusplus: true, regexp: true, vars: true */ /*global atropa */ // end header (function () { "use strict"; atropa.requires( 'TextAnalyzer', function () { var supported = true; [ atropa.string, atropa.arrays, atropa.setAsOptionalArg ].forEach(function (prerequisite) { if(prerequisite === undefined) { supported = false; } }); return supported; } ); }()); /** * Represents a utility for analyzing text. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130311 * @class Represents a utility for analyzing text. * @param {String} text The text to analyze. * @returns {TextAnalyzer} Returns an instance of the text analyzer. * @requires atropa.string * @requires atropa.arrays * @requires atropa.setAsOptionalArg */ atropa.TextAnalyzer = function TextAnalyzer(text) { "use strict"; var that = this; var construct; /** * The supplied text. Defaults to an empty string. * @type String * @fieldOf atropa.TextAnalyzer# */ this.text = String(atropa.setAsOptionalArg('', text)); /** * Gives the count of words in the text. Defaults to 0. * @type Number * @fieldOf atropa.TextAnalyzer# */ this.wordCount = 0; /** * An array of every word in the supplied text. * Defaults to an empty array. * @type Array * @fieldOf atropa.TextAnalyzer# */ this.words = []; /** * Sets the basic properties of the text analyzer. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @private * @version 20130311 * @methodOf atropa.TextAnalyzer- */ construct = function () { atropa.supportCheck('TextAnalyzer'); that.text = atropa.string.convertEol(that.text, '\n'); that.wordCount = atropa.string.countWords(that.text); that.words = atropa.string.getWords(that.text); }; construct(); return this; }; /** * Gets an index of the text. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130118 * @methodOf atropa.TextAnalyzer# * @returns {Array} Returns an array of unique values * derived from the text given. */ atropa.TextAnalyzer.prototype.getIndex = function () { "use strict"; this.words = atropa.arrays.reindex(this.words); return atropa.arrays.getUnique(this.words); }; /** * Get the frequency data for each unique word in * the text. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130118 * @methodOf atropa.TextAnalyzer# * @returns {Object} Returns an object whose keys are * the unique words from the given text and whose * values are the count of each words occurrence. */ atropa.TextAnalyzer.prototype.getWordFrequency = function () { "use strict"; this.words = atropa.arrays.reindex(this.words); return atropa.arrays.getFrequency(this.words); }; /** * Gets phrases of the specified length from the text. * @param {Number} phraseLength The length of the phrases * to extract from the text. Defaults to 2. * @returns {Object} Returns an object whose keys are phrases * and whose values are the number of occurrences of the phrase. */ atropa.TextAnalyzer.prototype.getPhraseFrequency = function getPhraseFrequency( phraseLength ) { "use strict"; phraseLength = atropa.setAsOptionalArg(2, phraseLength); if(2 > phraseLength) { phraseLength = 2; } var counter = 0, prop, out = []; this.words = atropa.arrays.reindex(this.words); this.words.map(function(element, index, arr) { counter = 1; // element is word 1 of phraseLength // making sure there are enough words to concatenate a phrase of the // proper length. if(arr[index + phraseLength - 1] !== undefined) { prop = String(element + ' ').toLowerCase(); for(counter; counter !== phraseLength; counter++) { prop += String(arr[index + counter] + ' ').toLowerCase(); } out.push(prop.trim()); } }); out = atropa.arrays.getFrequency(out); return out; }; while(atropa.data.requirements.length > 0) { atropa.data.requirements.pop()(); } module.exports = atropa;