Fixing an issue with output parameters that are of type IntPtr
[castle.git] / Experiments / Generator / src / app / Extentions / Inflector.boo
blob36599d64156062b05080e7b427c1e7c9f9151220
1 namespace Generator.Extentions
3 import System.Collections
4 import System.Text.RegularExpressions
5 import Useful.Attributes from "Boo.Lang.Useful"
7 # Inspired by RoR's inflector
8 [Singleton]
9 class Inflector:
10 _plurals = []
11 _singulars = []
12 _uncountables = []
14 def constructor():
15 InitInflections()
17 def AddPluralInflections(inflections as IEnumerable):
18 _plurals.Extend(inflections)
20 def AddSingularInflections(inflections as IEnumerable):
21 _singulars.Extend(inflections)
23 def AddIrregularInflections(inflections as IEnumerable):
24 _plurals.Extend(inflections)
25 _singulars.Extend([Inflection(inf.Replace, inf.Pattern) for inf as Inflection in inflections])
27 def AddUncountables(words as (string)):
28 _uncountables.Extend(words)
30 def IsUncountable(word as string) as bool:
31 return _uncountables.Contains(word.ToLower())
33 def ToPlural(word as string) as string:
34 return word if IsUncountable(word)
36 for inflection as Inflection in _plurals.Reversed:
37 return inflection.Apply(word) if inflection.IsMatch(word)
38 return word
40 def ToSingular(word as string) as string:
41 return word if IsUncountable(word)
43 for inflection as Inflection in _singulars.Reversed:
44 return inflection.Apply(word) if inflection.IsMatch(word)
45 return word
47 #region Inflections definitions
48 private def InitInflections():
49 AddPluralInflections(( \
50 Inflection(@/$/, 's'),
51 Inflection(/(?i)s$/, 's'),
52 Inflection(/(?i)(ax|test)is$/, '$1es'),
53 Inflection(/(?i)(octop|vir)us$/, '$1i'),
54 Inflection(/(?i)(alias|status)$/, '$1es'),
55 Inflection(/(?i)(bu)s$/, '$1ses'),
56 Inflection(/(?i)(buffal|tomat)o$/, '$1oes'),
57 Inflection(/(?i)([ti])um$/, '$1a'),
58 Inflection(/(?i)sis$/, 'ses'),
59 Inflection(/(?i)(?:([^f])fe|([lr])f)$/, '$1$2ves'),
60 Inflection(/(?i)(hive)$/, '$1s'),
61 Inflection(/(?i)([^aeiouy]|qu)y$/, '$1ies'),
62 Inflection(/(?i)([^aeiouy]|qu)ies$/, '$1y'),
63 Inflection(/(?i)(x|ch|ss|sh)$/, '$1es'),
64 Inflection(/(?i)(matr|vert|ind)ix|ex$/, '$1ices'),
65 Inflection(/(?i)([m|l])ouse$/, '$1ice'),
66 Inflection(/(?i)^(ox)$/, '$1en'),
67 Inflection(/(?i)(quiz)$/, '$1zes')))
69 AddSingularInflections(( \
70 Inflection(/(?i)s$/, ''),
71 Inflection(/(?i)(n)ews$/, '$1ews'),
72 Inflection(/(?i)([ti])a$/, '$1um'),
73 Inflection(/(?i)((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/, '$1$2sis'),
74 Inflection(/(?i)(^analy)ses$/, '$1sis'),
75 Inflection(/(?i)([^f])ves$/, '$1fe'),
76 Inflection(/(?i)(hive)s$/, '$1'),
77 Inflection(/(?i)(tive)s$/, '$1'),
78 Inflection(/(?i)([lr])ves$/, '$1f'),
79 Inflection(/(?i)([^aeiouy]|qu)ies$/, '$1y'),
80 Inflection(/(?i)(s)eries$/, '$1eries'),
81 Inflection(/(?i)(m)ovies$/, '$1ovie'),
82 Inflection(/(?i)(x|ch|ss|sh)es$/, '$1'),
83 Inflection(/(?i)([m|l])ice$/, '$1ouse'),
84 Inflection(/(?i)(bus)es$/, '$1'),
85 Inflection(/(?i)(o)es$/, '$1'),
86 Inflection(/(?i)(shoe)s$/, '$1'),
87 Inflection(/(?i)(cris|ax|test)es$/, '$1is'),
88 Inflection(/(?i)([octop|vir])i$/, '$1us'),
89 Inflection(/(?i)(alias|status)es$/, '$1'),
90 Inflection(/(?i)^(ox)en/, '$1'),
91 Inflection(/(?i)(vert|ind)ices$/, '$1ex'),
92 Inflection(/(?i)(matr)ices$/, '$1ix'),
93 Inflection(/(?i)(quiz)zes$/, '$1')))
95 AddIrregularInflections(( \
96 Inflection('person', 'people'),
97 Inflection('man', 'men'),
98 Inflection('child', 'children'),
99 Inflection('sex', 'sexes'),
100 Inflection('move', 'moves')))
102 AddUncountables(( \
103 'equipment', 'information', 'rice', 'money',
104 'species', 'series', 'fish', 'sheep'))
105 #endregion
108 class Inflection:
109 [Property(Pattern)] _pattern as object
110 [Property(Replace)] _replace as string
112 def constructor(pattern, replace):
113 _pattern = pattern
114 _replace = replace
116 def IsMatch(str as string) as bool:
117 return string.Compare(_pattern, str, true) == 0 if (_pattern isa string)
118 return (_pattern as Regex).IsMatch(str)
120 def Apply(str as string) as string:
121 return _replace if (_pattern isa string)
122 return (_pattern as Regex).Replace(str, _replace)