A simple dialog system for Sage in natural language

Goal

Develop a command-line tool able to take commands in natural language and have them executed by Sage (an open-source Computer Algebra System 1)

How to use it

Building

You'll need:

  • ghc
  • curl (so that cabal install curl succeeds)
  • a way to call Sage on a terminal (usually sage command. It assumes it's in your PATH)
  • A POSIX system
  • and, of course, GF

Checkout the mathematics grammar library from:

 svn co svn://molto-project.eu/mgl

Go into the sage directory and make it:

cd mgl/sage
make

The first time you make it will fail, asking you to make modifications in the Sage installation. Please refer to the installation page.

Now try to build gfsage again2:

make

The system as been tested in Mac (OS X 10.7) and Linux (Ubuntu).

Usage

Run the tool as:

./gfsage english

giving the input language as argument. It will take some seconds to start the server. After that it will reply with some server information and will show the prompt:

    sage>

You can then enter your query:

    sage> compute the product of the octal number 12 and the binary number 100.
    (3) 40
    answer: it is 40 .

To show that a CAS is actually behind the scene, let's try something symbolic:

    sage> compute the greatest common divisor of x and the product of x and y.
    (4) x
    answer: it is x .

and compare it with:

    sage> compute the greatest common divisor of x and the sum of x and y.
    (5) 1
    answer: it is 1 .

Sage does the right thing in both cases, x and y being unbound numeric variables.

    sage> compute the second iterated derivative of the cosine at pi.
    (6) 1
    answer: it is 1 .

Exiting

Exit the session by issuing CRTL+D: This way the server exits cleanly.

Just another example in a different language:

    ./gfsage spanish
    Login into localhost at port 9000
    Session ID is c1ef10dfd49e4fdb3214fa6d3a3b9c92
    waiting... EmptyBlock 2
    finished handshake. Session is c1ef10dfd49e4fdb3214fa6d3a3b9c92
    sage> calcula la parte imaginaria  de la derivada de la exponencial en pi.
    (4) 0
    answer: es 0 .

More recent examples involving integer literals and integration:

    sage> compute the sum of 1, 2, 3, 4 and 5.
    (3) 15
    answer: it is 15 .
   
    sage> compute the summation of x when x ranges from 1 to 100.
    (4) 5050
    answer: it is 5050 .

    sage> compute the integral of the cosine from 0 to the quotient of pi and 2.
    waiting... (5) 1
    answer: it is 1 .

    sage> compute the integral of the function mapping x to the square root of x from 1 to 2.
    (6) 4/3*sqrt(2) - 2/3
    answer: it is 4 over 3 times the square root of 2 minus the quotient of 2 and 3 .

Other invocation options

Use english:

gfsage      

Use LANGUAGE:

gfsage LANGUAGE

General invocation:

gfsage [OPTIONS]

where OPTIONS are:

short form long form description
-h --help Print usage page
-i LANGUAGE --input-lang=LANGUAGE Make queries in LANGUAGE
-o LANGUAGE --output-lang=LANGUAGE Give answers in LANGUAGE
-V LEVEL --verbose=LEVEL Set the verbosity LEVEL
-t FILE --test=FILE Test samples in FILE
-v[VOICE] --voice[=VOICE] Use voice output. To list voices use ? as VOICE.
-F --with-feedback Restate the query when answering.

Limitations

  • On Darwin (OS X 10.6 and 10.7) a bug in the Sage part makes the system unresponsive after some computations (between 7 and 10)
  • On some machines, it takes time for the Sage server to respond.

This condition is signaled by the message:

gfsage: Connecting CurlCouldntConnect 

I used a Linux virtual machine to reproduce this condition and find that, sometimes, it takes about 10 retries for the server to catch, but then it stays running ok for hours. My guess is that is related to some timeout limit in the server. Killing the orphaned python processes from the previous retries might help too (killall python).


  1. In fact Sage is more a collection of Computer Algebra packages presented in a uniform way. 

  2. All these building operations will ask Sage to "rebuild" itself. Be warned that the first rebuild takes some time. 

Comments

Installation bottleneck

Jordi,

while following the installation procedure, I managed to get Sage up and running, notebook included. However I get

supersoulfighter:sage olgacaprotti$ make
ghc -cpp -DDARWIN --make GfSage.hs -optl"-Wl,-read_only_relocs,suppress" -package gf -package curl -package json  -o gfsage
: cannot satisfy -package curl
    (use -v for more information)
make: *** [gfsage] Error 1

why do you need curl at all? what are you fetching? I have curl installed in /usr/bin/curl - what am i compiling here?

cabal install

Jordi,

I read the code and saw where the curl network library functions are called, so I fixed it by running

supersoulfighter:sage olgacaprotti$ sudo cabal install curl

but then

supersoulfighter:sage olgacaprotti$ make
ghc -cpp -DDARWIN --make GfSage.hs -optl"-Wl,-read_only_relocs,suppress" -package gf -package curl -package json  -o gfsage
[1 of 6] Compiling Voice            ( Voice.hs, Voice.o )
[2 of 6] Compiling Commands         ( Commands.hs, Commands.o )
[3 of 6] Compiling SageSide         ( SageSide.hs, SageSide.o )
[4 of 6] Compiling Transfer         ( Transfer.hs, Transfer.o )
[5 of 6] Compiling GfSide           ( GfSide.hs, GfSide.o )
[6 of 6] Compiling Main             ( GfSage.hs, GfSage.o )
Linking gfsage ...
ld: warning: -read_only_relocs cannot be used with x86_64
ld: warning: could not create compact unwind for _ffi_call_unix64: does not use RBP or RSP based frame
ld: warning: PIE disabled. Absolute addressing (perhaps -mdynamic-no-pic) not allowed in code signed PIE, but used in ___gmpn_modexact_1c_odd from /Library/Frameworks/GHC.framework/Versions/7.0.3-x86_64/usr/lib/ghc-7.0.3/integer-gmp-0.2.0.3/libHSinteger-gmp-0.2.0.3.a(mode1o.o). To fix this warning, don't compile with -mdynamic-no-pic or link with -Wl,-no_pie
make -C nlgf basic
grep -q "sage.nlgf" /Applications/sage/devel/sage/setup.py || (echo "\nPlease add 'sage.nlgf' to /Applications/sage/devel/sage/setup.py.\n"  ; exit 1)

Please add 'sage.nlgf' to /Applications/sage/devel/sage/setup.py.

make[1]: *** [basic] Error 1
make: *** [gfsage] Error 2

It seems to me that setup.py is automatically generated, but from what?

sage.py is auto-generated the

sage.py is auto-generated the 1st time Sage is compiled. It is just that you have to add sage.nlgf to the list of imported modules inside setup.py for Sage to locate our files.

--Jordi Saludes

python.py

Which line exactly? How does it know where to find sage.nlgf?

Sorry - I never used python before.

Sage module tree

Assuming SAGEROOT is the root of your sage installation (you can get it by writing sage -root) setup.py should be inside SAGEROOT/devel/sage and it points to modules that live inside SAGEROOT/devel/sage/sage.

If all goes well there would be SAGEROOT/devel/sage/sage/nlgf directory which contains, at least, prelude.sage and realsets.py.

In setup.py find a line like:

packages = [ 

it defines the list of packages to be imported by Sage as strings delimited by commas. Just insert 'sage.nlgf' among them.

--Jordi Saludes

voice input?

Do you think we could have voice input at all? We used the Google Toolkit for the Travel Phrasebook on Android, so I wonder. That would be great. I tried the MAC shell command "say" with a math expression, maybe I can try with your examples too :)

In fact Jordi, the voice interface would be very closely related to the Java applet that was shown in Trondheim and allowed to dictate LaTeX, do you remember it? It was done at Kingston College by the group of Gordon Hunter.

Voice input

I had very disappointing experiences with voice input, that I'll plan to describe in another page in this book. But perhaps I got the wrong toolkit or maybe I've a really mangled English accent.

Would you mind to point me to this Java applet?

--Jordi Saludes

TalkMaths