| 41 | | * Pluralize an English noun. Converts 'Category' to 'Categories'. |
| 42 | | * |
| 43 | | * @param string $word English noun to pluralize |
| 44 | | * @return string $word Plural noun |
| 45 | | */ |
| 46 | | public static function pluralize($word) |
| 47 | | { |
| 48 | | $plural = array('/(quiz)$/i' => '\1zes', |
| 49 | | '/^(ox)$/i' => '\1en', |
| 50 | | '/([m|l])ouse$/i' => '\1ice', |
| 51 | | '/(matr|vert|ind)ix|ex$/i' => '\1ices', |
| 52 | | '/(x|ch|ss|sh)$/i' => '\1es', |
| 53 | | '/([^aeiouy]|qu)ies$/i' => '\1y', |
| 54 | | '/([^aeiouy]|qu)y$/i' => '\1ies', |
| 55 | | '/(hive)$/i' => '\1s', |
| 56 | | '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves', |
| 57 | | '/sis$/i' => 'ses', |
| 58 | | '/([ti])um$/i' => '\1a', |
| 59 | | '/(buffal|tomat)o$/i' => '\1oes', |
| 60 | | '/(bu)s$/i' => '\1ses', |
| 61 | | '/(alias|status)/i' => '\1es', |
| 62 | | '/(octop|vir)us$/i' => '\1i', |
| 63 | | '/(ax|test)is$/i' => '\1es', |
| 64 | | '/s$/i' => 's', |
| 65 | | '/$/' => 's'); |
| 66 | | |
| 67 | | $uncountable = array('equipment', |
| 68 | | 'information', |
| 69 | | 'rice', |
| 70 | | 'money', |
| 71 | | 'species', |
| 72 | | 'series', |
| 73 | | 'fish', |
| 74 | | 'sheep'); |
| 75 | | |
| 76 | | $irregular = array('person' => 'people', |
| 77 | | 'man' => 'men', |
| 78 | | 'child' => 'children', |
| 79 | | 'sex' => 'sexes', |
| 80 | | 'move' => 'moves'); |
| 81 | | |
| 82 | | $lowercasedWord = strtolower($word); |
| 83 | | |
| 84 | | foreach ($uncountable as $_uncountable) { |
| 85 | | if(substr($lowercasedWord, (-1 * strlen($_uncountable))) == $_uncountable) { |
| 86 | | return $word; |
| 87 | | } |
| 88 | | } |
| 89 | | |
| 90 | | foreach ($irregular as $_plural=> $_singular){ |
| 91 | | if (preg_match('/('.$_plural.')$/i', $word, $arr)) { |
| 92 | | return preg_replace('/('.$_plural.')$/i', substr($arr[0],0,1) . substr($_singular,1), $word); |
| 93 | | } |
| 94 | | } |
| 95 | | |
| 96 | | foreach ($plural as $rule => $replacement) { |
| 97 | | if (preg_match($rule, $word)) { |
| 98 | | return preg_replace($rule, $replacement, $word); |
| 99 | | } |
| 100 | | } |
| 101 | | |
| 102 | | return false; |
| 103 | | } |
| 104 | | |
| 105 | | /** |
| 106 | | * Singularize an English noun. Converts 'Quizzes' to 'Quiz'. |
| 107 | | * |
| 108 | | * @param string $word English noun to singularize |
| 109 | | * @return string $word Singular noun. |
| 110 | | */ |
| 111 | | public static function singularize($word) |
| 112 | | { |
| 113 | | $singular = array('/(quiz)zes$/i' => '\\1', |
| 114 | | '/(matr)ices$/i' => '\\1ix', |
| 115 | | '/(vert|ind)ices$/i' => '\\1ex', |
| 116 | | '/^(ox)en/i' => '\\1', |
| 117 | | '/(alias|status)es$/i' => '\\1', |
| 118 | | '/([octop|vir])i$/i' => '\\1us', |
| 119 | | '/(cris|ax|test)es$/i' => '\\1is', |
| 120 | | '/(shoe)s$/i' => '\\1', |
| 121 | | '/(o)es$/i' => '\\1', |
| 122 | | '/(bus)es$/i' => '\\1', |
| 123 | | '/([m|l])ice$/i' => '\\1ouse', |
| 124 | | '/(x|ch|ss|sh)es$/i' => '\\1', |
| 125 | | '/(m)ovies$/i' => '\\1ovie', |
| 126 | | '/(s)eries$/i' => '\\1eries', |
| 127 | | '/([^aeiouy]|qu)ies$/i' => '\\1y', |
| 128 | | '/([lr])ves$/i' => '\\1f', |
| 129 | | '/(tive)s$/i' => '\\1', |
| 130 | | '/(hive)s$/i' => '\\1', |
| 131 | | '/([^f])ves$/i' => '\\1fe', |
| 132 | | '/(^analy)ses$/i' => '\\1sis', |
| 133 | | '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\\1\\2sis', |
| 134 | | '/([ti])a$/i' => '\\1um', |
| 135 | | '/(n)ews$/i' => '\\1ews', |
| 136 | | '/^(.{2,2})$/i' => '\\1', |
| 137 | | '/s$/i' => ''); |
| 138 | | |
| 139 | | $uncountable = array('equipment', |
| 140 | | 'information', |
| 141 | | 'rice', |
| 142 | | 'money', |
| 143 | | 'species', |
| 144 | | 'series', |
| 145 | | 'fish', |
| 146 | | 'sheep', |
| 147 | | 'sms', |
| 148 | | 'status', |
| 149 | | 'access'); |
| 150 | | |
| 151 | | $irregular = array('person' => 'people', |
| 152 | | 'man' => 'men', |
| 153 | | 'child' => 'children', |
| 154 | | 'sex' => 'sexes', |
| 155 | | 'move' => 'moves'); |
| 156 | | |
| 157 | | $lowercasedWord = strtolower($word); |
| 158 | | foreach ($uncountable as $_uncountable){ |
| 159 | | if(substr($lowercasedWord, ( -1 * strlen($_uncountable))) == $_uncountable){ |
| 160 | | return $word; |
| 161 | | } |
| 162 | | } |
| 163 | | |
| 164 | | foreach ($irregular as $_singular => $_plural) { |
| 165 | | if (preg_match('/('.$_plural.')$/i', $word, $arr)) { |
| 166 | | return preg_replace('/('.$_plural.')$/i', substr($arr[0],0,1).substr($_singular,1), $word); |
| 167 | | } |
| 168 | | } |
| 169 | | |
| 170 | | foreach ($singular as $rule => $replacement) { |
| 171 | | if (preg_match($rule, $word)) { |
| 172 | | return preg_replace($rule, $replacement, $word); |
| 173 | | } |
| 174 | | } |
| 175 | | |
| 176 | | return $word; |
| 177 | | } |
| 178 | | |
| 179 | | /** |