3 * Parse and evaluate a plural rule.
6 * http://www.unicode.org/reports/tr35/tr35-33/tr35-numbers.html#Language_Plural_Rules
8 * @author Niklas Laxström, Tim Starling
10 * @copyright Copyright © 2010-2012, Niklas Laxström
11 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 * 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 );
45 return self
::evaluateCompiled( $number, $rules );
49 * Convert a set of rules to a compiled form which is optimised for
50 * fast evaluation. The result will be an array of strings, and may be cached.
52 * @param array $rules The rules to compile
53 * @return array An array of compile rules.
55 public static function compile( array $rules ) {
56 // We can't use array_map() for this because it generates a warning if
57 // there is an exception.
58 foreach ( $rules as &$rule ) {
59 $rule = CLDRPluralRuleConverter
::convert( $rule );
65 * Evaluate a compiled set of rules returned by compile(). Do not allow
66 * the user to edit the compiled form, or else PHP errors may result.
68 * @param string $number The number to be evaluated against the rules, in English, or it
69 * may be a type convertible to string.
70 * @param array $rules The associative array of plural rules in pluralform => rule format.
71 * @return int The index of the plural form which passed the evaluation
73 public static function evaluateCompiled( $number, array $rules ) {
74 // Calculate the values of the operand symbols
75 $number = strval( $number );
76 if ( !preg_match( '/^ -? ( ([0-9]+) (?: \. ([0-9]+) )? )$/x', $number, $m ) ) {
77 wfDebug( __METHOD__
. ": invalid number input, returning 'other'\n" );
78 return count( $rules );
80 if ( !isset( $m[3] ) ) {
81 $operandSymbols = array(
82 'n' => intval( $m[1] ),
83 'i' => intval( $m[1] ),
93 $operandSymbols = array(
94 'n' => floatval( $absValStr ),
95 'i' => intval( $intStr ),
96 'v' => strlen( $fracStr ),
97 'w' => strlen( rtrim( $fracStr, '0' ) ),
98 'f' => intval( $fracStr ),
99 't' => intval( rtrim( $fracStr, '0' ) ),
103 // The compiled form is RPN, with tokens strictly delimited by
104 // spaces, so this is a simple RPN evaluator.
105 foreach ( $rules as $i => $rule ) {
109 foreach ( StringUtils
::explode( ' ', $rule ) as $token ) {
110 $ord = ord( $token );
111 if ( isset( $operandSymbols[$token] ) ) {
112 $stack[] = $operandSymbols[$token];
113 } elseif ( $ord >= $zero && $ord <= $nine ) {
114 $stack[] = intval( $token );
116 $right = array_pop( $stack );
117 $left = array_pop( $stack );
118 $result = self
::doOperation( $token, $left, $right );
126 // None of the provided rules match. The number belongs to category
127 // 'other', which comes last.
128 return count( $rules );
132 * Do a single operation
134 * @param string $token The token string
135 * @param mixed $left The left operand. If it is an object, its state may be destroyed.
136 * @param mixed $right The right operand
137 * @throws CLDRPluralRuleError
138 * @return mixed The operation result
140 private static function doOperation( $token, $left, $right ) {
141 if ( in_array( $token, array( 'in', 'not-in', 'within', 'not-within' ) ) ) {
142 if ( !( $right instanceof CLDRPluralRuleEvaluator_Range
) ) {
143 $right = new CLDRPluralRuleEvaluator_Range( $right );
148 return $left ||
$right;
150 return $left && $right;
152 return $left == $right;
154 return $left != $right;
156 return $right->isNumberIn( $left );
158 return !$right->isNumberIn( $left );
160 return $right->isNumberWithin( $left );
162 return !$right->isNumberWithin( $left );
164 if ( is_int( $left ) ) {
165 return (int)fmod( $left, $right );
167 return fmod( $left, $right );
169 if ( $left instanceof CLDRPluralRuleEvaluator_Range
) {
172 $range = new CLDRPluralRuleEvaluator_Range( $left );
174 $range->add( $right );
177 return new CLDRPluralRuleEvaluator_Range( $left, $right );
179 throw new CLDRPluralRuleError( "Invalid RPN token" );