1 /**
2 	* English inflections
3 	*
4 	* Copyright: © 2016 David Monagle
5 	* License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
6 	* Authors: David Monagle
7 */
8 module inflections.en;
9 
10 import inflections.Inflector;
11 
12 class Inflector_EN : Inflector {
13 	static this() {
14 		plural!(`([^aeiouy])y$`, `$1ies`, "i");
15 		plural!(`([^aeiouy]o)$`, `$1es`, "i");
16 		plural!(`([sxz]|[cs]h)$`, `$1es`, "i");
17 		plural!(`(el)f$`, `$1ves`, "i");
18 		plural!(`$`, `s`);
19 		
20 		singular!(`([^aeiouy])ies$`, `$1y`, "i");
21 		singular!(`([^aeiouy]o)es$`, `$1`, "");
22 		singular!(`([sxz]|[cs]h)es$`, `$1`, "");
23 		singular!(`(el)ves$`, `$1f`, "i");
24 		singular!(`(ss)$`, `$1`, "i");
25 		singular!(`s$`, ``, "i");
26 		
27 		irregular("person", "people");
28 		irregular("child", "children");
29 		
30 		uncountable("series");
31 	}
32 }
33 
34 unittest {
35 	void inflectorTest(string singular, string plural = "") {
36 		if (!plural.length) plural = singular;
37 		
38 		auto p = Inflector_EN.pluralize(singular);
39 		assert(p == plural, "Pluralization of '" ~ singular ~ "' yielded '" ~ p ~ "' rather than the expected '" ~ plural ~ "'");
40 		
41 		auto s = Inflector_EN.singularize(plural);
42 		assert(s == singular, "Singularization of '" ~ plural ~ "' yielded '" ~ s ~ "' rather than the expected '" ~ singular ~ "'");
43 	}
44 	
45 	inflectorTest("apple", "apples");
46 	inflectorTest("shelf", "shelves");
47 	inflectorTest("dress", "dresses");
48 	
49 	// Irregulars
50 	inflectorTest("person", "people");
51 	
52 	// Uncountable
53 	inflectorTest("series");
54 }
55 
56 string pluralize(const string input) {
57 	return Inflector_EN.pluralize(input);
58 }
59 
60 string singularize(const string input) {
61 	return Inflector_EN.singularize(input);
62 }
63 
64 unittest {
65 	assert("apple".pluralize == "apples");
66 	assert("apples".singularize == "apple");
67 }