compromise
Version:
natural language processing in the browser
83 lines (73 loc) • 1.94 kB
JavaScript
;
const fixContraction = require('./fix');
//these are always contractions
// const blacklist = {
// 'it\'s': true,
// 'that\'s': 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;
// }
// "'s" may be a contraction or a possessive
// 'spencer's house' vs 'spencer's good'
const isPossessive = (ts, i) => {
let t = ts.terms[i];
let next_t = ts.terms[i + 1];
//a pronoun can't be possessive - "he's house"
if (t.tag.Pronoun || t.tag.QuestionWord) {
return false;
}
//if end of sentence, it is possessive - "was spencer's"
if (!next_t) {
return true;
}
//a gerund suggests 'is walking'
if (next_t.tag.VerbPhrase) {
return false;
}
//spencer's house
if (next_t.tag.Noun) {
return true;
}
//rocket's red glare
if (next_t.tag.Adjective && ts.terms[i + 2] && ts.terms[i + 2].tag.Noun) {
return true;
}
//an adjective suggests 'is good'
if (next_t.tag.Adjective || next_t.tag.Adverb || next_t.tag.Verb) {
return false;
}
return false;
};
//handle ambigous contraction "'s"
const hardOne = (ts) => {
for(let i = 0; i < ts.terms.length; i++) {
//skip existing
if (ts.terms[i].silent_term) {
continue;
}
let parts = ts.terms[i].term.contraction();
if (parts) {
//have we found a hard one
if (parts.end === 's') {
//spencer's house
if (isPossessive(ts, i)) {
ts.terms[i].tagAs('#Possessive', 'hard-contraction');
// console.log('==possessive==');
continue;
}
//is vs was
let arr = [parts.start, 'is'];
ts = fixContraction(ts, arr, i);
i += 1;
}
}
}
return ts;
};
module.exports = hardOne;