Lorca.js
Lorca is a NLP library for Spanish written in javascript. Tokenization,
concordance, stemmer, statistics, sentiment analysis, readability 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();
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.
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();
Stemmer (Beta)
Get the stem of any word in Spanish. This stemmer is based in Porter algorithm and
still need improvement.
doc.stem('recomendaciones');
var doc = lorca('Los niños juegan con las pelotas');
doc.words().stem();
var doc = lorca('Los niños juegan con las pelotas. Los profesores hablan del tiempo.');
doc.sentences().words().stem();
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