语种识别.

@param string $text
@param array  $languages zh en jp kr
@param bool   $force     强制从待选择语言中选择

@throws TencentAIException

@return array

@see   https://ai.qq.com/doc/textdetect.shtml
325492
public function detect(string $text, array $languages = null, bool $force = false)
    {
        if ($languages) {
            foreach ($languages as $k) {
                $this->checkDetectType($k);
            }
            $languages = implode('|', $languages);
        }

        $candidate_langs = $languages;
        $force = (int) $force;

        return Request::exec(self::DETECT, compact('text', 'candidate_langs', 'force'));
    }
602740
public function aILabText(string $text, int $type = 0)
    {
        $this->checkAILabTextType($type);

        return Request::exec(self::AILAB_TEXT, compact('text', 'type'));
    }
1290962
public function make($name, $force = FALSE) {
        if (isset($this->singles[$name])) {
            return $this->singles[$name];
        }
        #获得实现提供者
        $closure = $this->getClosure($name);
        #获取实例
        $object = $this->build($closure, $name);
        #单例绑定
        if (isset($this->bindings[$name]['force']) && $this->bindings[$name]['force'] || $force) {
            $this->singles[$name] = $object;
        }
        return $object;
    }
594288
public function text($string, array $arguments = array())
    {
        if (!$this->prepared) {
            return $this->formatString($string, $arguments);
        }

        if (empty($this->langcode) || $this->langcode === 'en') {
            return $this->formatString($string, $arguments);
        }

        $context_file = $this->getContextFile($this->context, $this->langcode);
        $context_translations = $this->loadTranslation($context_file);

        if (isset($context_translations[$string])) {
            return $this->formatString($string, $arguments, $context_translations[$string]);
        }

        $common_file = $this->getCommonFile();
        $common_translations = $this->loadTranslation($common_file);

        if (isset($common_translations[$string])) {
            $this->addTranslation($string, $common_translations, $context_file);
            return $this->formatString($string, $arguments, $common_translations[$string]);
        }

        $this->addTranslation($string, $common_translations, $context_file);
        $this->addTranslation($string, $common_translations, $common_file);

        return $this->formatString($string, $arguments);
    }
1359582
public function text(String $text, Array $settings = []) : GD
    {
        return $this->char($text, $settings, 'string');
    }
1057344
static function Upper($text,$charset = null){
		static $TR_TABLES = array();

		$charset = self::_GetCharsetByName($charset);
		switch($charset){
			case "windows-1250":
			case "iso-8859-2":
				if(!isset($TR_TABLES[$charset])){
					require(dirname(__FILE__)."/tr_tables/lower_upper/$charset.php");
				}
				$text = strtr($text,$TR_TABLES[$charset]["mala"],$TR_TABLES[$charset]["velka"]);
				break;
			case "utf8":
				$text = mb_strtoupper($text,"utf8");
				break;
			case "ascii":
			default: 
				$text = strtoupper($text);
		}

		return $text;
	}
574689
public function detect($text)
    {
        $requestUrl = $this->buildRequestUrl($this->getDetectUrl(), [
            'q' => $text
        ]);

        $response = $this->getResponse($requestUrl);

        if (isset($response['data']['detections'])) {
            return $response['data']['detections'][0][0]['language'];
        }
        throw new TranslateException('Could not detect provided text language.');
    }
