Code coverage report for api/datum/ComputationalLinguisticsDatum.js

Statements: 96.43% (27 / 28)      Branches: 75% (6 / 8)      Functions: 100% (6 / 6)      Lines: 96.43% (27 / 28)      Ignored: none     

All files » api/datum/ » ComputationalLinguisticsDatum.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101    1                 1 20 20   20 20       1 30 30 30   30 98745   98745 5455 5455   93290     30             1             15       15   15   15     15                                               15             155     15         1  
"use strict";
 
var LanguageDatum = require("./LanguageDatum").LanguageDatum;
 
/**
 * @class The ComputationalLinguisticsDatum represents some text and statistics about that text
 *
 * @name  ComputationalLinguisticsDatum
 * @extends LanguageDatum
 * @constructs
 */
var ComputationalLinguisticsDatum = function ComputationalLinguisticsDatum(options) {
  Eif (!this._fieldDBtype) {
    this._fieldDBtype = "ComputationalLinguisticsDatum";
  }
  this.debug("Constructing ComputationalLinguisticsDatum: ", options);
  LanguageDatum.apply(this, [options]);
};
 
 
ComputationalLinguisticsDatum.naiveFrequency = function(items) {
  var naiveFrequency = {};
  var tokens = 0;
  var types = 0;
 
  items.map(function(item) {
    tokens++;
 
    if (!naiveFrequency[item]) {
      types = types + 1;
      return naiveFrequency[item] = 1;
    }
    naiveFrequency[item] = naiveFrequency[item] + 1;
  });
 
  return {
    tokens: tokens,
    types: types,
    frequencies: naiveFrequency
  };
};
 
ComputationalLinguisticsDatum.prototype = Object.create(LanguageDatum.prototype, /** @lends ComputationalLinguisticsDatum.prototype */ {
  constructor: {
    value: ComputationalLinguisticsDatum
  },
 
  extractStats: {
    value: function(options) {
      Iif (options) {
        this.warn("Options aren't used", options);
      }
 
      var text = this.utterance || this.orthography;
 
      var characters = ComputationalLinguisticsDatum
        .naiveFrequency(text.split(""));
      var words = ComputationalLinguisticsDatum
        .naiveFrequency(text.split(/\s+/));
 
      this.stats = {
        characters: {
          unigrams: characters.frequencies,
          bigrams: {}
        },
        words: {
          unigrams: words.frequencies,
          bigrams: {}
        },
        tokens: {
          characters: {
            unigrams: characters.tokens,
            bigrams: {}
          },
          words: {
            unigrams: words.tokens,
            bigrams: {}
          }
        },
        types: {
          characters: characters.types,
          words: words.types
        }
      };
      return this;
    }
  },
 
  stats: {
    get: function() {
      // this.debug("get stats", this._stats);
      return this._stats;
    },
    set: function(value) {
      this._stats = value;
    }
  }
 
});
exports.ComputationalLinguisticsDatum = ComputationalLinguisticsDatum;