This demo takes text and annotates it in real-time:
- It tags all the Part of Speech tags and Entities in the text using the
markup
method. - It shows over-all statistics like number of tokens, words and sentences. To calculate the number of words it
filter
s out all the tokens that haveits.type
asword
. - For sentiment analysis it uses the
its.sentiment
helper. - Using the
as
helper it generates a table of all the words and their frequency of occurence
const winkNLP = require('wink-nlp');
const its = require( 'wink-nlp/src/its.js' );
const as = require( 'wink-nlp/src/as.js' );
const model = require('wink-eng-lite-model');
const nlp = winkNLP(model);
var text = `Yesterday at 3am I was surfing http://twitter.com. I won a 100$ lottery for the first time. I spent 100% of it in just 1 hour :P Can you imagine that π
? #yolo`;
var doc = nlp.readDoc(text);
// Entities
var entities = doc.entities().out(its.detail);
// Counts
var sentences = doc.sentences().length();
var tokens = doc.tokens().length();
var words = doc.tokens().filter( (token) => {
return token.out(its.type) === 'word'
} ).length();
// Tagged text
var seenEntities = new Set();
doc.tokens().each( (token) => {
var entity = token.parentEntity();
if (entity === undefined) {
if (token.out(its.type) === 'word') {
token.markup('<span class=\"tag '+ token.out(its.pos) +'\">','</span>');
}
} else {
if (!seenEntities.has(entity.index())) {
entity.markup('<span class=\"tag '+ entity.out(its.type) +'\">', "</span>");
}
seenEntities.add(entity.index());
}
} )
// Word frequency
var wordFreq = doc.tokens().filter((token) => {
return token.out(its.type) === 'word' && !token.out(its.stopWordFlag);
}).out(its.normal, as.freqTable);
wordFreq = wordFreq.slice(0, 5)
// Sentiment
var sentiments = [];
doc.sentences().each((s) => {
sentiments.push({
sentence: s.out(),
sentiment: s.out(its.sentiment)
})
})
console.log(entities)
console.log(sentiments);
console.log(wordFreq);
console.log(doc.out(its.markedUpText));