86167
private function tokenize($text)
    {
        $count = preg_match_all($this->lexerRegex, $text, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
        if(false === $count || preg_last_error() !== PREG_NO_ERROR) {
            throw new \RuntimeException(sprintf('PCRE failure `%s`.', preg_last_error()));
        }

        $tokens = array();
        $position = 0;

        foreach($matches as $match) {
            switch(true) {
                case -1 !== $match['string'][1]: { $token = $match['string'][0]; $type = self::TOKEN_STRING; break; }
                case -1 !== $match['ws'][1]: { $token = $match['ws'][0]; $type = self::TOKEN_WS; break; }
                case -1 !== $match['marker'][1]: { $token = $match['marker'][0]; $type = self::TOKEN_MARKER; break; }
                case -1 !== $match['delimiter'][1]: { $token = $match['delimiter'][0]; $type = self::TOKEN_DELIMITER; break; }
                case -1 !== $match['separator'][1]: { $token = $match['separator'][0]; $type = self::TOKEN_SEPARATOR; break; }
                case -1 !== $match['open'][1]: { $token = $match['open'][0]; $type = self::TOKEN_OPEN; break; }
                case -1 !== $match['close'][1]: { $token = $match['close'][0]; $type = self::TOKEN_CLOSE; break; }
                default: { throw new \RuntimeException(sprintf('Invalid token.')); }
            }
            $tokens[] = array($type, $token, $position);
            $position += mb_strlen($token, 'utf-8');
        }

        return $tokens;
    }
192815
private function decode($text)
    {
        try {
            if (!$this->pandoc) {
                $this->pandoc = new Pandoc();
            }

            return $this->pandoc->runWith($text, [
                'from' => 'latex',
                'to' => 'plain',
            ]);
        } catch (PandocException $exception) {
            throw new ProcessorException(
                sprintf(
                    'Error while processing LaTeX to Unicode: %s',
                    $exception->getMessage()
                ),
                0,
                $exception
            );
        }
    }
1085101
private function calculateLanguagesPercentage($languages)
    {
        $total = $languages->sum('count');

        return $languages->transform(function ($item) use ($total) {
            return $item + [
                'percentage' => round(($item['count'] / $total) * 100, 2)
            ];
        });
    }
310356
private function getLookupRequest(string $text, Language $languageTo, Language $languageFrom): RequestContext
    {
        $requestContext = (new RequestContext(sprintf("%s/%s", $this->apiEndpoint, self::LOOKUP_API_PATH)))
            ->setRequestParameters(
                [
                    'lang' => $this->getLanguageString($languageTo, $languageFrom),
                    'flags' => self::LOOKUP_MORPHO_FLAG,
                    'ui' => Language::CODE_ENGLISH
                ] + $this->getAuthData()
            )
            ->setData(['text' => $text])
            ->setMethod(RequestContext::METHOD_POST);

        return $requestContext;
    }
581827
public static function convertToUnicode( $string, $force = FALSE )
	{
		if( !( !$force && self::isUnicode( $string ) ) )
			$string	= utf8_encode( $string );
		return $string;
	}
1327052
private function parseBlock($text, &$lines)
    {
        $lines = explode("\n", $text);
        $this->_blocks = array();
        $this->_current = 'normal';
        $this->_pos = -1;
        $state = array(
            'special'   =>  implode("|", array_keys($this->_specialWhiteList)),
            'empty'     =>  0,
            'html'      =>  false
        );
        // analyze by line
        foreach ($lines as $key => $line) {
            $block = $this->getBlock();
            $args = array($block, $key, $line, &$state, $lines);
            if ($this->_current != 'normal') {
                $pass = call_user_func_array($this->_parsers[$this->_current], $args);
                if (!$pass) {
                    continue;
                }
            }
            foreach ($this->_parsers as $name => $parser) {
                if ($name != $this->_current) {
                    $pass = call_user_func_array($parser, $args);
                    if (!$pass) {
                        break;
                    }
                }
            }
        }
        return $this->optimizeBlocks($this->_blocks, $lines);
    }
1100458
private function getLanguagesNumbers(array $languages): array
    {
        $languagesNumbers = array_column($languages, 'id');

        return array_combine($languagesNumbers, array_fill_keys($languagesNumbers, false));
    }
995635
public function getJsApiTicket($force = false)
    {
        $time = time(); // 为了更精确控制.取当前时间计算
        $cache_name = 'wechat_jsapi_ticket_' . $this->corpId . '_' . $this->app;
        if ($this->_jsApiTicket === null || $this->_jsApiTicket['expire'] < $time || $force) {
            $result = $this->_jsApiTicket === null && !$force ? $this->getCache($cache_name, false) : false;
            if ($result === false) {
                if (!($result = $this->requestJsApiTicket())) {
                    throw new HttpException(500, 'Fail to get jsapi_ticket from wechat server.');
                }
                $result['expire'] = $time + $result['expires_in'];
                $this->trigger(self::EVENT_AFTER_JS_API_TICKET_UPDATE, new Event(['data' => $result]));
                $this->setCache($cache_name, $result, $result['expires_in']);
            }
            $this->setJsApiTicket($result);
        }
        return $this->_jsApiTicket['ticket'];
    }
283160
public function translate(string $text, string $languageTo, string $languageFrom = ''): TranslateResponse
    {
        return $this->getTranslateApi()->translate($text, new Language($languageTo), new Language($languageFrom));
    }
904519
public function text(string $text, string $source = 'auto', string $target = 'auto')
    {
        if (!('auto' === $source && 'auto' === $target)) {
            $this->checkTextTarget($source, $target);
        }

        return Request::exec(self::TEXT, compact('text', 'source', 'target'));
    }