<commit_before><?php

namespace Core\Service;

use Zend\Http\Request;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use DetectLanguage\DetectLanguage as DetectLanguageLibrary;

class DetectLanguage extends \Core\Service\CoreService implements ServiceLocatorAwareInterface
{
    public static $plans = [
        "free"=>["requests"=>5000,"bytes"=>1048576],
        "basic"=>["requests"=>100000,"bytes"=>20971520],
        "plus"=>["requests"=>1000000,"bytes"=>209715200],
        "premium"=>["requests"=>10000000,"bytes"=>2147483648],
        "premium+"=>["requests"=>40000000,"bytes"=>8589934592],
    ];
    private $api;
    protected $plan;
    protected $key;
    public function __construct()
    {

    }

    protected function init()
    {
        $config = $this->sm->get('AppConfig')->get('detectlanguage');
        if(!isset($config) || !isset($config["key"]))
        {
            throw new \Exception("you need to specify an api key for detectlanguage before using it");
        }
        DetectLanguageLibrary::setSecure( true );
        DetectLanguageLibrary::setApiKey( $config['key'] );
        $this->key = $config['key'];
        $plan = isset($config["plan"])?$config["plan"]:"free";
        if(!isset(DetectLanguage::$plans[$plan]))
        {
            $plan = "free";
        }
        $this->plan = DetectLanguage::$plans[$plan];
    }
    protected function __getDetection($text)
    {
        if(mb_strlen($text) == 0)
        {
            return [$this->_getDefaultLang($text)];
        }
        $this->getDetectLanguageTable()->addCall($this->key, mb_strlen($text));
        $detections = DetectLanguageLibrary::detect( $text ); 
        foreach($detections as $key=>$detect)
        {
            $detections[$key]->len =  mb_strlen($text);
        }

        return $detections;
    }
    protected function _getDefaultLang($text)
    {
        $this->getNotificationManager()->alert("detect_language", "used default language");
        //TODO:alert
        $lang = new \StdClass();
        $lang->language = "en";
        $lang->isReliable = true;
        $lang->confidence = 10.0;
        $lang->fake = true;
        $lang->len = mb_strlen($text);
        return $lang;
    }
    protected function _detect($text, $smart)
    {<commit_msg>add local fix<commit_after><?php

namespace Core\Service;

use Zend\Http\Request;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use DetectLanguage\DetectLanguage as DetectLanguageLibrary;

class DetectLanguage extends \Core\Service\CoreService implements ServiceLocatorAwareInterface
{
    public static $plans = [
        "free"=>["requests"=>5000,"bytes"=>1048576],
        "basic"=>["requests"=>100000,"bytes"=>20971520],
        "plus"=>["requests"=>1000000,"bytes"=>209715200],
        "premium"=>["requests"=>10000000,"bytes"=>2147483648],
        "premium+"=>["requests"=>40000000,"bytes"=>8589934592],
    ];
    private $api;
    protected $plan;
    protected $key;
    public function __construct()
    {

    }

    protected function init()
    {
        $config = $this->sm->get('AppConfig')->get('detectlanguage');
        if(!isset($config) || !isset($config["key"]))
        {
            throw new \Exception("you need to specify an api key for detectlanguage before using it");
        }
        DetectLanguageLibrary::setSecure( true );
        DetectLanguageLibrary::setApiKey( $config['key'] );
        $this->key = $config['key'];
        $plan = isset($config["plan"])?$config["plan"]:"free";
        if(!isset(DetectLanguage::$plans[$plan]))
        {
            $plan = "free";
        }
        $this->plan = DetectLanguage::$plans[$plan];
    }
    protected function __getDetection($text)
    {
        if($this->sm->get("AppConfig")->isLocal())
        {
            dd("ok");
        }
        if(mb_strlen($text) == 0)
        {
            return [$this->_getDefaultLang($text)];
        }
        $this->getDetectLanguageTable()->addCall($this->key, mb_strlen($text));
        $detections = DetectLanguageLibrary::detect( $text ); 
        foreach($detections as $key=>$detect)
        {
            $detections[$key]->len =  mb_strlen($text);
        }

        return $detections;
    }
    protected function _getDefaultLang($text)
    {
        $this->getNotificationManager()->alert("detect_language", "used default language");
        //TODO:alert
        $lang = new \StdClass();
        $lang->language = "en";
        $lang->isReliable = true;
        $lang->confidence = 10.0;
        $lang->fake = true;
        $lang->len = mb_strlen($text);
        return $lang;
    }
    protected function _detect($text, $smart)
    {
{'fraction_non_alphanumeric': 0.16228715577044356, 'fraction_numerical': 0.0340550767290309, 'mean_word_length': 2.82475884244373, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, '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': '5754189', 'n_tokens_mistral': 1511, 'n_tokens_neox': 1387, 'n_words': 236}