13

我正在尝试提出一些类似于 Google 日历(甚至是一些 gmail 消息)的内容,其中将解析自由格式的文本并将其转换为特定的日期/时间。

一些示例(为简单起见,假设现在是 2013 年 1 月 1 日凌晨 1 点):

"I should call Mom tomorrow to wish her a happy birthday" -> "tomorrow" = "2013-01-02"
"The super bowl is on Feb 3rd at 6:30pm" -> "Feb 3rd at 6:30" => "2013-02-03T06:30:00Z"
"Remind me to take out the trash on Friday" => "Friday" => "2013-01-04"

首先,我会问这个 - 是否有任何已经存在的开源库(或其中的一部分)。如果不是,你认为我应该采取什么样的方法?

我正在考虑几种不同的可能性:

  1. 很多正则表达式,我可以为每个不同的用例想出尽可能多的正则表达式
  2. 某种贝叶斯网络,它查看 n-gram 并将它们分类为不同的场景,如“相对日期”、“相对星期几”、“特定日期”、“日期和时间”,然后通过规则引擎运行它(也许更多的正则表达式)来确定实际日期。
  3. 将其发送到谷歌搜索并尝试从搜索结果中提取有意义的信息(这个可能不现实)
4

1 回答 1

11

你可以使用这个库:https ://github.com/wanait/chrono

演示:

inputs = ["I should call Mom tomorrow to with her a happy birthday",
"The super bowl is on Feb 3rd at 6:30pm", "Remind me to take out the trash on Friday"];

for(var i = 0; i < inputs.length; i++) {
    var input = inputs[i];
    var parsed = chrono.parse(input);
    console.log(input + " parsed as: " + JSON.stringify(parsed.map(function(p) { return [p.text, p.startDate]; })));
}
​

输出:

I should call Mom tomorrow to with her a happy birthday parsed as: [["tomorrow","2012-12-31T06:30:00.000Z"]]
The super bowl is on Feb 3rd at 6:30pm parsed as: [["Feb 3rd at 6:30pm","2013-02-03T13:00:00.000Z"]]
Remind me to take out the trash on Friday parsed as: [["Friday","2013-01-04T06:30:00.000Z"]] 

http://jsfiddle.net/TXX3Z/

于 2012-12-29T19:05:04.190 回答