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 suported yet, but soon.
Server-side
Run:
$ npm install lorca-nlp
Start using the library like:
const lorca = require('lorca-nlp');
var doc = lorca('esto es un test');
doc.words().get();
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 = lorca('En verano hace calor. En invierno hace frío');
var concordance = doc.concordance().sort().get();
var originalWords = doc.load().words().get();
Text tokenization
Extract sentences, words or syllables.
var doc = lorca('En verano hace calor. En invierno hace frío');
doc.get();
doc.sentences().get();
doc.words().get();
doc.syllables().get();
doc.uniqueWords().get();
doc.onceWords().get();
Group the output by sentence, word or both.
doc.sentences().words().get();
doc.sentences().words().syllables().get();
doc.sentences().syllables().get();
doc.words().syllables().get();
Prepositions
Extract prepositions from text, sentences or words.
doc.prepositions().get();
doc.sentences().prepositions().get();
doc.words().prepositions().get();
Pronouns
Extract pronouns from text, sentences or words.
var doc = lorca('Yo le canto a él. Él se rie.');
doc.pronouns().get();
doc.sentences().pronouns().get();
doc.words().pronouns().get()
doc.pronouns().percentage().get();
doc.sentences().pronouns().percentage().get();
Adverbs
Extract adverbs from text, sentences or words.
var doc = lorca('En verano hace realmente calor. En invierno hace frío');
doc.adverbs().get();
doc.sentences().adverbs().get();
Pasive Voice
Test whether a sentence is passive.
var doc = lorca('El niño ha sido castigado.');
doc.isPassive().get();
var doc = lorca('El niño ha sido castigado. La madre lo ha castigado.');
doc.sentences().isPassive().get();
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 = lorca('En verano hace calor. En invierno hace frío');
doc.concordance().get();
doc.concordance().sort().get();
doc.concordance('relative').sort().get();
doc.concordance().sort(3).get();
doc.words().corpusFrequency().get();
doc.sentences().words().corpusFrequency().get();
Statistics
Get basic statistics of a text.
var doc = lorca('En verano hace calor. En invierno hace frío');
doc.words().get().length;
doc.sentences().get().length;
doc.wordsPerSentence().get();
doc.syllablesPerWord().get()
doc.syllablesPerSentence().get()
doc.uniqueWords().percentage().get();
doc.onceWords().percentage().get();
doc.prepositions().percentage().get();
doc.sentences().prepositions().percentage().get();
Readability
IFSZ Index
doc.ifsz().get();
doc.ifsz().grade().get();
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 sentece 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 = lorca('El plátano está malo.');
doc.sentiment();
var doc = lorca('Me gusta la navidad.');
doc.sentiment();
var doc = lorca('El plátano está muy bueno. Me gusta la navidad. Esto no ha sido magnífico.');
doc.sentences().sentiment();
doc.words().sentiment();
doc.sentences().words().sentiment()
doc.sentiment();
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 = lorca('El plátano está malo.');
doc.sentiment('senticon');
Stemmer
Get the stem of any word in Spanish. The stemmer is based on the Porter algorithm.
You can also get the stemed concordance of your document.
doc.stem('recomendaciones');
var doc = lorca('Los niños juegan con las pelotas');
doc.words().stem().get();
var doc = lorca('Los niños juegan con las pelotas. Los profesores hablan del tiempo.');
doc.sentences().words().stem().get();
var doc = lorca('Los niños juegan con las niñas.');
doc.words().stem().concordance().sort().get();
tf-idf
The tf-idf has batteries included. It leverages a big frequency list to get the main words of any text.
var doc = lorca('En verano hace calor. En invierno hace frío. El verano me gusta');
doc.tfidf().sort().get();
doc.tfidf().sort(2).get();
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 = lorca('El niño ha sido castigado. La madre lo ha castigado.');
doc.readingTime();
Search
Search any word in the text. You can use Regex too.
var doc = lorca('En verano hace calor. En invierno hace frío');
doc.find("verano");
doc.sentences().find("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.