PageRenderTime 89ms CodeModel.GetById 10ms app.highlight 53ms RepoModel.GetById 24ms app.codeStats 0ms

/Library/TMInflector+Inflections.m

http://github.com/RISCfuture/TMInflector
Objective C | 199 lines | 150 code | 49 blank | 0 comment | 0 complexity | 460d814e4be27278d08d41a6f91a8842 MD5 | raw file
  1#import "TMInflector+Inflections.h"
  2
  3@implementation TMInflector (Inflections)
  4
  5+ (void) initialize {
  6	[self addPluralRules];
  7	[self addSingularRules];
  8	[self addIrregularRules];
  9	[self addUncountables];
 10}
 11
 12+ (void) addPluralRules {
 13	RKRegex *rule;
 14	
 15	[[TMInflector inflector] addPluralRule:@"$" replacement:@"s"];
 16	
 17	rule = [[RKRegex alloc] initWithRegexString:@"s$" options:RKCompileCaseless];
 18	[[TMInflector inflector] addPluralRule:rule replacement:@"s"];
 19	[rule release];
 20	
 21	rule = [[RKRegex alloc] initWithRegexString:@"(ax|test)is$" options:RKCompileCaseless];
 22	[[TMInflector inflector] addPluralRule:rule replacement:@"$1es"];
 23	[rule release];
 24	
 25	rule = [[RKRegex alloc] initWithRegexString:@"(octop|vir)us$" options:RKCompileCaseless];
 26	[[TMInflector inflector] addPluralRule:rule replacement:@"$1i"];
 27	[rule release];
 28	
 29	rule = [[RKRegex alloc] initWithRegexString:@"(alias|status)$" options:RKCompileCaseless];
 30	[[TMInflector inflector] addPluralRule:rule replacement:@"$1es"];
 31	[rule release];
 32	
 33	rule = [[RKRegex alloc] initWithRegexString:@"(bu)s$" options:RKCompileCaseless];
 34	[[TMInflector inflector] addPluralRule:rule replacement:@"$1ses"];
 35	[rule release];
 36	
 37	rule = [[RKRegex alloc] initWithRegexString:@"(buffal|tomat)o$" options:RKCompileCaseless];
 38	[[TMInflector inflector] addPluralRule:rule replacement:@"$1oes"];
 39	[rule release];
 40	
 41	rule = [[RKRegex alloc] initWithRegexString:@"([ti])um$" options:RKCompileCaseless];
 42	[[TMInflector inflector] addPluralRule:rule replacement:@"$1a"];
 43	[rule release];
 44	
 45	rule = [[RKRegex alloc] initWithRegexString:@"sis$" options:RKCompileCaseless];
 46	[[TMInflector inflector] addPluralRule:rule replacement:@"ses"];
 47	[rule release];
 48	
 49	rule = [[RKRegex alloc] initWithRegexString:@"(?:([^f])fe|([lr])f)$" options:RKCompileCaseless];
 50	[[TMInflector inflector] addPluralRule:rule replacement:@"$1$2ves"];
 51	[rule release];
 52	
 53	rule = [[RKRegex alloc] initWithRegexString:@"(hive)$" options:RKCompileCaseless];
 54	[[TMInflector inflector] addPluralRule:rule replacement:@"$1s"];
 55	[rule release];
 56	
 57	rule = [[RKRegex alloc] initWithRegexString:@"([^aeiouy]|qu)y$" options:RKCompileCaseless];
 58	[[TMInflector inflector] addPluralRule:rule replacement:@"$1ies"];
 59	[rule release];
 60	
 61	rule = [[RKRegex alloc] initWithRegexString:@"(x|ch|ss|sh)$" options:RKCompileCaseless];
 62	[[TMInflector inflector] addPluralRule:rule replacement:@"$1es"];
 63	[rule release];
 64	
 65	rule = [[RKRegex alloc] initWithRegexString:@"(matr|vert|ind)(?:ix|ex)$" options:RKCompileCaseless];
 66	[[TMInflector inflector] addPluralRule:rule replacement:@"$1ices"];
 67	[rule release];
 68	
 69	rule = [[RKRegex alloc] initWithRegexString:@"([m|l])ouse$" options:RKCompileCaseless];
 70	[[TMInflector inflector] addPluralRule:rule replacement:@"$1ice"];
 71	[rule release];
 72	
 73	rule = [[RKRegex alloc] initWithRegexString:@"^(ox)$" options:RKCompileCaseless];
 74	[[TMInflector inflector] addPluralRule:rule replacement:@"$1en"];
 75	[rule release];
 76	
 77	rule = [[RKRegex alloc] initWithRegexString:@"(quiz)$" options:RKCompileCaseless];
 78	[[TMInflector inflector] addPluralRule:rule replacement:@"$1zes"];
 79	[rule release];
 80}
 81
 82+ (void) addSingularRules {
 83	RKRegex *rule;
 84	
 85	rule = [[RKRegex alloc] initWithRegexString:@"s$" options:RKCompileCaseless];
 86	[[TMInflector inflector] addSingularRule:rule replacement:@""];
 87	[rule release];
 88	
 89	rule = [[RKRegex alloc] initWithRegexString:@"(n)ews$" options:RKCompileCaseless];
 90	[[TMInflector inflector] addSingularRule:rule replacement:@"$1ews"];
 91	[rule release];
 92	
 93	rule = [[RKRegex alloc] initWithRegexString:@"([ti])a$" options:RKCompileCaseless];
 94	[[TMInflector inflector] addSingularRule:rule replacement:@"$1um"];
 95	[rule release];
 96	
 97	rule = [[RKRegex alloc] initWithRegexString:@"((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$" options:RKCompileCaseless];
 98	[[TMInflector inflector] addSingularRule:rule replacement:@"$1$2sis"];
 99	[rule release];
100	
101	rule = [[RKRegex alloc] initWithRegexString:@"(^analy)ses$" options:RKCompileCaseless];
102	[[TMInflector inflector] addSingularRule:rule replacement:@"$1sis"];
103	[rule release];
104	
105	rule = [[RKRegex alloc] initWithRegexString:@"([^f])ves$" options:RKCompileCaseless];
106	[[TMInflector inflector] addSingularRule:rule replacement:@"\1fe"];
107	[rule release];
108	
109	rule = [[RKRegex alloc] initWithRegexString:@"(hive)s$" options:RKCompileCaseless];
110	[[TMInflector inflector] addSingularRule:rule replacement:@"$1"];
111	[rule release];
112	
113	rule = [[RKRegex alloc] initWithRegexString:@"(tive)s$" options:RKCompileCaseless];
114	[[TMInflector inflector] addSingularRule:rule replacement:@"$1"];
115	[rule release];
116	
117	rule = [[RKRegex alloc] initWithRegexString:@"([lr])ves$" options:RKCompileCaseless];
118	[[TMInflector inflector] addSingularRule:rule replacement:@"\1f"];
119	[rule release];
120	
121	rule = [[RKRegex alloc] initWithRegexString:@"([^aeiouy]|qu)ies$" options:RKCompileCaseless];
122	[[TMInflector inflector] addSingularRule:rule replacement:@"$1y"];
123	[rule release];
124	
125	rule = [[RKRegex alloc] initWithRegexString:@"(s)eries$" options:RKCompileCaseless];
126	[[TMInflector inflector] addSingularRule:rule replacement:@"$1eries"];
127	[rule release];
128	
129	rule = [[RKRegex alloc] initWithRegexString:@"(m)ovies$" options:RKCompileCaseless];
130	[[TMInflector inflector] addSingularRule:rule replacement:@"$1ovie"];
131	[rule release];
132	
133	rule = [[RKRegex alloc] initWithRegexString:@"(x|ch|ss|sh)es$" options:RKCompileCaseless];
134	[[TMInflector inflector] addSingularRule:rule replacement:@"$1"];
135	[rule release];
136	
137	rule = [[RKRegex alloc] initWithRegexString:@"([m|l])ice$" options:RKCompileCaseless];
138	[[TMInflector inflector] addSingularRule:rule replacement:@"$1ouse"];
139	[rule release];
140	
141	rule = [[RKRegex alloc] initWithRegexString:@"(bus)es$" options:RKCompileCaseless];
142	[[TMInflector inflector] addSingularRule:rule replacement:@"$1"];
143	[rule release];
144	
145	rule = [[RKRegex alloc] initWithRegexString:@"(o)es$" options:RKCompileCaseless];
146	[[TMInflector inflector] addSingularRule:rule replacement:@"$1"];
147	[rule release];
148	
149	rule = [[RKRegex alloc] initWithRegexString:@"(shoe)s$" options:RKCompileCaseless];
150	[[TMInflector inflector] addSingularRule:rule replacement:@"$1"];
151	[rule release];
152	
153	rule = [[RKRegex alloc] initWithRegexString:@"(cris|ax|test)es$" options:RKCompileCaseless];
154	[[TMInflector inflector] addSingularRule:rule replacement:@"$1is"];
155	[rule release];
156	
157	rule = [[RKRegex alloc] initWithRegexString:@"(octop|vir)i$" options:RKCompileCaseless];
158	[[TMInflector inflector] addSingularRule:rule replacement:@"$1us"];
159	[rule release];
160	
161	rule = [[RKRegex alloc] initWithRegexString:@"(alias|status)es$" options:RKCompileCaseless];
162	[[TMInflector inflector] addSingularRule:rule replacement:@"$1"];
163	[rule release];
164	
165	rule = [[RKRegex alloc] initWithRegexString:@"^(ox)en" options:RKCompileCaseless];
166	[[TMInflector inflector] addSingularRule:rule replacement:@"$1"];
167	[rule release];
168	
169	rule = [[RKRegex alloc] initWithRegexString:@"(vert|ind)ices$" options:RKCompileCaseless];
170	[[TMInflector inflector] addSingularRule:rule replacement:@"$1ex"];
171	[rule release];
172	
173	rule = [[RKRegex alloc] initWithRegexString:@"(matr)ices$" options:RKCompileCaseless];
174	[[TMInflector inflector] addSingularRule:rule replacement:@"$1ix"];
175	[rule release];
176	
177	rule = [[RKRegex alloc] initWithRegexString:@"(quiz)zes$" options:RKCompileCaseless];
178	[[TMInflector inflector] addSingularRule:rule replacement:@"$1"];
179	[rule release];
180	
181	rule = [[RKRegex alloc] initWithRegexString:@"(database)s$" options:RKCompileCaseless];
182	[[TMInflector inflector] addSingularRule:rule replacement:@"$1"];
183	[rule release];	
184}
185
186+ (void) addIrregularRules {
187	[[TMInflector inflector] addIrregularRuleWithSingular:@"person" plural:@"people"];
188	[[TMInflector inflector] addIrregularRuleWithSingular:@"man" plural:@"men"];
189	[[TMInflector inflector] addIrregularRuleWithSingular:@"child" plural:@"children"];
190	[[TMInflector inflector] addIrregularRuleWithSingular:@"sex" plural:@"sexes"];
191	[[TMInflector inflector] addIrregularRuleWithSingular:@"move" plural:@"moves"];
192	[[TMInflector inflector] addIrregularRuleWithSingular:@"cow" plural:@"kine"];
193}
194
195+ (void) addUncountables {
196	[[TMInflector inflector] addUncountableWords:@"equipment", @"information", @"rice", @"money", @"species", @"series", @"fish", @"sheep", NULL];
197}
198
199@end