Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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.
Posted
Updated 24-Mar-19 21:59pm

1 solution

Those rules are productions (see, for instance Grammars and Parsing[^]). The usual approach to deal with them is recursion (again, see the linked page).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900