Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Sep 18, 2016
1 parent 4e8e9e4 commit 8889496
Show file tree
Hide file tree
Showing 3 changed files with 273 additions and 0 deletions.
137 changes: 137 additions & 0 deletions lib/PluralRulesTokenizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ICanBoogie\CLDR;

class PluralRulesTokenizer
{
private $data;

public function __construct(array $data)
{
$this->data = $data;
}

static public function examples_for($locale)
{

}

static public function tokenize_witness($data)
{
$testData = [];
foreach ($data as $l => $lData) {
$testData[$l] = array();
$keys = array_keys($lData);
foreach ($keys as $key) {
if (!preg_match('/^pluralRule-count-(.+)$/', $key, $m)) {
throw new \Exception("Invalid node '$key' in ".$dstFile);
}
$rule = $m[1];
$testData[$l][$rule] = array();
$vOriginal = $lData[$key];
$examples = explode('@', $vOriginal);
$v = trim(array_shift($examples));

// var_dump($examples); exit;

foreach ($examples as $example) {

// var_dump($examples); exit;

list($exampleNumberType, $exampleValues) = explode(' ', $example, 2);
switch ($exampleNumberType) {
case 'integer':
case 'decimal':
$exampleValues = preg_replace('/, …$/', '', $exampleValues);
$exampleValuesParsed = array();
foreach (explode(', ', trim($exampleValues)) as $ev) {
if (preg_match('/^[+\-]?\d+$/', $ev)) {
$exampleValuesParsed[] = $ev;
$exampleValuesParsed[] = intval($ev);
} elseif (preg_match('/^[+\-]?\d+\.\d+$/', $ev)) {
$exampleValuesParsed[] = $ev;
} elseif (preg_match('/^([+\-]?\d+)~([+\-]?\d+)$/', $ev, $m)) {
$exampleValuesParsed[] = $m[1];
$exampleValuesParsed[] = intval($m[1]);
$exampleValuesParsed[] = $m[2];
$exampleValuesParsed[] = intval($m[2]);
} elseif (preg_match('/^([+\-]?\d+(\.\d+)?)~([+\-]?\d+(\.\d+)?)$/', $ev, $m)) {
$exampleValuesParsed[] = $m[1];
$exampleValuesParsed[] = $m[3];
} elseif ($ev !== '…') {
throw new Exception("Invalid node '$key' in $dstFile: $vOriginal");
}
}
$testData[$l][$rule] = $exampleValuesParsed;
break;
default:
throw new Exception("Invalid node '$key' in $dstFile: $vOriginal");
}
}
if ($rule === 'other') {
if (strlen($v) > 0) {
throw new Exception("Invalid node '$key' in $dstFile: $vOriginal");
}
} else {
$v = str_replace(' = ', ' == ', $v);
$map = array('==' => 'true', '!=' => 'false');
foreach (array('^', ' and ', ' or ') as $pre) {
while (preg_match(
'/'.$pre.'(([nivfwft]( % \\d+)?) (==|!=) ((\\d+)(((\\.\\.)|,)+(\\d+))+))/',
$v,
$m
)) {
$found = $m[1];
$leftyPart = $m[2]; // eg 'n % 10'
$operator = $m[4]; // eg '=='
$ranges = explode(',', $m[5]);
foreach (array_keys($ranges) as $j) {
if (preg_match('/^(\\d+)\\.\\.(\\d+)$/', $ranges[$j], $m)) {
$ranges[$j] = "array({$m[1]}, {$m[2]})";
}
}
$v = str_replace($found, "static::inRange($leftyPart, {$map[$operator]}, ".implode(', ', $ranges).')', $v);
}
}
if (strpos($v, '..') !== false) {
throw new Exception("Invalid node '$key' in $dstFile: $vOriginal");
}
foreach (array(
'n' => '%1$s', // absolute value of the source number (integer and decimals).
'i' => '%2$s', // integer digits of n
'v' => '%3$s', // number of visible fraction digits in n, with trailing zeros.
'w' => '%4$s', // number of visible fraction digits in n, without trailing zeros.
'f' => '%5$s', // visible fractional digits in n, with trailing zeros.
't' => '%6$s', // visible fractional digits in n, without trailing zeros.
) as $from => $to) {
$v = preg_replace('/^'.$from.' /', "$to ", $v);
$v = preg_replace("/^$from /", "$to ", $v);
$v = str_replace(" $from ", " $to ", $v);
$v = str_replace("($from, ", "($to, ", $v);
$v = str_replace("($from ", "($to ", $v);
$v = str_replace(" $from,", " $to,", $v);
}
$v = str_replace(' % ', ' %% ', $v);
$lData[$rule] = $v;
}
unset($lData[$key]);
}
$data[$l] = $lData;
}

var_dump($data['ar'], $testData['ar']);

var_export($testData['ar']);

// saveJsonFile($testData, TESTS_DIR.DIRECTORY_SEPARATOR.basename($dstFile));
}
}
31 changes: 31 additions & 0 deletions lib/Plurals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ICanBoogie\CLDR;

use ICanBoogie\CLDR\Plurals\PluralSamples;

class Plurals extends \ArrayObject
{

public function examples_for($locale)
{
$examples = [];

foreach ($this[$locale] as $count => $rules)
{
$count = substr($count, strlen('pluralRule-count-'));
$examples[$count] = PluralSamples::from($rules);
}

var_dump($examples); exit;
}
}
105 changes: 105 additions & 0 deletions tests/PluralRulesTokenizerTe_st.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ICanBoogie\CLDR;

class PluralRulesTokenizerTest extends \PHPUnit_Framework_TestCase
{
public function test_tokenize()
{
$data = get_repository()->supplemental['plurals'];

$plurals = new Plurals($data);

$plurals->examples_for('fr');

// PluralRulesTokenizer::tokenize($data);
}

public function provide_examples()
{
return [

[ 'ar', [

'zero' => [

0 => '0.0',
1 => '0.00',
2 => '0.000',
3 => '0.0000',

],

'one' => [

0 => '1.0',
1 => '1.00',
2 => '1.000',
3 => '1.0000',
],

'two' => [

0 => '2.0',
1 => '2.00',
2 => '2.000',
3 => '2.0000',
],

'few' => [

0 => '3.0',
1 => '4.0',
2 => '5.0',
3 => '6.0',
4 => '7.0',
5 => '8.0',
6 => '9.0',
7 => '10.0',
8 => '103.0',
9 => '1003.0',

],

'many' => [

0 => '11.0',
1 => '12.0',
2 => '13.0',
3 => '14.0',
4 => '15.0',
5 => '16.0',
6 => '17.0',
7 => '18.0',
8 => '111.0',
9 => '1011.0',

],

'other' => [

0 => '0.1',
1 => '0.9',
2 => '1.1',
3 => '1.7',
4 => '10.1',
5 => '100.0',
6 => '1000.0',
7 => '10000.0',
8 => '100000.0',
9 => '1000000.0',

],
] ]
];
}
}

0 comments on commit 8889496

Please sign in to comment.