PHP 和 NLP:如何将嵌套的括号(分析器输出)转换为数组?

8

希望将带有嵌套括号的文本转换为嵌套数组。以下是NLP解析器的示例输出:

(TOP (S (NP (PRP I)) (VP (VBP love) (NP (NP (DT a) (JJ big) (NN bed)) (PP (IN of) (NP (NNS roses))))) (. .)))

我喜欢一张大床,就像一片玫瑰花海。

希望将这个转换为嵌套数组,使其看起来像这样:

TOP
 S
  NP
   PRP I
  VP 
   VBP Love

我在网上找到了这个php大括号转换为数组,但那不是一个嵌套数组

2个回答

25

代码解释:

<?php

class ParensParser
{
    // something to keep track of parens nesting
    protected $stack = null;
    // current level
    protected $current = null;

    // input string to parse
    protected $string = null;
    // current character offset in string
    protected $position = null;
    // start of text-buffer
    protected $buffer_start = null;

    public function parse($string)
    {
        if (!$string) {
            // no string, no data
            return array();
        }

        if ($string[0] == '(') {
            // killer outer parens, as they're unnecessary
            $string = substr($string, 1, -1);
        }

        $this->current = array();
        $this->stack = array();

        $this->string = $string;
        $this->length = strlen($this->string);
        // look at each character
        for ($this->position=0; $this->position < $this->length; $this->position++) {
            switch ($this->string[$this->position]) {
                case '(':
                    $this->push();
                    // push current scope to the stack an begin a new scope
                    array_push($this->stack, $this->current);
                    $this->current = array();
                    break;

                case ')':
                    $this->push();
                    // save current scope
                    $t = $this->current;
                    // get the last scope from stack
                    $this->current = array_pop($this->stack);
                    // add just saved scope to current scope
                    $this->current[] = $t;
                    break;
               /* 
                case ' ':
                    // make each word its own token
                    $this->push();
                    break;
                */
                default:
                    // remember the offset to do a string capture later
                    // could've also done $buffer .= $string[$position]
                    // but that would just be wasting resources…
                    if ($this->buffer_start === null) {
                        $this->buffer_start = $this->position;
                    }
            }
        }

        return $this->current;
    }

    protected function push()
    {
        if ($this->buffer_start !== null) {
            // extract string from buffer start to current position
            $buffer = substr($this->string, $this->buffer_start, $this->position - $this->buffer_start);
            // clean buffer
            $this->buffer_start = null;
            // throw token into current scope
            $this->current[] = $buffer;
        }
    }
}

$string = '(TOP (S (NP (PRP I)) (VP (VBP love) (NP (NP (DT a) (JJ big) (NN bed)) (PP (IN of) (NP (NNS roses))))) (. .)))';
$p = new ParensParser();
$result = $p->parse($string);
var_dump($result);

3

非常棒的回答! 要捕获尾随字符串(例如“a(b)c”中的“c”),您需要更改类的结尾。

                default:
                // remember the offset to do a string capture later
                // could've also done $buffer .= $string[$position]
                // but that would just be wasting resources…
                if ($this->buffer_start === null) {
                    $this->buffer_start = $this->position;
                }
        }
    }
    // catch any trailing text
    if ($this->buffer_start < $this->position) {
        $this->push();
    }

    return $this->current;

谢谢


好主意,我在一个项目中使用了@rodneyrehm的代码,并回来查看这个答案。 - Scuzzy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接