php - 在 PHP 中解析日期字符串

标签 php datetime parsing nlp

给定一个任意字符串,例如(“下周五我要去玩槌球”“Gadzooks,已经是 6 月 17 日了吗?”),如何你会从那里提取日期吗?

如果这看起来像是太硬篮子的一个很好的候选者,也许你可以建议一个替代方案。我希望能够解析 Twitter 消息中的日期。我要查看的推文将是用户针对此服务的推文,因此可以指导他们使用更简单的格式,但我希望它尽可能透明。您能想到一个好的中间立场吗?

最佳答案

如果您有能力,可以尝试以下算法。我正在展示一个例子,把繁琐的工作留给你:)

//Attempt to perform strtotime() on each contiguous subset of words...

//1st iteration
strtotime("Gadzooks, is it 17th June already")
strtotime("is it 17th June already")
strtotime("it 17th June already")
strtotime("17th June already")
strtotime("June already")
strtotime("already")

//2nd iteration
strtotime("Gadzooks, is it 17th June")
strtotime("is it 17th June")
strtotime("17th June") //date!
strtotime("June") //date!

//3rd iteration
strtotime("Gadzooks, is it 17th")
strtotime("is it 17th")
strtotime("it 17th")
strtotime("17th") //date!

//4th iteration
strtotime("Gadzooks, is it")
//etc

而且我们可以假设 strtotime("17th June")strtotime("17th") 更准确,因为它包含更多的词...即 "下周五”总是比“周五”更准确。

关于php - 在 PHP 中解析日期字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3053580/

相关文章:

ios - Swift - 解析嵌套 JSON 对象中的数组不起作用

javascript - 如何检查 USERNAME 是否已经存在于 PHP/MYSQL 中?

php - 在日期时间 DST 重叠期间选择首选偏移量

java - 将日期时间字符串从一个时区 A 转换为时区 B,其中 jvm 在时区 C 中运行

python - 如何选择多列和行

linux - 解析 Linux 命令输出

php - PHP中单引号和双引号字符串有什么区别?

php - PHP sprintf 中使用的该格式字符串的解释

php - 如何从mysql中获取数据并存储在android中的Sqlite数据库中

c++ - 使用用户定义的过滤器/运行时表达式评估来过滤对象