4 * Parse and evaluate a plural rule.
7 * http://www.unicode.org/reports/tr35/tr35-33/tr35-numbers.html#Language_Plural_Rules
9 * @author Niklas Laxström, Tim Starling
11 * @copyright Copyright © 2010-2012, Niklas Laxström
12 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 * http://www.gnu.org/copyleft/gpl.html
34 class CLDRPluralRuleEvaluator
{
36 * Evaluate a number against a set of plural rules. If a rule passes,
37 * return the index of plural rule.
39 * @param int $number The number to be evaluated against the rules
40 * @param array $rules The associative array of plural rules in pluralform => rule format.
41 * @return int The index of the plural form which passed the evaluation
43 public static function evaluate( $number, array $rules ) {
44 $rules = self
::compile( $rules );
46 return self
::evaluateCompiled( $number, $rules );
50 * Convert a set of rules to a compiled form which is optimised for
51 * fast evaluation. The result will be an array of strings, and may be cached.
53 * @param array $rules The rules to compile
54 * @return array An array of compile rules.
56 public static function compile( array $rules ) {
57 // We can't use array_map() for this because it generates a warning if
58 // there is an exception.
59 foreach ( $rules as &$rule ) {
60 $rule = CLDRPluralRuleConverter
::convert( $rule );
67 * Evaluate a compiled set of rules returned by compile(). Do not allow
68 * the user to edit the compiled form, or else PHP errors may result.
70 * @param string $number The number to be evaluated against the rules, in English, or it
71 * may be a type convertible to string.
72 * @param array $rules The associative array of plural rules in pluralform => rule format.
73 * @return int The index of the plural form which passed the evaluation
75 public static function evaluateCompiled( $number, array $rules ) {
76 // Calculate the values of the operand symbols
77 $number = strval( $number );
78 if ( !preg_match( '/^ -? ( ([0-9]+) (?: \. ([0-9]+) )? )$/x', $number, $m ) ) {
79 wfDebug( __METHOD__
. ": invalid number input, returning 'other'\n" );
81 return count( $rules );
83 if ( !isset( $m[3] ) ) {
84 $operandSymbols = array(
85 'n' => intval( $m[1] ),
86 'i' => intval( $m[1] ),
96 $operandSymbols = array(
97 'n' => floatval( $absValStr ),
98 'i' => intval( $intStr ),
99 'v' => strlen( $fracStr ),
100 'w' => strlen( rtrim( $fracStr, '0' ) ),
101 'f' => intval( $fracStr ),
102 't' => intval( rtrim( $fracStr, '0' ) ),
106 // The compiled form is RPN, with tokens strictly delimited by
107 // spaces, so this is a simple RPN evaluator.
108 foreach ( $rules as $i => $rule ) {
112 foreach ( StringUtils
::explode( ' ', $rule ) as $token ) {
113 $ord = ord( $token );
114 if ( isset( $operandSymbols[$token] ) ) {
115 $stack[] = $operandSymbols[$token];
116 } elseif ( $ord >= $zero && $ord <= $nine ) {
117 $stack[] = intval( $token );
119 $right = array_pop( $stack );
120 $left = array_pop( $stack );
121 $result = self
::doOperation( $token, $left, $right );
129 // None of the provided rules match. The number belongs to category
130 // 'other', which comes last.
131 return count( $rules );
135 * Do a single operation
137 * @param string $token The token string
138 * @param mixed $left The left operand. If it is an object, its state may be destroyed.
139 * @param mixed $right The right operand
140 * @throws CLDRPluralRuleError
141 * @return mixed The operation result
143 private static function doOperation( $token, $left, $right ) {
144 if ( in_array( $token, array( 'in', 'not-in', 'within', 'not-within' ) ) ) {
145 if ( !( $right instanceof CLDRPluralRuleEvaluatorRange
) ) {
146 $right = new CLDRPluralRuleEvaluatorRange( $right );
151 return $left ||
$right;
153 return $left && $right;
155 return $left == $right;
157 return $left != $right;
159 return $right->isNumberIn( $left );
161 return !$right->isNumberIn( $left );
163 return $right->isNumberWithin( $left );
165 return !$right->isNumberWithin( $left );
167 if ( is_int( $left ) ) {
168 return (int)fmod( $left, $right );
171 return fmod( $left, $right );
173 if ( $left instanceof CLDRPluralRuleEvaluatorRange
) {
176 $range = new CLDRPluralRuleEvaluatorRange( $left );
178 $range->add( $right );
182 return new CLDRPluralRuleEvaluatorRange( $left, $right );
184 throw new CLDRPluralRuleError( "Invalid RPN token" );