473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

module to parse "pseudo natural" language?

Hi all

I've written a python program that adds orders into our order routing
simulation system. It works well, and has a syntax along these lines:

./neworder --instrument NOKIA --size 23 --price MARKET --repeats 20

etc

However, I'd like to add a mode that will handle, say:

./neworder buy 23 NOKIA at MKT x 20

I could enter several orders either by running multiple times, or use a
comma-separated approach, like:

./neworder buy 23 NOKIA at MKT on helsinki, sell 20 NOKIA at market on
helsinki

The thing about this is that its a "tolerant" parser, so all of these
should also work:

# omit words like "at", "on"
./neworder buy 23 NOKIA mkt helsinki

# take any symbol for helsinki
./neworder buy 23 mkt helsinki

# figure out that market=helsinki
./neworder buy 23 NOKIA at market price
I've started writing a simple state-based parser, usage like:

class NaturalLanguageInsructionBuilder:

def parse( self, arglist ):
"""Given a sequence of args, return an Instruction object"""
...
return Instruction( instrument, size, price, ... )
class Instruction:
"""encapsulate a single instruction to buy, sell, etc"""

def __init__( self, instrument, size, price, ... ):
...
This doesn't work yet, but I know with time I'll get there.

Question is - is there a module out there that will already handle this
approach?

Thanks for any suggestions :)

Andrew
Jul 19 '05 #1
4 2156
>
Question is - is there a module out there that will already handle this
approach?


Try the python nltk. I haven't done stuff with it so far, but I'd certainly
give it a try when I need natural language support.
http://nltk.sourceforge.net/

--
Regards,

Diez B. Roggisch
Jul 19 '05 #2
Le Sun, 17 Apr 2005 13:38:09 +0200, Andrew E a écrit :
Hi all

I've written a python program that adds orders into our order routing
simulation system. It works well, and has a syntax along these lines:

./neworder --instrument NOKIA --size 23 --price MARKET --repeats 20

etc

However, I'd like to add a mode that will handle, say:

./neworder buy 23 NOKIA at MKT x 20

I could enter several orders either by running multiple times, or use a
comma-separated approach, like:

./neworder buy 23 NOKIA at MKT on helsinki, sell 20 NOKIA at market on
helsinki It would be simpler to have the rule : one line == one order, action,
command wahtever.
The thing about this is that its a "tolerant" parser, so all of these
should also work:

# omit words like "at", "on"
./neworder buy 23 NOKIA mkt helsinki

# take any symbol for helsinki
./neworder buy 23 mkt helsinki

# figure out that market=helsinki
./neworder buy 23 NOKIA at market price You have some variation of the same « buy » order. If you want to detail
the semantics : you arrive to determine the different parameter of a
BuyCommand : what, where, how (many) ...
This semantic is expressed differently in english, US-english, french
...
If you base your parser on regex you see that the same BuyOrder is
represented by a number of variant :
Please, buy 23 NOKIA at market Helsinki
Acheter 40 actions Bouygues à la Bouse de Paris

So you have for a given environment (orders management module) a number
of commands expressed in pseudo-natural languages. Each command has a
certain number of attributes
name, module_name, parameters, variants(list of regexps for instance),
description, short description, sample sentence (to be presented
to the user in an interactive session if requested).
An important attribute is naturally the action taken when the
parser/interpreter matches the current line.

So you group the description of the Commands in an easy to parse file
and write an interpreter which on startup read such a file for the
Commands it understands : command to load a module, to list the commands
in the currently loaded module or in the interpreter, command to display
the sample, short or long description (help about cmd or how to spell
cmd)., command to stop the interpreter (and the program) ...
command to read/include a file containings commands to parse and
execute.
Python is dynamic, with setattr() you can easily construct a Command
object from its description.

Here is what could be the description of a command to compute the
equilibrium position in a stability module (using a «here» shell like
syntax ):
command equilibre << eoc
description << eod
La commande « equilibre » permet de lancer le calcul de la position
d'équilibre du flotteur couramment décrit.
position equilibre en partant de pqr 0.0/0.0/1.0 et h=8.500
les mots-clés sont equilibre pqr et h[=]
eod
sample <<eos
equilibre pqr 0.0/0.0/1.0 avec h=8.500
eos
# name of function to call
action compute_equi
# parameters of the command (a dictionary of parameters could be in
# action's arguments
parameters << eop
pqr
h
eop # The regexpes
variants << eov
# the following regexp is with re.I (ignore case flag) :
variant I << eovi
equilibrer?\spqr\s+(?P<pqr>\d*\.\d*/\d*\.\d*/\d*\.\d*)\s+(?P<h>\d*\.\d*)
eovvi
# syntax of regexp untested :-)
variant << eovi
equilibrium\spqr\s+(?P<pqr>\d*\.\d*/\d*\.\d*/\d*\.\d*)\s+(?P<h>\d*\.\d*)
eovvi
eov
eoc
Thanks for any suggestions :)

Andrew

Jul 19 '05 #3
"Andrew E" <an****@nospam.com> wrote in message
news:d3**********@news.hispeed.ch...
Hi all

I've written a python program that adds orders into our order routing
simulation system. It works well, and has a syntax along these lines:

./neworder --instrument NOKIA --size 23 --price MARKET --repeats 20

etc

However, I'd like to add a mode that will handle, say:

./neworder buy 23 NOKIA at MKT x 20

I could enter several orders either by running multiple times, or use a
comma-separated approach, like:

./neworder buy 23 NOKIA at MKT on helsinki, sell 20 NOKIA at market on
helsinki

