compromise
Version:
natural language processing in the browser
32 lines (28 loc) • 903 B
JavaScript
;
const irregulars = require('../../paths').data.irregular_plurals.toSingle;
const singleRules = require('./data/singleRules');
//turn 'shoes' into 'shoe'
const toSingle = function (str) {
//irregular
if (irregulars[str]) {
return irregulars[str];
}
//inflect first word of preposition-phrase
if (str.match(/([a-z]*) (of|in|by|for) [a-z]/)) {
const first = (str.match(/^([a-z]*) (of|in|by|for) [a-z]/) || [])[1];
if (first) {
const better_first = toSingle(first); //recursive
return better_first + str.replace(first, '');
}
}
//regular rule-based inflector
for (let i = 0; i < singleRules.length; i++) {
if (str.match(singleRules[i].reg)) {
return str.replace(singleRules[i].reg, singleRules[i].repl);
}
}
return null;
};
// console.log(toSingle('gases') === 'gas')
module.exports = toSingle;
// console.log(toSingle('days'))