Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #43 from jonpacker/master

Case preservation for irregular nouns
  • Loading branch information...
commit 2f24e69a89466619fa2a51607ec69b3ce3fcba39 2 parents 79b77b5 + 402c80b
@chrisumbel chrisumbel authored
View
27 lib/natural/inflectors/singular_plural_inflector.js
@@ -32,9 +32,10 @@ TenseInflector.prototype.addPlural = function(pattern, replacement) {
};
TenseInflector.prototype.ize = function (token, formSet, customForms) {
- return this.izeRegExps(token, customForms) || this.izeAbiguous(token) ||
+ var restoreCase = this.restoreCase(token);
+ return restoreCase(this.izeRegExps(token, customForms) || this.izeAbiguous(token) ||
this.izeRegulars(token, formSet) || this.izeRegExps(token, formSet.regularForms) ||
- token;
+ token);
}
TenseInflector.prototype.izeAbiguous = function (token) {
@@ -52,6 +53,28 @@ TenseInflector.prototype.singularize = function(token) {
return this.ize(token, this.singularForms, this.customSingularForms);
};
+var uppercaseify = function(token) {
+ return token.toUpperCase();
+}
+var capitalize = function(token) {
+ return token[0].toUpperCase() + token.slice(1);
+}
+var lowercaseify = function(token) {
+ return token.toLowerCase();
+}
+
+TenseInflector.prototype.restoreCase = function(token) {
+ if (token[0] === token[0].toUpperCase()) {
+ if (token[1] && token[1] === token[1].toLowerCase()) {
+ return capitalize;
+ } else {
+ return uppercaseify;
+ }
+ } else {
+ return lowercaseify;
+ }
+}
+
TenseInflector.prototype.izeRegulars = function(token, formSet) {
token = token.toLowerCase();
View
7 spec/noun_inflector_spec.js
@@ -153,9 +153,14 @@ describe('noun inflector', function() {
expect(inflector.pluralize('person')).toBe('people');
expect(inflector.pluralize('child')).toBe('children');
expect(inflector.pluralize('ox')).toBe('oxen');
- expect(inflector.pluralize('OX')).toBe('oxen');
});
+ it('should maintain case of irregulars', function() {
+ expect(inflector.pluralize('OX')).toBe('OXEN');
+ expect(inflector.pluralize('Person')).toBe('People');
+ expect(inflector.pluralize('child')).toBe('children');
+ });
+
it('should handle IX cases', function() {
expect(inflector.pluralize('matrix')).toBe('matrices');
expect(inflector.pluralize('index')).toBe('indices');
Please sign in to comment.
Something went wrong with that request. Please try again.