Command Parsing

29 views
Skip to first unread message

da...@nautoguide.com

unread,
Feb 28, 2018, 2:41:29 PM2/28/18
to PEG.js: Parser Generator for JavaScript
I am looking to build a simple parser to find commands such as:-

"where am i"
"what is my location"
"find where i am"

These could be in longer sentence such as

"help me find where i am"
"please could you tell me my location"
"what is my current position"

I was hoping to achieve this with:-

location_request = "am i"/"i am"/"my location"/"location"/"position"
location_predicate = "where"/"what"/"find"/"tell"

And a top level rule that says:-

<optional stuff> +location_predicate + <option stuff> + location_request + <optional stuff>

I am not sure how to define the <optional stuff> to ignore the location_request and location_predicate rules

Can this be done?

TIA
Dave

Samuel Crow

unread,
Mar 1, 2018, 5:02:19 AM3/1/18
to da...@nautoguide.com, PEG.js: Parser Generator for JavaScript
Natural language processing is possible but I've heard that other parser generators may be better suited to it than PEGs.  The terms "natural language parser" are good search terms though.

Futago-za Ryuu

unread,
Mar 2, 2018, 7:11:40 AM3/2/18
to PEG.js: Parser Generator for JavaScript
Samuel is right that in most cases of parsing natrual languages, PEG.js might not be the right tool to use, but it is possible:

start
 
= _ predicate:LocationPredicate _ request:LocationRequest _ {
     
return { predicate, request };
   
}

LocationRequest "Request"

 
= "am i"
 
/ "i am"
 
/ "my location"
 
/ "location"
 
/ "position"

LocationPredicate "Predicate"

 
= "where"
 
/ "what"
 
/ "find"
 
/ "tell"


_
"Optional Stuff"
 
= (!(LocationRequest / LocationPredicate) .)*

You can test the above example at: https://pegjs.org/online

Reply all
Reply to author
Forward
0 new messages