Fukuball\Jieba\Jieba::__cutAll PHP Метод

__cutAll() публичный статический Метод

Static method __cutAll
public static __cutAll ( string $sentence, array $options = [] ) : array
$sentence string # input sentence
$options array # other options
Результат array $words
    public static function __cutAll($sentence, $options = array())
    {
        $defaults = array('mode' => 'default');
        $options = array_merge($defaults, $options);
        $words = array();
        $DAG = self::getDAG($sentence);
        $old_j = -1;
        foreach ($DAG as $k => $L) {
            if (count($L) == 1 && $k > $old_j) {
                $word = mb_substr($sentence, $k, $L[0] - $k + 1, 'UTF-8');
                array_push($words, $word);
                $old_j = $L[0];
            } else {
                foreach ($L as $j) {
                    if ($j > $k) {
                        $word = mb_substr($sentence, $k, $j - $k + 1, 'UTF-8');
                        array_push($words, $word);
                        $old_j = $j;
                    }
                }
            }
        }
        return $words;
    }

Usage Example

Пример #1
0
 /**
  * Static method cut
  *
  * @param string  $sentence # input sentence
  * @param boolean $cut_all  # cut_all or not
  * @param array   $options  # other options
  *
  * @return array $seg_list
  */
 public static function cut($sentence, $cut_all = false, $options = array())
 {
     $defaults = array('mode' => 'default');
     $options = array_merge($defaults, $options);
     $seg_list = array();
     $re_han_pattern = '([\\x{4E00}-\\x{9FA5}]+)';
     $re_skip_pattern = '([a-zA-Z0-9+#\\n]+)';
     preg_match_all('/(' . $re_han_pattern . '|' . $re_skip_pattern . ')/u', $sentence, $matches, PREG_PATTERN_ORDER);
     $blocks = $matches[0];
     foreach ($blocks as $blk) {
         if (preg_match('/' . $re_han_pattern . '/u', $blk)) {
             if ($cut_all) {
                 $words = Jieba::__cutAll($blk);
             } else {
                 $words = Jieba::__cutDAG($blk);
             }
             foreach ($words as $word) {
                 array_push($seg_list, $word);
             }
         } else {
             array_push($seg_list, $blk);
         }
         // end else (preg_match('/'.$re_han_pattern.'/u', $blk))
     }
     // end foreach ($blocks as $blk)
     return $seg_list;
 }