自然语言处理公共方法.

@param      $url
@param      $text
@param bool $charSetGBK

@throws TencentAIException

@return array
432026
private function nlp($url, $text, bool $charSetGBK = true)
    {
        if ($charSetGBK) {
            $data = [
                'text' => mb_convert_encoding($text, 'gbk', 'utf8'),
            ];

            return Request::exec($url, $data, false);
        }

        $data = [
            'text' => $text,
        ];

        return Request::exec($url, $data);
    }
643309
function borkify( $text )
    {
        $textBlocks = preg_split( "/(%[^ ]+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE );
        $newTextBlocks = array();
        foreach ( $textBlocks as $text )
        {
            if ( $text[0] == '%' )
            {
                $newTextBlocks[] = $text;
                continue;
            }
            $orgtext = $text;
            $text = preg_replace( "/a\B/", "e", $text );
            $text = preg_replace( "/an/", "un", $text );
            $text = preg_replace( "/au/", "oo", $text );
            $text = preg_replace( "/en\b/", "ee", $text );
            $text = preg_replace( "/\Bew/", "oo", $text );
            $text = preg_replace( "/\Bf/", "ff", $text );
            $text = preg_replace( "/\Bi/", "ee", $text );
            $text = preg_replace( "/\Bir/", "ur", $text );
            $text = preg_replace( "/\bo/", "oo", $text );
            $text = preg_replace( "/ow/", "oo", $text );
            $text = preg_replace( "/ph/", "f", $text );
            $text = preg_replace( "/th\b/", "t", $text );
            $text = preg_replace( "/\Btion/", "shun", $text );
            $text = preg_replace( "/\Bu/", "oo", $text );
            $text = preg_replace( "/\bU/", "Oo", $text );
            $text = preg_replace( "/y\b/", "ai", $text );
            $text = preg_replace( "/v/", "f", $text );
            $text = preg_replace( "/w/", "v", $text );
            $text = preg_replace( "/ooo/", "oo", $text );
            if ( $orgtext == $text )
                $text = $text . "-a";
            $newTextBlocks[] = $text;
        }
        $text = implode( '', $newTextBlocks );
        $text = preg_replace( "/([:.?!])(.*)/", "\\2\\1", $text );
        $text = "[" . $text . "]";
        return $text;
    }
1030028
public static function preg($text, $preg)
	{
	    if (!$preg) {
	        throw new \InvalidArgumentException('Please set the $preg pattern');
	    }

	    if (is_array($preg)) {
	        if (empty($preg['preg'])) {
	            throw new \InvalidArgumentException('The $preg argument must have "preg" key');
	        }
	        $preg = $preg['preg'];
	    }

	    return (bool)preg_match($preg, $text);
	}
733319
static function Tokenize($string, $splitChars = ' %$§&/()?!.:;\#\'"')
    {
       $tokens = array();

       $tok = strtok($string,  $splitChars);
       while ($tok !== false)
       {
            array_push($tokens, $tok);
            $tok = strtok($splitChars);
       }
       return $tokens;
    }
991616
static char getChecksum(String text) {
        int chk = 0;
        for (int k = 0; k < text.length(); ++k) {
            int idx = CHARS.indexOf(text.charAt(k));
            if (idx < 0)
                throw new IllegalArgumentException("The character '" + text.charAt(k) + "' is illegal in code 39.");
            chk += idx;
        }
        return CHARS.charAt(chk % 43);
    }
570826
public static function sendGreenAlert(string $text, int $to_user_id = USER_ID, int $from_user_id = 0): UsersMessageEntity
    {
        return self::sendMessage($text, $to_user_id, $from_user_id, self::TOASTR_MESSAGE_COLOR_GREEN);
    }
501495
public function read($text)
    {
        $payload = $this->getPayload($text);

        $handle = $this->getHandle();
        $handle->setUrl($payload->getServiceUrl());
        $handle->setOption(CURLOPT_RETURNTRANSFER, 1);
        $handle->setOption(CURLOPT_POST, true);

        $handle->setOption(CURLOPT_POSTFIELDS, $payload->getPayload());
        $handle->setOption(CURLOPT_HTTPHEADER, $payload->getHeaders());
        $handle->setOption(CURLOPT_SSL_VERIFYPEER, false);

        $response = $handle->execute();
        $this->setHeaders($handle->getInfo());
        return $response;
    }
900713
public function addJavaScript( $uri, $type = NULL, $charset = NULL )
	{
		$typeDefault	= 'text/javascript';
		if( isset( $this->metaTags["http-equiv:content-script-type"] ) )
			$typeDefault	= $this->metaTags["http-equiv:content-script-type"]['content'];
		$scriptData	= array(
			'type'		=> $type ? $type : $typeDefault,
			'charset'	=> $charset ? $charset : NULL,
			'src'		=> $uri,
		);
		$this->scripts[]	= $scriptData;
	}
580921
public function parse(string $text) : bool
    {
        $lower = strtolower($text);
        if (! in_array($lower, array_merge(self::TRUE_VALUES, self::FALSE_VALUES)))
            throw new ParseException($this, $text, 0, self::PARSE_ERROR);
        return in_array($lower, self::TRUE_VALUES);
    }
1187330
public function setText($content, $charset = 'utf-8') {
        $this->text = $content;
        $this->textCharset = $charset;
        return $this;
    }
666076
public function makeLinks($text)
    {
        $pattern = '~(?xi)
              (?:
                ((ht|f)tps?://)                    # scheme://
                |                                  #   or
                www\d{0,3}\.                       # "www.", "www1.", "www2." ... "www999."
                |                                  #   or
                www\-                              # "www-"
                |                                  #   or
                [a-z0-9.\-]+\.[a-z]{2,4}(?=/)      # looks like domain name followed by a slash
              )
              (?:                                  # Zero or more:
                [^\s()<>]+                         # Run of non-space, non-()<>
                |                                  #   or
                \(([^\s()<>]+|(\([^\s()<>]+\)))*\) # balanced parens, up to 2 levels
              )*
              (?:                                  # End with:
                \(([^\s()<>]+|(\([^\s()<>]+\)))*\) # balanced parens, up to 2 levels
                |                                  #   or
                [^\s`!\-()\[\]{};:\'".,<>?«»“”‘’]  # not a space or one of these punct chars
              )
        ~';

        $callback = function ($urlMatch) {
            $url = $urlMatch[0];

            // Look for protocol
            preg_match('~^(ht|f)tps?://~', $url, $protocolMatch);

            if ($protocolMatch) {
                $protocol = $protocolMatch[0];
            } else {
                $protocol = 'http://';
                $url = $protocol.$url;
            }

            // Start building caption, remove protocol from url
            $noProtocol = substr($url, strlen($protocol));

            // Check for a variation of www
            preg_match('/www\d{0,3}\./', $noProtocol, $wwwMatch);

            if ($wwwMatch) {
                // Remove www
                $noProtocol = substr($noProtocol, strlen($wwwMatch[0]));
            }

            // Only use domain name as caption
            $caption = explode('/', $noProtocol)[0];

            return $this->generateTag('a', $this->linkAttributes + ['href' => $url, $this->externalKey => $caption]);
        };

        return preg_replace_callback($pattern, $callback, $text);
    }
306864
private function checkTextTarget($source, $target): void
    {
        $text_array = $this->text_array;
        if (!(\array_key_exists($source, $text_array))) {
            throw new TencentAIException(90701);
        }
        if (!(\in_array($target, $text_array[$source], true))) {
            throw new TencentAIException(90702);
        }
    }
239228
public static String encodeIllegalXMLCharacters(String text)
   {
      if (text == null)
      {
         throw new IllegalArgumentException("null argument");
      }
      StringBuilder buf = null;
      int length = text.length();
      int pos = 0;
      for (int i = 0; i < length; i++)
      {
         int ch = text.charAt(i);
         switch (ch)
         {
            case '<' :
            case '>' :
            case '&' :
            case '"' :
            case '\'' :
               if (buf == null)
               {
                  buf = new StringBuilder();
               }
               if (i > 0)
               {
                  buf.append(text.substring(pos, i));
               }
               pos = i + 1;
               break;
            default :
               continue;
         }
         if (ch == '<')
         {
            buf.append("&lt;");
         }
         else if (ch == '>')
         {
            buf.append("&gt;");
         }
         else if (ch == '&')
         {
            buf.append("&amp;");
         }
         else if (ch == '"')
         {
            buf.append("&quot;");
         }
         else if (ch == '\'')
         {
            buf.append("&apos;");
         }
      }
      if (buf == null)
      {
         return text;
      }
      else
      {
         if (pos < length)
         {
            buf.append(text.substring(pos));
         }
         return buf.toString();
      }
   }
339119
public function lexerCustom($text, $options=array()){

        $data = array();
        
        $data['text'] = $text;

        $data = array_merge($data, $options);
        $data = mb_convert_encoding(json_encode($data), 'GBK', 'UTF8');

        return $this->request($this->lexerCustomUrl, $data);
    }
886447
static function parse_string( $src, $unicode = true, $parser = __CLASS__, $lexer = 'JTokenizer' ){
		$Tokenizer = new $lexer( false, $unicode);
		$tokens = $Tokenizer->get_all_tokens( $src );
		unset( $src );
		$Parser = new $parser;
		return $Parser->parse( $tokens );
	}
1359582
public function text(String $text, Array $settings = []) : GD
    {
        return $this->char($text, $settings, 'string');
    }
435043
public function hashString($text)
    {
        $charList = str_split($text);
        $chars = array_fill_keys( $charList, 0);
        foreach($charList as $char) { 
            $chars[$char]++;
        }
        return $chars;
    }