Commit 4c8b58ea authored by Ludan Stoecklé's avatar Ludan Stoecklé

present

parent 24a79829
export type EnglishTense = 'PRESENT' | 'PAST' | 'FUTURE';
export type EnglishTense = 'SIMPLE_PRESENT' | 'PRESENT' | 'PAST' | 'FUTURE';
export type Numbers = 'S' | 'P';
import compromise from 'compromise';
const modals = ['can', 'could', 'may', 'might', 'must', 'shall', 'should', 'will', 'would'];
function getSimplePresent(verb: string, number: Numbers): string {
if (number === 'P') {
if (verb === 'be') {
return 'are';
} else {
return verb;
}
} else {
if (modals.indexOf(verb) > -1) {
return verb;
} else if (verb === 'have') {
return 'has';
} else if (verb === 'be') {
return 'is';
} else if (verb === 'do') {
return 'does';
} else if (verb === 'go') {
return 'goes';
} else if (verb.match(/[aeiouy]y$/)) {
// vowel + y: play -> plays
return verb + 's';
} else if (verb.endsWith('y')) {
// no vowel + y: fly -> flies
return verb.substring(0, verb.length - 1) + 'ies';
} else if (verb.endsWith('ss') || verb.endsWith('x') || verb.endsWith('sh') || verb.endsWith('ch')) {
return verb + 'es';
} else {
// default
return verb + 's';
}
}
}
export function getConjugation(verb: string, tense: EnglishTense, number: Numbers): string {
if (!verb) {
......@@ -10,27 +43,16 @@ export function getConjugation(verb: string, tense: EnglishTense, number: Number
err.message = 'verb must not be null';
throw err;
}
if (tense === 'PRESENT' && number === 'P') {
return verb;
if (number != 'S' && number != 'P') {
const err = new Error();
err.name = 'TypeError';
err.message = 'number must be S or P';
throw err;
}
const tenseMapping = {
PRESENT: 'PresentTense',
PAST: 'PastTense',
FUTURE: 'FutureTense',
};
const conjugated: any[] = compromise('he ' + verb)
.verbs()
.conjugate();
if (conjugated && conjugated.length > 0) {
return conjugated[0][tenseMapping[tense]];
if (tense === 'PRESENT' || tense === 'SIMPLE_PRESENT') {
return getSimplePresent(verb, number);
}
const err = new Error();
err.name = 'InvalidArgumentError';
err.message = `could not conjugate ${verb} properly using compromise lib`;
throw err;
return null;
}
{
"name": "english-verbs",
"version": "0.0.1",
"description": "English verbs conjugation, based on compromise lib",
"description": "English verbs conjugation",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"test": "nyc mocha",
"clean": "rm -rf dist/",
"build": "cp lib/ExoticInterface.d.ts dist && tsc"
"build": "tsc"
},
"repository": {
"type": "git",
......@@ -44,13 +44,11 @@
]
}
},
"dependencies": {
"compromise": "^13.0.0"
},
"devDependencies": {
"mocha": "^6.2.2",
"nyc": "^14.1.1",
"typescript": "^3.7.2"
"typescript": "^3.7.2",
"english-verbs-irregular": "0.0.1"
},
"keywords": [
"verbs",
......
const assert = require('assert');
const EnglishVerbs = require('../dist/index.js');
const testCases = [
['eat', 'PRESENT', 'S', 'eats'],
['eat', 'PRESENT', 'P', 'eat'],
];
const testCases = {
// SIMPLE
PRESENT: [
['eat', 'S', 'eats'],
['eat', 'P', 'eat'],
['listen', 'S', 'listens'],
// irregular
['fly', 'S', 'flies'],
['play', 'S', 'plays'],
['go', 'S', 'goes'],
['do', 'S', 'does'],
['have', 'S', 'has'],
['be', 'S', 'is'],
['be', 'P', 'are'],
['pass', 'S', 'passes'],
['catch', 'S', 'catches'],
['fix', 'S', 'fixes'],
['push', 'S', 'pushes'],
['can', 'S', 'can'],
],
SIMPLE_PRESENT: [['snow', 'S', 'snows']],
};
describe('english-verbs', function() {
describe('#getConjugation()', function() {
describe('nominal cases', function() {
for (let i = 0; i < testCases.length; i++) {
const testCase = testCases[i];
const verb = testCase[0];
const tense = testCase[1];
const number = testCase[2];
const expected = testCase[3];
it(`${verb} ${tense} ${number} => ${expected}`, function() {
assert.equal(EnglishVerbs.getConjugation(verb, tense, number), expected);
const tenses = Object.keys(testCases);
for (let i = 0; i < tenses.length; i++) {
const tense = tenses[i];
describe(tense, function() {
for (let j = 0; j < testCases[tense].length; j++) {
const testCase = testCases[tense][j];
const verb = testCase[0];
const number = testCase[1];
const expected = testCase[2];
it(`${verb} ${tense} ${number} => ${expected}`, function() {
assert.equal(EnglishVerbs.getConjugation(verb, tense, number), expected);
});
}
});
}
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment