根据不同的条件将字符串解析为自定义对象
作为我正在开发的一个小项目的一部分,我需要能够将字符串解析为自定义对象,该对象表示操作、日期和一些其他属性。棘手的部分是输入字符串可以有多种风格,所有这些都需要正确解析。
输入字符串可能采用以下格式:
明天上午 9 点上班
- 周一下午 3 点洗车 下周二
- 上午 10 点打电话给医生
- 3 天内下午 2:30 填写回扣表格
- 每天 7 点叫醒我:00am
输出对象看起来像这样:
{
"Action":"Wash my car",
"DateTime":"2011-12-26 3:00PM", // Format is irrelevant at this point
"Recurring":False,
"RecurranceType":""
}
首先,我想到构建某种树来表示不同的状态(On、In、Every 等),具有不同的结果和进一步的状态(状态的候选状态)机,对吧?)。然而,我越想这个问题,就越觉得它像是一个语法解析问题。由于句子的形成方式有限,看起来需要实现某种语法解析算法。
另外,我是在前端执行此操作,因此 JavaScript 是这里选择的语言。后端将用 Python 编写,如有必要,可以通过调用 AJAX 方法来使用,但我更愿意将其全部保留在 JavaScript 中。 (说实话,我不认为语言在这里是一个大问题)。
那么,我是不是有点超出我的能力范围了?我有很强的 JavaScript 背景,但在语言设计、解析等方面没有超出学校课程的范围。有没有更好的方法来解决这个问题?非常感谢任何建议。
As part of a small project I'm working on, I need to be able to parse a string into a custom object, which represents an action, date and a few other properties. The tricky part is that the input string can come in a variety of flavors that all need to be properly parsed.
Input strings may be in the following formats:
Go to work tomorrow at 9am
- Wash my car on Monday, at 3 pm
- Call the doctor next Tuesday at 10am
- Fill out the rebate form in 3 days at 2:30pm
- Wake me up every day at 7:00am
And the output object would look something like this:
{
"Action":"Wash my car",
"DateTime":"2011-12-26 3:00PM", // Format is irrelevant at this point
"Recurring":False,
"RecurranceType":""
}
At first I thought of constructing some sort of tree to represent different states (On, In, Every, etc.) with different outcomes and further states (candidate for a state machine, right?). However, the more I thought about this, the more it started looking like a grammar parsing problem. Due to a (limited) number of ways the sentence could be formed, it looks like some sort of grammar parsing algorithm would need to be implemented.
In addition, I'm doing this on the front end, so JavaScript is the language of choice here. Back end will be written in Python and could be used by calling AJAX methods, if necessary, but I'd prefer to keep it all in JavaScript. (To be honest, I don't think the language is a big issue here).
So, am I in way over my head? I have a strong JavaScript background, but nothing beyond school courses when it comes to language design, parsing, etc. Is there a better way to solve this problem? Any suggestions are greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对语法分析了解不多,但这里的一些内容可能会有所帮助。
我的第一个想法是,你的句子语法似乎非常一致,
第一个 3-4 个单词通常是动词文本名词,后面跟着某种形式的时间。如果总选项仅限于句子可以采用的形式,您可以硬编码一些解析规则。
我还遇到了一些 js 语法解析器,它们可能会帮助你:
http://jscc.jmksf.com/
http://pegjs.majda.cz/
http://www.corion.net/perl-dev/Javascript-Grammar.html
这是你遇到的一个有趣的问题。请稍后用您的解决方案更新此内容。
I don't know a lot about grammar parsing, but something here might help.
My first thought is that your sentence syntax seems to be pretty consistent
1st 3-4 words are generally VERB text NOUN, followed by some form of time. If the total options are limited to what form the sentence can take, you can hard-code some parsing rules.
I also ran across a couple of js grammar parsers that might get you somewhere:
http://jscc.jmksf.com/
http://pegjs.majda.cz/
http://www.corion.net/perl-dev/Javascript-Grammar.html
This is an interesting problem you have. Please update this with your solutions later.