This demo takes an article from English Wikipedia and converts it into a timeline. It does this by using the entity recognition in winkNLP. For all the DATE
s that it finds it looks for the shapes that can be understood by the JavaScript Date object.
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 = "She was born in 1869. She died in 1940."
var doc = nlp.readDoc(text);
var timeline = [];
doc
.entities()
.filter((e) => {
var shapes = e.tokens().out(its.shape);
// We only want dates that can be converted to an actual
// time using new Date()
return (
e.out(its.type) === 'DATE' &&
(
shapes[0] === 'dddd' ||
( shapes[0] === 'Xxxxx' && shapes[1] === 'dddd' ) ||
( shapes[0] === 'Xxxx' && shapes[1] === 'dddd' ) ||
( shapes[0] === 'dd' && shapes[1] === 'Xxxxx' && shapes[2] === 'dddd' ) ||
( shapes[0] === 'dd' && shapes[1] === 'Xxxx' && shapes[2] === 'dddd' ) ||
( shapes[0] === 'd' && shapes[1] === 'Xxxxx' && shapes[2] === 'dddd' ) ||
( shapes[0] === 'd' && shapes[1] === 'Xxxx' && shapes[2] === 'dddd' )
)
);
})
.each((e) => {
e.markup();
timeline.push({
date: e.out(),
unixTime: new Date(e.out()).getTime() / 1000,
sentence: e.parentSentence().out(its.markedUpText)
})
});
timeline.sort((a, b) => {
return a.unixTime - b.unixTime;
})
console.log(timeline);