<commit_before><?php

/*
 * This file is part of the JpnForPhp package.
 *
 * (c) <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace JpnForPhp\Converter;

/**
 * Provides useful methods for Japanese units conversion.
 *
 * @author <NAME>
 */
class Converter
{

    /**
     * Converts a number in Arabic/Western format into Japanese numeral.
     *
     * @param integer $number The input number.
     *
     * @return string The Japanese numeral.
     */
    public static function toJapaneseNumeral($number) 
    {
        // Mapping between digits and kanjis
        $mapDigits = array(
            0 => '〇',
            1 => '一',
            2 => '二',
            3 => '三',
            4 => '四',
            5 => '五',
            6 => '六',
            7 => '七',
            8 => '八',
            9 => '九'
        );
        
        // Mapping powers of ten and kanjis
        $mapPowersOfTen = array(
            1 => '十',
            2 => '百',
            3 => '千',
            4 => '万',
            8 => '億',
            12 => '兆',
            16 => '京'
        );
        
        // Before doing need ensure that an integer value has been given
        if(!preg_match('/^[1-9]?[0-9]*$/', $number)) {
            throw new Exception('Wrong input value');
        }
        // ... and force type to integer
        $number = intval($number);
        
        // Easy with digits...
        if($number < 10) {
            return $mapDigits[$number];
        }
        
        // Split number by group of 4 digits (!! starting from the end)
        $chunks = self::rstr_split($number, 4);
        
        $res = '';
        for ($i = count($chunks) - 1; $i >= 0; $i--) {
            $chunk = '';
            for ($j = 1; $j <= strlen($chunks[$i]); $j++) {
                
                $d = substr($chunks[$i], $j * -1, 1);

                // 0 (ie: zero) is not use for number superior to 0.
                if(!$d) {
                    continue;
                }
                
                $d = intval($d);
                if($d > 1) {
                    $chunk = $mapDigits[$d] . $mapPowersOfTen[$j-1] . $chunk;
                }
                else { // ie: $d == 1
                    $chunk = $mapPowersOfTen[$j-1] . $chunk;
                }
            }
            $res .= $chunk . $mapPowersOfTen[$i*4];
        }

        return $res;
    }
    
    /**
     * Reverse version of str_split.
     * @see str_split()
     */
    private static function rstr_split($string, $split_length) {
        $splits = array();
        $tmp = '';
        for ($i = 1; $i <= strlen($string); $i++) {<commit_msg>Fix undefined offset on<commit_after><?php

/*
 * This file is part of the JpnForPhp package.
 *
 * (c) <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace JpnForPhp\Converter;

/**
 * Provides useful methods for Japanese units conversion.
 *
 * @author <NAME>
 */
class Converter
{

    /**
     * Converts a number in Arabic/Western format into Japanese numeral.
     *
     * @param integer $number The input number.
     *
     * @return string The Japanese numeral.
     */
    public static function toJapaneseNumeral($number) 
    {
        // Mapping between digits and kanjis
        $mapDigits = array(
            0 => '〇',
            1 => '一',
            2 => '二',
            3 => '三',
            4 => '四',
            5 => '五',
            6 => '六',
            7 => '七',
            8 => '八',
            9 => '九'
        );
        
        // Mapping powers of ten and kanjis
        $mapPowersOfTen = array(
            1 => '十',
            2 => '百',
            3 => '千',
            4 => '万',
            8 => '億',
            12 => '兆',
            16 => '京'
        );
        
        // Before doing need ensure that an integer value has been given
        if(!preg_match('/^[1-9]?[0-9]*$/', $number)) {
            throw new Exception('Wrong input value');
        }
        // ... and force type to integer
        $number = intval($number);
        
        // Easy with digits...
        if($number < 10) {
            return $mapDigits[$number];
        }
        
        // Split number by group of 4 digits (!! starting from the end)
        $chunks = self::rstr_split($number, 4);
        
        $res = '';
        for ($i = count($chunks) - 1; $i >= 0; $i--) {
            $chunk = '';
            for ($j = 1; $j <= strlen($chunks[$i]); $j++) {
                
                $d = substr($chunks[$i], $j * -1, 1);

                // 0 (ie: zero) is not use for number superior to 0.
                if(!$d) {
                    continue;
                }
                
                $d = intval($d);
                if($d > 1) {
                    $chunk = $mapDigits[$d] . (isset($mapPowersOfTen[$j - 1]) ? $mapPowersOfTen[$j - 1] : '') . $chunk;
                }
                else { // ie: $d == 1
                    $chunk = (isset($mapPowersOfTen[$j - 1]) ? $mapPowersOfTen[$j - 1] : '') . $chunk;
                }
            }
            $res .= $chunk . $mapPowersOfTen[$i * 4];
        }

        return $res;
    }
    
    /**
     * Reverse version of str_split.
     * @see str_split()
     */
    private static function rstr_split($string, $split_length) {
        $splits = array();
        $tmp = '';
        for ($i = 1; $i <= strlen($string); $i++) {
{'fraction_non_alphanumeric': 0.1500539762504498, 'fraction_numerical': 0.014753508456279237, 'mean_word_length': 1.2433414043583535, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4610415', 'n_tokens_mistral': 1747, 'n_tokens_neox': 1663, 'n_words': 480}