Created
October 30, 2014 19:24
-
-
Save letsgetrandy/1e05a68ea74ba6736eb5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// language exceptions | |
var exceptions = { | |
"are": "were", | |
"eat": "ate", | |
"go": "went", | |
"have": "had", | |
"inherit": "inherited", | |
"is": "was", | |
"run": "ran", | |
"sit": "sat", | |
"visit": "visited" | |
} | |
// grammatically predictable rules | |
function getPastTense(verb) { | |
if (exceptions[verb]) { | |
return exceptions[verb]; | |
} | |
if ((/e$/i).test(verb)) { | |
return verb + 'd'; | |
} | |
if ((/[aeiou]c/i).test(verb)) { | |
return verb + 'ked'; | |
} | |
// for american english only | |
if ((/el$/i).test(verb)) { | |
return verb + 'ed'; | |
} | |
if ((/[aeio][aeiou][dlmnprst]$/).test(verb)) { | |
return verb + 'ed'; | |
} | |
if ((/[aeiou][bdglmnprst]$/i).test(verb)) { | |
return verb.replace(/(.+[aeiou])([bdglmnprst])/, '$1$2$2ed'); | |
} | |
return verb + 'ed'; | |
} | |
// tests | |
var tests = { | |
"bake": "baked", | |
"smile": "smiled", | |
"free": "freed", | |
"dye": "dyed", | |
"tiptoe": "tiptoed", | |
"travel": "traveled", | |
"model": "modeled", | |
"distil": "distilled", | |
"equal": "equalled", | |
"admit": "admitted", | |
"commit": "committed", | |
"refer": "referred", | |
"inherit": "inherited", | |
"visit": "visited", | |
"stop": "stopped", | |
"tap": "tapped", | |
"sob": "sobbed", | |
"treat": "treated", | |
"wheel": "wheeled", | |
"pour": "poured", | |
"picnic": "picnicked", | |
"mimic": "mimicked", | |
"traffic": "trafficked" | |
}; | |
for (verb in tests) { | |
var past = getPastTense(verb); | |
console.log('Expect "' + verb + '" to be "' + tests[verb] + '": ' + (past === tests[verb] ? 'PASS' : 'FAIL (' + past + ')') ); | |
} |
@chadknight-wf, thanks for the heads up but unfortunately /[aeiou]c$/i also does not work because now "edited" becomes "editted". I don't know the solution but just added to the exceptions map. Thanks for the gist, small, lightweight and good enough for a small selection of words.
@letsgetrandy Great, but 'open' -> 'openned', 'listen' -> 'listenned', etc. Had to remove 'n' from /(.+[aeiou])([bdglmnprst])/
- after that works fine for my limited set of verbs.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@letsgetrandy great gist. I think you have a minor bug on one of your cases, though. L22,
(/[aeiou]c/i)
should be(/[aeiou]c$/i)
testing with "connect" -> "connectked"
thanks for putting this function out here!