Python解析器通过应用给定规则生成字符串。 [英] Python parser to generate strings by applying given rules.

查看:91
本文介绍了Python解析器通过应用给定规则生成字符串。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python解析器通过应用给定规则生成字符串。

以下是500个例子中的6个例子


1)([go]返回|返回)



list = [返回,返回,返回]



2)(AM |中等波浪)帮助

list = [AM帮助,中波帮助]



3)(SiriusXM)[卫星广播|收音机]



list = SiriusXM收音机,SeriusXM卫星收音机



4)(北上[在地图中] [查看]])

list = [北上地图,北上地图视图]



5)((取消|清除)路线|(停止(导航|方向)))

列表 - [取消路线,清除路线,停止导航,停止指示]



6)(停止)(我的家[地址] |我的公寓)[并避免< avoidances> | [via]< route_type> [路线]]

list = [

停在我家,

停在我家的地址,

停在我的公寓,

停在我家,避免<避免>,

停在我家的地址,避免< avoidances>

停在我的公寓,避免< avoidances>,

通过< route_type>停在我家。

通过< route_type>停在我的家庭住址

通过< route_type>停在我的公寓

通过< route_type>停在我家。路线

通过< route_type>停在我的家庭住址路线

通过< route_type>停在我的公寓路线

通过路线在我家停车

在我的家乡地址路线停车

停在我的公寓路线

抱在我的家里,

在我的家庭住址,

在我的公寓里举行,

在我家停留并避免< avoidances> ,

保留在我的家庭住址,并避免<避免>

在我的公寓里举行并避免< avoidances>,

持有我的通过< route_type>回家

通过< route_type>在我的家庭住址处保留

通过< route_type>保存在我的公寓

通过< route_type>保存在我的家中路线

通过< route_type>保存在我的家庭住址路线

通过< route_type>保持在我的公寓路线

通过路线在我家停留

在我的家庭住址路线保持

在我的公寓路线保持

]

7)< app_name>

list = [< app_name>]

8)退出< app_name>

list = [exit< app_name>]

9)< app_name>帮助

list = [< app_name>帮助]



10)(select | choose | pick | take)([(line | row)[number] | number | option | entry]< cardinal_number> |(< line_position> |< ordinal_number>)[(line | row)[number] | number | option | entry])

list = [

选择行号< cardinal_numbers>

选择< ordinal_number>行

选择< ordinal_number> line

选择< line_position>行

和..so on ..]



生成句子或字符串的规则:

1) '[]'下的任何内容都是期权

2)'()'下的任何内容都是强制性的,

3)围绕'|'是OR而rest是具体文本

4)'<>'下的任何东西被视为混凝土

5)基数和序数文本被视为普通文本



我尝试过:



当前版本的解析器:

1)我开始计算:

字数,

数字或'|'

强制数'()'

选项数'[]'

混凝土数量:特殊字符之间的文字。



然后放入它们在以下情况下:

如果#words是> 5然后执行此操作

如果强制性小于1则执行此操作

如果强制性= 2且选项= = 2则执行此操作`



但是这些只有6个例子,我的日期超过500个,我的解析不能满足所有。



so想知道是否有更好的解决方案,你可以建议,谢谢。

python parser to generate strings by applying given rules.
following are 6 examples out of 500

1) ([go] back | return)

list = [ go back, back, return]

2) (AM | Medium Wave) help
list = [AM help, Medium Wave help]

3) (SiriusXM) [satellite radio | radio]

list = SiriusXM radio, SeriusXM Satellite radio

4) (north up [in map [view]])
list = [north up in map , north up in map view ]

5) ((cancel | clear) route | (stop (navigation | directions)))
list - [cancel route, clear route, stop navigations, stop directions]

6) (stop | hold) at (my home [address] | my apartment) [and avoid <avoidances> | [via] <route_type> [route]]
list = [
stop at my home,
stop at my home address,
stop at my appartment,
stop at my home and avoid <avoidances>,
stop at my home address and avoid <avoidances>
stop at my appartment and avoid <avoidances>,
stop at my home via <route_type>
stop at my home address via <route_type>
stop at my appartment via <route_type>
stop at my home via <route_type> route
stop at my home address via <route_type> route
stop at my appartment via <route_type> route
stop at my home via route
stop at my home address route
stop at my appartment route
hold at my home,
hold at my home address,
hold at my appartment,
hold at my home and avoid <avoidances>,
hold at my home address and avoid <avoidances>
hold at my appartment and avoid <avoidances>,
hold at my home via <route_type>
hold at my home address via <route_type>
hold at my appartment via <route_type>
hold at my home via <route_type> route
hold at my home address via <route_type> route
hold at my appartment via <route_type> route
hold at my home via route
hold at my home address route
hold at my appartment route
]
7) <app_name>
list = [<app_name>]
8) exit <app_name>
list = [exit <app_name>]
9) <app_name> help
list = [<app_name> help]

10) (select | choose | pick | take) ([(line | row) [number] | number | option | entry] <cardinal_number> | the (<line_position> | <ordinal_number>) [(line | row) [number] | number | option | entry])
list = [
Choose line number <cardinal_numbers>
Pick the <ordinal_number> row
Select the <ordinal_number> line
Select <line_position> row
and ..so on.. ]

Rules for generating sentences or strings:
1) anything under '[]' is optionals
2) anything under '()' is mandatory and
3) around '|' is OR and rest is concrete text
4) anything under '<>' treated as concretes
5) cardinal number and ordinal number text is treated as normal text

What I have tried:

current version of the parser :
1) I started with calculating :
number of words,
number of or ' |'
number of mandatory '()'
number of optionals' []'
number of concretes : text that is between special characters.

and then putting them in conditions like :
if # words are > 5 then do this
if mandatory are less than 1 then do this
if mandatory = 2 and optionals ==2 then do this `

but these are only 6 examples and i have really big sheet of date more than 500 and my parses can not satisfy all .

so wondering if there is better solutions you can suggest, and thanks.

推荐答案

这些规则是制作(参见,例如Grammars and Parsing [ ^ ])。处理它们的常用方法是递归(再次,请参阅链接页面)。
Those rules are productions (see, for instance Grammars and Parsing[^]). The usual approach to deal with them is recursion (again, see the linked page).


这篇关于Python解析器通过应用给定规则生成字符串。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