<commit_before><?php

/**
 * Chinese to pinyin translator
 *
 * @author Joy <<EMAIL>>
 *
 * @example
 * <pre>
 *      echo Pinyin::trans('带着希望去旅行，比到达终点更美好'), "\n";
 *      //output: "dài zhe xī wàng qù lǔ xíng bǐ dào dá zhōng diǎn gèng měi hǎo"
 * </pre>
 */
class Pinyin
{   

    /**
     * dictionary path
     *
     * @var string
     */
    protected $dictionary;

    /**
     * settings
     *
     * @var array
     */
    protected static $setting = array(
                                 'delimiter' => ' ',
                                 'accent'    => true,
                                );

    /**
     * instance
     *
     * @var Pinyin
     */
    protected static $instance;

    /**
     * constructor
     *
     * set dictionary path.
     */
    public function __construct()
    {
        $this->dictionary = __DIR__ . '/cedict/cedict_ts.u8';
    }

    /**
     * set the dictionary.
     *
     * @param array $setting settings.
     */
    public static function set(array $setting = array())
    {
        self::$setting = array_merge(self::$setting, $setting);
    }

    /**
     * get Pinyin instance
     *
     * @return Pinyin
     */
    public static function getInstance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new self;
        }

        return self::$instance;
    }

    /**
     * chinese to pinyin
     *
     * @param string $string  source string.
     * @param array  $setting settings.
     *
     * @return string
     */
    public static function trans($string, array $setting = array())
    {
        $instance = self::getInstance();

        // merge setting
        empty($setting) || self::set($setting);

        $dictionary = $instance->loadDictionary();
            
        // do replace
        foreach ($dictionary as $line) {
            $string = str_replace($line['simplified'], "{$line['pinyin_marks']} ", $string);
            if (!$instance->containsChinese($string)) {
                break;
            }
        }

        // add accents
        if(self::$setting['accent']) {
            $string = $instance->pinyin_addaccents(strtolower($string));
        } else {
            $string = $instance->removeTone($string);
        }

        // clean the string
        $string = $instance->removeUnwantedCharacters($string);
        
        // add delimiter
        $string = $instance->addDelimiter($string);

        return $instance->escape($string);
    }

    /**
     * load dictionary content
     *
     * @return array
     */
    protected function loadDictionary()
    {<commit_msg>strtolower<commit_after><?php

/**
 * Chinese to pinyin translator
 *
 * @author Joy <<EMAIL>>
 *
 * @example
 * <pre>
 *      echo Pinyin::trans('带着希望去旅行，比到达终点更美好'), "\n";
 *      //output: "dài zhe xī wàng qù lǔ xíng bǐ dào dá zhōng diǎn gèng měi hǎo"
 * </pre>
 */
class Pinyin
{   

    /**
     * dictionary path
     *
     * @var string
     */
    protected $dictionary;

    /**
     * settings
     *
     * @var array
     */
    protected static $setting = array(
                                 'delimiter' => ' ',
                                 'accent'    => true,
                                );

    /**
     * instance
     *
     * @var Pinyin
     */
    protected static $instance;

    /**
     * constructor
     *
     * set dictionary path.
     */
    public function __construct()
    {
        $this->dictionary = __DIR__ . '/cedict/cedict_ts.u8';
    }

    /**
     * set the dictionary.
     *
     * @param array $setting settings.
     */
    public static function set(array $setting = array())
    {
        self::$setting = array_merge(self::$setting, $setting);
    }

    /**
     * get Pinyin instance
     *
     * @return Pinyin
     */
    public static function getInstance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new self;
        }

        return self::$instance;
    }

    /**
     * chinese to pinyin
     *
     * @param string $string  source string.
     * @param array  $setting settings.
     *
     * @return string
     */
    public static function trans($string, array $setting = array())
    {
        $instance = self::getInstance();

        // merge setting
        empty($setting) || self::set($setting);

        $dictionary = $instance->loadDictionary();
            
        // do replace
        foreach ($dictionary as $line) {
            $string = str_replace($line['simplified'], "{$line['pinyin_marks']} ", $string);
            if (!$instance->containsChinese($string)) {
                break;
            }
        }

        $string = strtolower($string);
        
        // add accents
        if(self::$setting['accent']) {
            $string = $instance->pinyin_addaccents($string);
        } else {
            $string = $instance->removeTone($string);
        }

        // clean the string
        $string = $instance->removeUnwantedCharacters($string);
        
        // add delimiter
        $string = $instance->addDelimiter($string);

        return $instance->escape($string);
    }

    /**
     * load dictionary content
     *
     * @return array
     */
    protected function loadDictionary()
    {
{'fraction_non_alphanumeric': 0.13689227836034318, 'fraction_numerical': 0.00038131553860819826, 'mean_word_length': 1.577886977886978, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5674173', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1563, 'n_words': 332}