PHP在变量中解析句子和存储信息

时间:2018-05-07 13:33:15

标签: php parsing artificial-intelligence

在PHP中,我试图建立一个能够以智能方式识别你所写内容的自然文本识别系统。
我喜欢用一个句子匹配一个'模式'并存储一些信息,例如,如果将schedule a {scheduletype} with {person} at {time}Schedule a meeting with Ann at 3pm进行比较,则PHP代码将创建变量$scheduletype,其值为{,{}}与Ann $person下午3点。如果句子以不同的顺序书写,也可以使其有效,例如'在下午3点与Ann的会面进行会议? 我已经尝试过在谷歌和Stack Exchange上寻找,但遗憾的是没有找到任何东西。

这是我目前的代码:

$time

问题是我的代码没有为$pattern = 'schedule a ([a-zA-Z0-9\-\.\/\?_=&;]*) with ([a-zA-Z0-9\-\.\/\?_=&;]*) at ([a-zA-Z0-9\-\.\/\?_=&;]*)'; $content = 'shedule a meeting with Ann at 3pm'; $match = preg_match($pattern, strtolower($content) , $found); print_r($match); 分配任何内容。

1 个答案:

答案 0 :(得分:1)

您可以使用这种命名的捕获组regex:

#Schedule a (?<scheduletype>\w+)\swith\s(?<person>\w+)\sat\s(?<time>\w+)#

示例:

<?php

$regex= '#Schedule a (?<scheduletype>\w+)\swith\s(?<person>\w+)\sat\s(?<time>\w+)#';
preg_match($regex, 'Schedule a meeting with Ann at 3pm', $matches);

echo $matches['scheduletype']."\n";
echo $matches['person']."\n";
echo $matches['time']."\n";

在这里使用正则表达式:https://regex101.com/r/6QPRsG/1/

在此处播放代码:https://3v4l.org/OBOnY