compromise
Version:
natural language processing in the browser
66 lines (62 loc) • 1.71 kB
JavaScript
;
//decide if an apostrophe s is a contraction or not
// 'spencer's nice' -> 'spencer is nice'
// 'spencer's house' -> 'spencer's house'
//these are always contractions
const blacklist = {
'it\'s': true,
'that\'s': true
};
//a possessive means "'s" describes ownership, not a contraction, like 'is'
const is_possessive = function(terms, x) {
let t = terms.get(x);
//these are always contractions, not possessive
if (blacklist[t.normal]) {
return false;
}
//"spencers'" - this is always possessive - eg "flanders'"
if (t.normal.match(/[a-z]s'$/)) {
return true;
}
//if no apostrophe s, return
if (!t.normal.match(/[a-z]'s$/)) {
return false;
}
//some parts-of-speech can't be possessive
if (t.tag['Pronoun']) {
return false;
}
let nextWord = terms.get(x + 1);
//last word is possessive - "better than spencer's"
if (!nextWord) {
return true;
}
//next word is 'house'
if (nextWord.tag['Noun']) {
return true;
}
//rocket's red glare
if (nextWord.tag['Adjective'] && terms.get(x + 2) && terms.get(x + 2).tag['Noun']) {
return true;
}
//next word is an adjective
if (nextWord.tag['Adjective'] || nextWord.tag['Verb'] || nextWord.tag['Adverb']) {
return false;
}
return false;
};
//tag each term as possessive, if it should
const possessiveStep = function(terms) {
for(let i = 0; i < terms.length; i++) {
if (is_possessive(terms, i)) {
let t = terms.get(i);
//if it's not already a noun, co-erce it to one
if (!t.tag['Noun']) {
t.tagAs('Noun', 'possessive_pass');
}
t.tagAs('Possessive', 'possessive_pass');
}
}
return terms;
};
module.exports = possessiveStep;