<commit_before><?php
/**
 * Tatoeba Project, free collaborativ creation of languages corpuses project
 * Copyright (C) 2009 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * PHP version 5 
 *
 * @category PHP
 * @package  Tatoeba
 * @author   <NAME> <<EMAIL>>
 * @license  Affero General Public License
 * @link     http://tatoeba.org
 */

/**
 * Model Class which represent chinese-like characters into the database
 *
 * @category Sinograms
 * @package  Tatoeba
 * @author   <NAME> <<EMAIL>>
 * @license  Affero General Public License
 * @link     http://tatoeba.org
*/

class Sinogram extends AppModel
{

    public $name = "Sinogram";
    public $hasMany = array ('SinogramSubglyph'); 
    public $actsAs = array("Containable");
    /**
    * search a sinogram matching the input requirements
    *
    * @param array $subGlyphArray an array containing in each cells a sublyph
    * @param int   $minStrokes    minimum strokes the matching characters should
    *                             have
    * @param int   $maxStrokes    maximum etc...
    *
    * @return array of matching sinograms
    */
    
    public function search($subGlyphArray , $minStrokes = -1 , $maxStrokes = -1 )
    {
       
        // if there's only character in subglyph search then we should
        // execute one more request to try to match itself 
        // it's because the characters decomposition data are trees
        // so you have'nt loopback  
        $onlyOneCharacter = false ;
        if ( count($subGlyphArray) == 1 ) {
            $sinogram = $subGlyphArray[0];
            $onlyOneCharacter = true;
        }

        // generate the IN() statement
        // TODO I think there's a better way to do that in sql
        // as IN statement will be very slow when exceeding 5+
        $numberOfSublyph = count($subGlyphArray);
        for ($i = 0 ; $i < $numberOfSublyph; $i++) {
            $subGlyphArray[$i] = "'".$subGlyphArray[$i] ."'" ;
        }
        $subglyphsString= implode(",", $subGlyphArray);
        
        $result = $this->query(
            "SELECT Sinogram.id , Sinogram.glyph
             FROM  sinogram_subglyphs , sinograms as Sinogram
             WHERE
                Sinogram.`glyph` = sinogram_subglyphs.`glyph`
                AND  subglyph IN ( ". $subglyphsString  ." )
            GROUP BY glyph 
                HAVING 
                    count(DISTINCT sinogram_subglyphs.subglyph) 
                    =". count($subGlyphArray) .";" 
        );

        // if there's only character, it should be logical that this character match
        // itself
        if ($onlyOneCharacter) {
            $thisGlyph = $this->find(
                "first", array(
                "fields" => array("Sinogram.id","Sinogram.glyph"),
                "conditions" => array("Sinogram.glyph" => $sinogram  )
                )
            );
            array_push($result, $thisGlyph);
        }


        return $result ;
    }
        
    /**
    * explode the input sinograms into their composants
    *
    * @param array $toExplodeArray array of sinograms to explod
    *
    * @return array of compounds
    */

    public function explode($toExplodeArray)
    {
        $explodeArraySize = count($toExplodeArray);
        for ($i = 0 ; $i < $explodeArraySize; $i++) {
            $toExplodeArray[$i] = "'".$toExplodeArray[$i] ."'" ;
        }

        $toExplodeString = implode(",", $toExplodeArray);

        $results = $this->query(
            "SELECT glyph , subglyph
            FROM  sinogram_subglyphs
            WHERE glyph IN (". $toExplodeString .") ;" 
        ); 
        return $results;
    }

    /**
     * get informations of a given sinogram
     *
     * @param string $sinogram the sinogram we want informations of
        *
     * @return array informations concerning this sinogram
     */

