source: trunk/include/inflectors/en.php @ 28532

Last change on this file since 28532 was 28319, checked in by rvelices, 10 years ago

english inflection rules ing/er

File size: 5.1 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2014 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24class Inflector_en
25{
26  private $exceptions;
27  private $pluralizers;
28  private $singularizers;
29
30  function __construct()
31  {
32    $tmp =      array ('octopus' => 'octopuses',
33      'virus' => 'viruses',
34      'person' => 'people',
35      'man' => 'men',
36      'woman' => 'women',
37      'child' => 'children',
38      'move' => 'moves',
39      'mouse' => 'mice',
40      'ox' => 'oxen',
41      'zombie' => 'zombies', // pl->sg exc.
42                        'serie' => 'series', // pl->sg exc.
43                        'movie' => 'movies', // pl->sg exc.
44    );
45
46    $this->exceptions = $tmp;
47    foreach ($tmp as $k => $v)
48      $this->exceptions[$v] = $k;
49
50    foreach ( explode(' ', 'new news advice art coal baggage butter clothing cotton currency deer energy equipment experience fish flour food furniture gas homework impatience information jeans knowledge leather love luggage money oil patience police polish progress research rice series sheep silk soap species sugar talent toothpaste travel vinegar weather wood wool work')
51      as $v)
52    {
53      $this->exceptions[$v] = 0;
54    }
55
56    $this->pluralizers = array_reverse(array( '/$/' => 's',
57      '/s$/' => 's',
58      '/^(ax|test)is$/' => '\1es',
59      '/(alias|status)$/' => '\1es',
60      '/(bu)s$/' => '\1ses',
61      '/(buffal|tomat)o$/' => '\1oes',
62      '/([ti])um$/' => '\1a',
63      '/([ti])a$/' => '\1a',
64      '/sis$/' => 'ses',
65      '/(?:([^f])fe|([lr])f)$/' => '\1\2ves',
66      '/(hive)$/' => '\1s',
67      '/([^aeiouy]|qu)y$/' => '\1ies',
68      '/(x|ch|ss|sh)$/' => '\1es',
69      '/(matr|vert|ind)(?:ix|ex)$/' => '\1ices',
70      '/(quiz)$/' => '\1zes',
71      ));
72
73    $this->singularizers = array_reverse(array(
74      '/s$/' => '',
75      '/(ss)$/' => '\1',
76      '/([ti])a$/' => '\1um',
77      '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$/' => '\1sis',
78      '/(^analy)(sis|ses)$/' => '\1sis',
79      '/([^f])ves$/' => '\1fe',
80      '/(hive)s$/' => '\1',
81      '/(tive)s$/' => '\1',
82      '/([lr])ves$/' => '\1f',
83      '/([^aeiouy]|qu)ies$/' => '\1y',
84      '/(x|ch|ss|sh)es$/' => '\1',
85      '/(bus)(es)?$/' => '\1',
86      '/(o)es$/' => '\1',
87      '/(shoe)s$/' => '\1',
88      '/(cris|test)(is|es)$/' => '\1is',
89      '/^(a)x[ie]s$/' => '\1xis',
90      '/(alias|status)(es)?$/' => '\1',
91      '/(vert|ind)ices$/' => '\1ex',
92      '/(matr)ices$/' => '\1ix',
93      '/(quiz)zes$/' => '\1',
94      '/(database)s$/' => '\1',
95      ));
96
97    $this->er2ing = array_reverse(array(
98      '/ers?$/' => 'ing',
99      '/(be|draw|liv)ers?$/' => '\0'
100    ));
101
102    $this->ing2er = array_reverse(array(
103      '/ing$/' => 'er',
104      '/(snow|rain)ing$/' => '\1',
105      '/(th|hous|dur|spr|wedd)ing$/' => '\0',
106      '/(liv|draw)ing$/' => '\0'
107    ));
108
109  }
110
111  function get_variants($word)
112  {
113    $res = array();
114
115    $lword = strtolower($word);
116
117    $rc = @$this->exceptions[$lword];
118    if ( isset($rc) )
119    {
120      if (!empty($rc))
121        $res[] = $rc;
122      return $res;
123    }
124
125    self::run($this->pluralizers, $word, $res);
126    self::run($this->singularizers, $word, $res);
127    if (strlen($word)>4)
128    {
129      self::run($this->er2ing, $word, $res);
130    }
131    if (strlen($word)>5)
132    {
133      $rc = self::run($this->ing2er, $word, $res);
134      if ($rc !== false)
135      {
136        self::run($this->pluralizers, $rc, $res);
137      }
138    }
139    return $res;
140  }
141
142  private static function run($rules, $word, &$res)
143  {
144    foreach ($rules as $rule => $replacement)
145    {
146      $rc = preg_replace($rule.'i', $replacement, $word, -1, $count);
147      if ($count)
148      {
149        if ($rc !== $word)
150        {
151          $res[] = $rc;
152          return $rc;
153        }
154        break;
155      }
156    }
157    return false;
158  }
159}
160?>
Note: See TracBrowser for help on using the repository browser.