Comparing version 0.3.0 to 0.3.1
61
index.js
/** | ||
* bigfive | ||
* v0.3.0 | ||
* v0.4.0 | ||
* | ||
@@ -33,3 +33,3 @@ * Analyse Big Five personality traits from strings. | ||
let lexicon = root.lexicon | ||
let natural = root.natural | ||
let simplengrams = root.simplengrams | ||
let tokenizer = root.tokenizer | ||
@@ -40,13 +40,19 @@ | ||
lexicon = require('./data/lexicon.json') | ||
natural = require('natural') | ||
simplengrams = require('simplengrams') | ||
tokenizer = require('happynodetokenizer') | ||
} else throw new Error('bigfive requires node modules happynodetokenizer and natural, and ./data/lexicon.json') | ||
} else throw new Error('bigfive requires happynodetokenizer and simplengrams, and ./data/lexicon.json') | ||
} | ||
// Find how many times an element appears in an array | ||
Array.prototype.indexesOf = function (el) { | ||
/** | ||
* Get the indexes of duplicate elements in an array | ||
* @function indexesOf | ||
* @param {Array} arr input array | ||
* @param {string} el element to test against | ||
* @return {Array} array of indexes | ||
*/ | ||
const indexesOf = (arr, el) => { | ||
const idxs = [] | ||
let i = this.length - 1 | ||
for (i; i >= 0; i--) { | ||
if (this[i] === el) { | ||
let i = arr.length | ||
while (i--) { | ||
if (arr[i] === el) { | ||
idxs.unshift(i) | ||
@@ -59,18 +65,13 @@ } | ||
/** | ||
* Get all the n-grams of a string and return as an array | ||
* @function getNGrams | ||
* @param {string} str input string | ||
* @param {number} n abitrary n-gram number, e.g. 2 = bigrams | ||
* @return {Array} array of ngram strings | ||
*/ | ||
const getNGrams = (str, n) => { | ||
// default to bi-grams on null n | ||
if (n == null) n = 2 | ||
if (typeof n !== 'number') n = Number(n) | ||
const ngrams = natural.NGrams.ngrams(str, n) | ||
const len = ngrams.length | ||
* Combines multidimensional array elements into strings | ||
* @function arr2string | ||
* @param {Array} arr input array | ||
* @return {Array} output array | ||
*/ | ||
const arr2string = arr => { | ||
let i = 0 | ||
const len = arr.length | ||
const result = [] | ||
let i = 0 | ||
for (i; i < len; i++) { | ||
result.push(ngrams[i].join(' ')) | ||
result.push(arr[i].join(' ')) | ||
} | ||
@@ -103,3 +104,3 @@ return result | ||
if (arr.indexOf(word) > -1) { | ||
let count = arr.indexesOf(word).length // number of times the word appears in the input text | ||
let count = indexesOf(arr, word).length // number of times the word appears in the input text | ||
match.push([word, count, weight]) | ||
@@ -146,5 +147,11 @@ } | ||
if (tokens == null) return null | ||
const bigrams = getNGrams(str, 2) | ||
const trigrams = getNGrams(str, 3) | ||
tokens = tokens.concat(bigrams, trigrams) | ||
// get n-grams | ||
const ngrams = [] | ||
ngrams.push(arr2string(simplengrams(str, 2))) | ||
ngrams.push(arr2string(simplengrams(str, 3))) | ||
const nLen = ngrams.length | ||
let i = 0 | ||
for (i; i < nLen; i++) { | ||
tokens = tokens.concat(ngrams[i]) | ||
} | ||
// get matches from array | ||
@@ -151,0 +158,0 @@ const matches = getMatches(tokens, lexicon) |
{ | ||
"name": "bigfive", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Analyse the Big Five personality traits from strings.", | ||
"main": "index.js", | ||
"dependencies": { | ||
"happynodetokenizer": "^0.1.1", | ||
"natural": "^0.5.1" | ||
"happynodetokenizer": "^0.1.2", | ||
"simplengrams": "^0.0.3" | ||
}, | ||
@@ -10,0 +10,0 @@ "keywords": [ |
48529
168
+ Addedsimplengrams@^0.0.3
+ Addedsimplengrams@0.0.3(transitive)
- Removednatural@^0.5.1
- Removedapparatus@0.0.10(transitive)
- Removedbindings@1.5.0(transitive)
- Removedfile-uri-to-path@1.0.0(transitive)
- Removednan@2.22.0(transitive)
- Removednatural@0.5.6(transitive)
- Removedsylvester@0.0.21(transitive)
- Removedunderscore@1.13.7(transitive)
- Removedwebworker-threads@0.7.17(transitive)
Updatedhappynodetokenizer@^0.1.2