    public function informations($sinogram)
    {
        $return = $this->find(
            'first',
            array(
                "fields" => array(
                    "Sinogram.glyph",
                    "Sinogram.strokes",
                    "Sinogram.english",<commit_msg>avoid sql error when clickig on explode with an empty input<commit_after><?php
/**
 * Tatoeba Project, free collaborativ creation of languages corpuses project
 * Copyright (C) 2009 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * PHP version 5 
 *
 * @category PHP
 * @package  Tatoeba
 * @author   <NAME> <<EMAIL>>
 * @license  Affero General Public License
 * @link     http://tatoeba.org
 */

/**
 * Model Class which represent chinese-like characters into the database
 *
 * @category Sinograms
 * @package  Tatoeba
 * @author   Allan Simon <<EMAIL>>
 * @license  Affero General Public License
 * @link     http://tatoeba.org
*/

class Sinogram extends AppModel
{

    public $name = "Sinogram";
    public $hasMany = array ('SinogramSubglyph'); 
    public $actsAs = array("Containable");
    /**
    * search a sinogram matching the input requirements
    *
    * @param array $subGlyphArray an array containing in each cells a sublyph
    * @param int   $minStrokes    minimum strokes the matching characters should
    *                             have
    * @param int   $maxStrokes    maximum etc...
    *
    * @return array of matching sinograms
    */
    
    public function search($subGlyphArray , $minStrokes = -1 , $maxStrokes = -1 )
    {
       
        // if there's only character in subglyph search then we should
        // execute one more request to try to match itself 
        // it's because the characters decomposition data are trees
        // so you have'nt loopback  
        $onlyOneCharacter = false ;
        if ( count($subGlyphArray) == 1 ) {
            $sinogram = $subGlyphArray[0];
            $onlyOneCharacter = true;
        }

        // generate the IN() statement
        // TODO I think there's a better way to do that in sql
        // as IN statement will be very slow when exceeding 5+
        $numberOfSublyph = count($subGlyphArray);
        for ($i = 0 ; $i < $numberOfSublyph; $i++) {
            $subGlyphArray[$i] = "'".$subGlyphArray[$i] ."'" ;
        }
        $subglyphsString= implode(",", $subGlyphArray);
        
        $result = $this->query(
            "SELECT Sinogram.id , Sinogram.glyph
             FROM  sinogram_subglyphs , sinograms as Sinogram
             WHERE
                Sinogram.`glyph` = sinogram_subglyphs.`glyph`
                AND  subglyph IN ( ". $subglyphsString  ." )
            GROUP BY glyph 
                HAVING 
                    count(DISTINCT sinogram_subglyphs.subglyph) 
                    =". count($subGlyphArray) .";" 
        );

        // if there's only character, it should be logical that this character match
        // itself
        if ($onlyOneCharacter) {
            $thisGlyph = $this->find(
                "first", array(
                "fields" => array("Sinogram.id","Sinogram.glyph"),
                "conditions" => array("Sinogram.glyph" => $sinogram  )
                )
            );
            array_push($result, $thisGlyph);
        }


        return $result ;
    }
        
    /**
    * explode the input sinograms into their composants
    *
    * @param array $toExplodeArray array of sinograms to explod
    *
    * @return array of compounds
    */

    public function explode($toExplodeArray)
    {
        $explodeArraySize = count($toExplodeArray);

        if ($explodeArraySize == 0){
            return array();
        }

        for ($i = 0 ; $i < $explodeArraySize; $i++) {
            $toExplodeArray[$i] = "'".$toExplodeArray[$i] ."'" ;
        }

        $toExplodeString = implode(",", $toExplodeArray);

        $results = $this->query(
            "SELECT glyph , subglyph
            FROM  sinogram_subglyphs
            WHERE glyph IN (". $toExplodeString .") ;" 
        ); 
        return $results;
    }

    /**
     * get informations of a given sinogram
     *
     * @param string $sinogram the sinogram we want informations of
        *
     * @return array informations concerning this sinogram
     */

    public function informations($sinogram)
    {
        $return = $this->find(
            'first',
            array(
                "fields" => array(
                    "Sinogram.glyph",
                    "Sinogram.strokes",
                    "Sinogram.english",
{'fraction_non_alphanumeric': 0.10068224105850734, 'fraction_numerical': 0.0027909861484391152, 'mean_word_length': 1.9944289693593316, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3358406', 'n_tokens_mistral': 2801, 'n_tokens_neox': 2604, 'n_words': 871}