<commit_before><?php
/**
 * Interface translation tool functionality
 *
 * @package    phpMyFAQ
 * @subpackage PMF_TransTool
 * @author     <NAME> <<EMAIL>>
 * @since      2009-05-12
 * @version    SVN: $Id$
 * @copyright  2006-2009 phpMyFAQ Team
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 */

/**
 * PMF_TransTool
 *
 * @package    phpMyFAQ
 * @subpackage PMF_TransTool
 * @author     <NAME> <<EMAIL>>
 * @since      2009-05-12
 * @version    SVN: $Id$
 * @copyright  2006-2009 phpMyFAQ Team
 */
class PMF_TransTool
{
    /**
     * Parse language file and get vars it does contain
     *
     * @param string $filepath
     * 
     * @return array
     */
    public function getVars($filepath)
    {
        $retval = array();
        
        if(file_exists($filepath) && is_readable($filepath)) {
        
            $orig = file($filepath);
            
            while(list(,$line) = each($orig)) {
                $line = rtrim($line);
                /**
                 * Bypass all but variable definitions
                 */
                if(strlen($line) && '$' == $line[0]) {
                    /**
                     * $PMF_LANG["key"] = "val";
                     * or
                     * $PMF_LANG["key"] = array(0 => "something", 1 => ...);
                     * turns to something like  array('$PMF_LANG["key"]', '"val";')
                     */
                    $m = explode("=", $line, 2);
                    
                    $key = str_replace(array('["', '"]', '[\'', '\']'), array('[', ']', '[', ']'), PMF_String::substr(trim($m[0]), 1));
                    
                    $tmp = trim(@$m[1]);
                    if(0 === PMF_String::strpos($tmp, 'array')) {
                        $retval[$key] = PMF_String::substr($tmp, 0, -1);
                    } else {
                        $retval[$key] = PMF_String::substr($tmp, 1, -2);
                    }
                }
            }
        }
    
        return $retval;
    }
    
    
    /**
     * Get the translation ratio of the language files
     * 
     * @param string $filepathExemplary
     * @param string $filepathToCheck
     * 
     * @return integer
     */
    public function getTranslatedPercentage($filepathExemplary, $filepathToCheck)
    {
        $exemplary = $this->getVars($filepathExemplary);
        $toCheck = $this->getVars($filepathToCheck);
        
        $retval = $countAll = $countTranslated = 0;
        
        if($exemplary) {
            while(list($key, $val) = each($exemplary)) {
                if(!$this->isKeyIgnorable($key) && !$this->isValIgnorable($val)) {
                    if(isset($toCheck[$key]) && $toCheck[$key] != $val) {
                        $countTranslated++;
                    }
                    
                    $countAll++;
                }
            }
            
            $retval = floor(100*$countTranslated/$countAll);
        }
        
        unset($exemplary, $toCheck);
        
        return $retval;
    }
    
    
    /**
     * Check if the key can be ignored while comparing
     * 
     * @param string $key
     * 
     * @return boolean
     */
    public function isKeyIgnorable($key)
    {
        $keyIgnore = array('PMF_LANG[metaCharset]',
                           'PMF_LANG[metaLanguage]',
                           'PMF_LANG[language]',
                           'PMF_LANG[dir]');

        return in_array($key, $keyIgnore);
    }
    
    /**
     * Check if we can ignore a value while comparing. Actually
     * catching empty and non alphanumeric strings
     * 
     * @param string $val
     * 
     * @return boolean
     */
    public function isValIgnorable($val)
    {
        return PMF_String::preg_match('/^[^a-z0-9]*$/i', $val);
    }
}
?><commit_msg>Coding style<commit_after><?php
/**
 * Interface translation tool functionality
 *
 * @package    phpMyFAQ
 * @subpackage PMF_TransTool
 * @author     <NAME> <<EMAIL>>
 * @since      2009-05-12
 * @version    SVN: $Id$
 * @copyright  2006-2009 phpMyFAQ Team
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 */

/**
 * PMF_TransTool
 *
 * @package    phpMyFAQ
 * @subpackage PMF_TransTool
 * @author     <NAME> <<EMAIL>>
 * @since      2009-05-12
 * @version    SVN: $Id$
 * @copyright  2006-2009 phpMyFAQ Team
 */
class PMF_TransTool
{
    /**
     * Parse language file and get vars it does contain
     *
     * @param string $filepath Filepath
     * 
     * @return array
     */
    public function getVars($filepath)
    {
        $retval = array();
        
        if (file_exists($filepath) && is_readable($filepath)) {
        
            $orig = file($filepath);
            
            while (list(,$line) = each($orig)) {
                $line = rtrim($line);
                /**
                 * Bypass all but variable definitions
                 */
                if (strlen($line) && '$' == $line[0]) {
                    /**
                     * $PMF_LANG["key"] = "val";
                     * or
                     * $PMF_LANG["key"] = array(0 => "something", 1 => ...);
                     * turns to something like  array('$PMF_LANG["key"]', '"val";')
                     */
                    $m   = explode("=", $line, 2);
                    $key = str_replace(array('["', '"]', '[\'', '\']'), array('[', ']', '[', ']'), PMF_String::substr(trim($m[0]), 1));
                    $tmp = trim(@$m[1]);
                    
                    if (0 === PMF_String::strpos($tmp, 'array')) {
                        $retval[$key] = PMF_String::substr($tmp, 0, -1);
                    } else {
                        $retval[$key] = PMF_String::substr($tmp, 1, -2);
                    }
                }
            }
        }
    
        return $retval;
    }
    
    
    /**
     * Get the translation ratio of the language files
     * 
     * @param string $filepathExemplary Exemplary file path
     * @param string $filepathToCheck   Filepath to check
     * 
     * @return integer
     */
    public function getTranslatedPercentage($filepathExemplary, $filepathToCheck)
    {
        $exemplary = $this->getVars($filepathExemplary);
        $toCheck   = $this->getVars($filepathToCheck);
        
        $retval = $countAll = $countTranslated = 0;
        
        if ($exemplary) {
            while (list($key, $val) = each($exemplary)) {
                if (!$this->isKeyIgnorable($key) && !$this->isValIgnorable($val)) {
                    if (isset($toCheck[$key]) && $toCheck[$key] != $val) {
                        $countTranslated++;
                    }
                    
                    $countAll++;
                }
            }
            
            $retval = floor(100 * $countTranslated / $countAll);
        }
        
        unset($exemplary, $toCheck);
        
        return $retval;
    }
    
    
    /**
     * Check if the key can be ignored while comparing
     * 
     * @param string $key Key
     * 
     * @return boolean
     */
    public function isKeyIgnorable($key)
    {
        $keyIgnore = array('PMF_LANG[metaCharset]',
                           'PMF_LANG[metaLanguage]',
                           'PMF_LANG[language]',
                           'PMF_LANG[dir]');

        return in_array($key, $keyIgnore);
    }
    
    /**
     * Check if we can ignore a value while comparing. Actually
     * catching empty and non alphanumeric strings
     * 
     * @param string $val Value
     * 
     * @return boolean
     */
    public function isValIgnorable($val)
    {
        return PMF_String::preg_match('/^[^a-z0-9]*$/i', $val);
    }
}
{'fraction_non_alphanumeric': 0.13497652582159625, 'fraction_numerical': 0.012206572769953052, 'mean_word_length': 1.541306292872055, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1330431', 'n_tokens_mistral': 2576, 'n_tokens_neox': 2427, 'n_words': 584}