PHP 和 NLP:嵌套括号(解析器输出)到数组?

2024-05-17

想要将带有嵌套括号的文本转换为嵌套数组。以下是 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

etc.

找到了这个php 大括号放入数组 https://stackoverflow.com/questions/2650414/php-curly-braces-into-array但这不是嵌套数组


通过代码解释:

<?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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PHP 和 NLP:嵌套括号(解析器输出)到数组? 的相关文章

  • 如何在 Laravel 中创建一条包罗万象的路线

    我需要一个 Laravelroutes php将捕获所有流量到特定的条目example com premium section网站 以便我可以提示人们在访问优质内容之前成为会员 您还可以通过在参数上使用正则表达式来捕获 全部 Route g
  • 通过jquery传递搜索参数

    我有一个表单 如果用户输入搜索查询 其参数应通过 jquery 传递 并在获取结果后将结果加载到 div 容器中 由于我不太熟悉 jquery 我该怎么做 html currently the data is being displayed
  • PHP mail() 函数不发送邮件

    我有一个小问题 我正在使用一个工作脚本 在我的测试帐户 共享服务器上工作 使用 mail 函数通过 PHP 发送邮件 我刚刚得到了一个专用服务器 但我还无法让该功能发挥作用 在过去的 10 个小时左右的时间里 我阅读了有关 BIND 用于
  • 我如何知道请求是否来自 flash swf?

    我有一个用 flash 开发的应用程序 我需要访问一些 php 文件 因此 如果访问来自 swf 则 php 文件会返回一些数据 如何判断请求是否来自Flash 无需将 get post 变量传递给 php 可能是用户代理 推荐人 请记住
  • 为什么当尝试使用 $this->data 访问 CakePHP 时,密码字段为空?

    我正在实现一个身份验证组件 这是我的注册页面 create User array action gt login echo form gt input primary email array size gt 32 echo form gt
  • PHP上传文件产生错误代码4。为什么?

    HTML
  • 使用 PHP/COM/ADSI/LDAP 更改 AD 密码

    我已经被这个问题困扰了好几天了 我尝试了各种解决方案均无济于事 请帮忙 Problem 我们有两个域控制器 它们不属于我们的管理范围 我们能够通过端口 389 上的 LDAP 进行连接 但无法通过端口 636 安全连接 我们正在开发一个系统
  • 访问 public_html 级别之外/以下的文件

    如何通过 url 访问文件 home uzair etc index php 即使我运行域 something com 它显示了 home uzair public html index php 这个文件 任何人请帮助我如何访问放置在 ho
  • PHP:如何防止不必要的换行

    我正在使用 PHP 创建一些基本的 HTML 标签始终相同 但实际链接 标题对应于 PHP 变量 string p a href strong i title i i strong a br echo string fwrite outfi
  • Node.js 进行 rsa 加密的正确方法?

    我正在尝试创建一个 WS 来发出肥皂请求 在消息正文中有一个包含加密文本的字段 我有公钥来加密文本 但我获得的唯一结果是文本无法识别 我使用节点的加密模块来发出请求 并且文本已加密 但我不知道为什么没有正确加密 PS我用 openssl p
  • 如何在无法重启的服务器(Apache)上使用gettext?

    我在服务器故障上问了这个问题 https serverfault com questions 104224 how do you use gettext on server apache you cant restart但我没有得到任何回应
  • 根据通过 AJAX 请求的用户输入重绘 google 图表

    我有一个谷歌图表从我的数据库中提取数据 它可以按我想要的方式工作 根据 URL 中的 get 请求 它从所选表中提取数据 我想根据下拉菜单中选定的表通过 ajax 更新此图表 我无法突破的部分是通过 ajax 获取数据响应 我认为下面的代码
  • DOMPDF - 未找到“字体”类

    我正在尝试通过命令行添加字体 每次我尝试都会收到以下错误 Fatal error Class Font not found in home scripts public html MarketingPalv2 load font php o
  • PHP 日志文件颜色

    我正在编写一个 PHP 日志文件类 但我想为写入文件的行添加颜色 我遇到的问题是颜色也会改变终端的颜色 我想要实现的是仅更改写入日志文件的行的颜色 class logClass extends Singleton private funct
  • Laravel 5.3:护照实现 - {“error”:“invalid_client”,“message”:“客户端身份验证失败”}

    我按照中提到的确切步骤进行操作Laracast Laravel 5 3 的新增功能 Laravel Passport https laracasts com series whats new in laravel 5 3 episodes
  • PHP 时间间隔

    我正在寻找一个看起来应该非常简单的解决方案 但似乎我不能在这里找到任何好的答案 而且我自己似乎无法让它发挥作用 我正在寻找的是设置开始时间 结束时间 然后迭代给定时间间隔之间的一组时间 例如 上午 9 00 下午 5 00 是开始时间 这些
  • 隐藏产品价格和添加到购物车按钮,但不隐藏 WooCommerce 中未注册用户的变体

    在我的 WooCommerce 商店中 我想隐藏价格 直到客户登录为止 我有以下代码可以实现这一点 add action init hide price function hide price if is user logged in re
  • Laravel - 覆盖模型 ID

    我正在开发电子书管理系统 我使用 UUID 而不是自动递增整数主键 它工作得很好 protected static function boot parent boot static creating function model model
  • 无法与站点通信以检查致命错误

    无法与站点通信以检查致命错误 因此 PHP 更改已恢复 您需要通过其他方式上传 PHP 文件更改 例如使用 SFTP 有什么解决办法 我正在 WordPress 中编辑头文件 遇到这个问题 尝试这个 我有同样的问题并决定调查一下 更改 wp
  • php56 - CentOS - Remi 仓库

    我刚刚在测试盒上安装了 php 5 6 正常的 cli php 解释器似乎不存在 gt php v bash php command not found gt php56 v PHP 5 6 13 cli built Sep 3 2015

随机推荐