The thing about this is that its a "tolerant" parser, so all of these
should also work:

# omit words like "at", "on"
./neworder buy 23 NOKIA mkt helsinki

# take any symbol for helsinki
./neworder buy 23 mkt helsinki

# figure out that market=helsinki
./neworder buy 23 NOKIA at market price
I've started writing a simple state-based parser, usage like:

class NaturalLanguageInsructionBuilder:

def parse( self, arglist ):
"""Given a sequence of args, return an Instruction object"""
...
return Instruction( instrument, size, price, ... )
class Instruction:
"""encapsulate a single instruction to buy, sell, etc"""

def __init__( self, instrument, size, price, ... ):
...
This doesn't work yet, but I know with time I'll get there.

Question is - is there a module out there that will already handle this
approach?

Thanks for any suggestions :)
There's NLTK (on Sourceforge) which has already been mentioned.
However, it's a teaching tool, not a real production natural language
parser.

I'd suggest you step back from the problem and take a wider
view. Parsing natural language, in all its variations, is an unsolved
research problem that is part of what has given Artificial Intelligence
somewhat of a black eye.

Your problem is, however, much simpler than the general one:
you've got a limited number of commands which pretty much
all follow the VO (verb operands) pattern.

You've also got a lot of words from limited and disjunct
vocabularies that can be used to drive the parse. In your example,
at least one of 'buy' and 'sell' is required to start a clause,
MKT is one of maybe a half dozen
qualifiers that specify other information that must be present,
there are a limited number of exchanges, and the number of
shares seems to be the only number present.

I'd also take a bit of advice from the XP community: don't
write the library first, wait until you've got at least three
working examples so you know the services that the
library really needs to support.

John Roth
Andrew


Jul 19 '05 #4
Andrew E wrote:
Hi all

I've written a python program that adds orders into our order routing
simulation system. It works well, and has a syntax along these lines:

./neworder --instrument NOKIA --size 23 --price MARKET --repeats 20

etc

However, I'd like to add a mode that will handle, say:

./neworder buy 23 NOKIA at MKT x 20

I could enter several orders either by running multiple times, or use a comma-separated approach, like:

./neworder buy 23 NOKIA at MKT on helsinki, sell 20 NOKIA at market on helsinki
You could add a string option instead:
$ neworder -c 'buy 23 NOKIA at MKT on helsinki, sell 20 NOKIA at market
on helsinki'

This would leave your current option parsing in tact. Then just
split on the comma.

Another suggestion would be to drop into an interactive mode if
no arguments are passed:
$ neworder
->? buy 23 NOKIA at MKT on helsinki
->? sell 20 NOKIA at market on helsinki
->? ^d
The thing about this is that its a "tolerant" parser, so all of these
should also work:

# omit words like "at", "on"
./neworder buy 23 NOKIA mkt helsinki

# take any symbol for helsinki
./neworder buy 23 mkt helsinki

# figure out that market=helsinki
./neworder buy 23 NOKIA at market price
I've started writing a simple state-based parser, usage like:

class NaturalLanguageInsructionBuilder:

def parse( self, arglist ):
"""Given a sequence of args, return an Instruction object"""
...
return Instruction( instrument, size, price, ... )
class Instruction:
"""encapsulate a single instruction to buy, sell, etc"""

def __init__( self, instrument, size, price, ... ):
...
This doesn't work yet, but I know with time I'll get there.

Question is - is there a module out there that will already handle this approach?

Thanks for any suggestions :)

Andrew


If I were in your situation, I'd probably write a BNF for the
tiny-language. This would help wrap my brain around the problem.
The BNF would help show what kind of regular expression you are
looking at creating as well.

--
bytecolor

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Zhang Weiwu | last post by:
Hello. I am working with a php software project, in it (www.egroupware.org) Chinese simplified locate is "zh" while Traditional Chinese "tw". I wish to send correct language attribute in http...
11
by: anon | last post by:
Dear Microsoft: In your new ASP.NET 2.0 page model of being able to crosspost to another page, it would seem that if you were to type that word in Google, "crosspost" or "cross post", you will...
1
by: Lars Erik Thorsplass | last post by:
I have recently ventured into the exciting world of stored procedures, but I have now become lost. Background: Am currently working on access control in a web application. My goal is to...
10
by: Paul McGuire | last post by:
I just published my first article on ONLamp, a beginner's walkthrough for pyparsing. Please check it out at http://www.onlamp.com/pub/a/python/2006/01/26/pyparsing.html, and be sure to post any...
9
by: Andrew Gwozdziewycz | last post by:
I've been looking recently for date processing modules that can parse dates in forms such as "next week" or "6 days from next sunday". PHP has a function that will do this called 'strtotime',...
3
by: jonnyblazed | last post by:
I've searched everywhere for this but I'm not sure exactly what to search for. What I am trying to do is pass a variable to a php script via the URL. However, I don't want to use the standard...
4
by: Shelly | last post by:
I am coding a psudo-cron job into an application that I am writing. I don't have access to write a real cron job. Here is what I did: 1 - I downloaded pseudocron.php from its website...
7
by: Daniel Fetchinson | last post by:
Many times a more user friendly date format is convenient than the pure date and time. For example for a date that is yesterday I would like to see "yesterday" instead of the date itself. And for...
4
by: k3pp0 | last post by:
Hey. I've got a very basic newbie question, I hope you can understand me. Sorry for asking it, first of all. I see a lot of sites (e.g. communities) with a url-structure like this:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.