Lorca.js
Lorca is a NLP library for Spanish written in javascript. Tokenization, concordance, stemmer, statistics, sentiment analysis, readability, tf-idf and more!
Installation
Client-side
Not supported yet, but soon.
Server-side
Run:
$ npm install lorca-nlp
Start using the library like:
const lorca = ; var doc = ; doc;// [ 'esto', 'es', 'un', 'test' ]
API Concept
This library lets concatenate different methods to the document you are working on. Each time a method
is executed, its output is stored in the doc object. If you need to execute methods more than once
you have to make use of the load()
method. This will reload the original document in the doc object.
var doc = ; // This stores the concordance output in the doc object.var concordance = doc; // If you now need the words of your document, you must reload it with load().var originalWords = doc;
Text tokenization
Extract sentences, words or syllables.
var doc = ; doc;// En verano hace calor. En invierno hace frío. doc;// [ 'En verano hace calor.', ' En invierno hace frío.' ] doc;// [ 'en', 'verano', 'hace', 'calor', 'en', 'invierno', 'hace', 'frío' ] doc;// [ 'en', 've', 'ra', 'no', 'ha', 'ce', 'ca', 'lor.', 'en', 'in', 'vier', 'no', 'ha', 'ce','frí', 'o.' ] doc;// [ 'en', 'verano', 'hace', 'calor', 'invierno', 'frío' ] doc;// [ 'verano', 'calor', 'invierno', 'frío' ]
Group the output by sentence, word or both.
doc;// [ [ 'en', 'verano', 'hace', 'calor' ],// [ 'en', 'invierno', 'hace', 'frío' ] ] doc;/* [ [ [ 'en' ], [ 've', 'ra', 'no' ], [ 'ha', 'ce' ], [ 'ca', 'lor' ] ], [ [ 'en' ], [ 'in', 'vier', 'no' ], [ 'ha', 'ce' ], [ 'frí', 'o' ] ] ]*/ doc;// [ [ 'En ', 've', 'ra', 'no', ' ha', 'ce ', 'ca', 'lor.' ],// [ ' En', ' in', 'vier', 'no', ' ha', 'ce ', 'frí', 'o.' ] ] doc;/*[ [ 'en' ], [ 've', 'ra', 'no' ], [ 'ha', 'ce' ], [ 'ca', 'lor' ], [ 'en' ], [ 'in', 'vier', 'no' ], [ 'ha', 'ce' ], [ 'frí', 'o' ] ]*/
Prepositions
Extract prepositions from text, sentences or words.
doc;// [ 'en', 'en' ] doc;// [ [ 'en' ], [ 'en' ] ] doc;// [ [ 'en' ], [], [], [], [ 'en' ], [], [], [] ]
Pronouns
Extract pronouns from text, sentences or words.
var doc = ; doc;// [ 'yo', 'él', 'él' ] doc;// [ [ 'yo', 'él' ], [ 'él' ] ] doc// [ [ 'yo' ], [], [], [], [ 'él' ], [ 'él' ], [], [] ] doc;// 0.375 doc;// [ 0.4, 0.3333333333333333 ]
Adverbs
Extract adverbs from text, sentences or words.
var doc = ; doc;// [ 'realmente' ] doc;// [ [ 'realmente' ], [] ]
Passive Voice
Test whether a sentence is passive.
var doc = ;doc;// true var doc = ;doc;// [ true, false ]
Concordance
Get the word frequency of a document. The concordance method accepts the mode 'relative'
which outputs the relative frequency of the words. It is posible to sort the output by frequency and to shorten the output array with the method sort()
.
var doc = ; doc;// [ en: 2, verano: 1, hace: 2, calor: 1, invierno: 1, 'frío': 1 ] doc;// [ en: 2, hace: 2, verano: 1, calor: 1, invierno: 1, 'frío': 1 ] doc;// [ en: 0.25, hace: 0.25, verano: 0.125 calor: 0.125, invierno: 0.125, 'frío': 0.125 ] doc;// [ en: 2, hace: 2, verano: 1 ] // Get the relative frequency from a huge corpus given by RAE.doc;// [ 0.02775516,// 0.00008453,// 0.00075057,// 0.00007254,// 0.02775516,// 0.00004106,// 0.00075057,// 0.00007199 ] doc;// [ [ 0.02775516, 0.00008453, 0.00075057, 0.00007254 ],// [ 0.02775516, 0.00004106, 0.00075057, 0.00007199 ] ]
Statistics
Get basic statistics of a text.
var doc = ; doclength;// 8 doclength;// 2 doc;// 4 doc// 2 doc// 8 doc;// 0.75 doc;// 0.5 doc;// 0.25 doc;// [ 0.25, 0.25 ]
Readability
IFSZ Index
doc;// 78 doc;// Bastante fácil
Sentiment
AFINN
Disclaimer: It uses a semi-automated translation of the original AFINN list. The list only contains words that are inside the 10.000 most used words. It has a total of 885 words. The sentiment()
method calculates the relative value of each sentence and then it returns the relative values of those sentences.
Positive values mean a positive sentiment and negative values mean negative sentiment.
The sentiment algorithm stems the text input in order to reach a wider spectrum of words.
var doc = ; doc;// -0.75 var doc = ; doc;// 0.5 var doc = ; doc;// [ 0.6, 0.5, -1.2 ] doc;// [ 0, 0, 0, 0, 3, 0, 2, 0, 0, 0, -1, 0, 0, 5 ] doc// [ [ 0, 0, 0, 0, 3 ], [ 0, 2, 0, 0 ], [ 0, -1, 0, 0, 5 ] ] doc;// -0.03333333333333329
ML-SENTICON
This method uses a powerful lemma-level lexicon created in 2014 by Fermín L. Cruz, José A. Troyano, Beatriz Pontes and F. Javier Ortega.
var doc = ; doc;// -0.09375
Stemmer
Get the stem of any word in Spanish. The stemmer is based on the Porter algorithm. You can also get the stemmed concordance of your document.
doc;// recomend var doc = ;doc;// [ 'los', 'niñ', 'jueg', 'con', 'las', 'pelot' ] var doc = ;doc;// [ [ 'los', 'niñ', 'jueg', 'con', 'las', 'pelot' ],// [ 'los', 'profesor', 'habl', 'del', 'tiemp' ] ] var doc = ;doc;// { 'niñ': 2, los: 1, jueg: 1, con: 1, las: 1 }
tf-idf
The tf-idf has batteries included. It leverages a big frequency list to get the main words of any text.
var doc = ; doc;// { verano: 4117.747963402167,// invierno: 2660.600722826124,// frío: 2192.690048750771,// calor: 2186.3476209394835,// gusta: 2068.4919519114765,// hace: 478.20393517589827,// me: -748.0756841870855,// el: -2833.037926222993,// en: -5539.036281420974 } doc;// { verano: 4117.747963402167,// invierno: 2660.600722826124 }
Reading Time
Get the reading time of a text in seconds. You can pass a reading speed as
an argument in the readingtime(400)
in words per minute. If no reading
speed is given, it will use default value of 220 wpm.
var doc = ; doc;// 2.72
Search
Search any word in the text. You can use Regex too.
var doc = ; doc;// [ 'verano' ] doc;// [ [ 'verano' ], [] ]
Testing
$ npm test
Thanks to
Vic - for his Spanish hyphenator in javascript. Cruz, Fermín L., José A. Troyano, Beatriz Pontes, F. Javier Ortega. Building layered, multilingual sentiment lexicons at synset and lemma levels, Expert Systems with Applications, 2014. For their senticon list in sentiment analysis.