strict warning: Only variables should be passed by reference in /home/local/www/ on line 559.


Using natural language inside Sage

By defining new Sage interfaces we can command the Sage shell and notebook server using natural language.


Move to the sage directory and build sage-shell:

cd mgl/sage
make sage-shell

The first time you build it, you may run into a warning as in the installation section of the front page, or:

Please add nlgf components to the interfaces list in /usr/local/sage-4.7.2/devel/sage/sage/interfaces/

We must inform Sage that there are some new interfaces for it: We open interfaces/ (Notice that your actual path might be different), go to the end of the file and add something like this:

from nlgf import english, spanish
interfaces.extend(['english', 'spanish'])

The first line asks the system to load the interfaces for commanding Sage using English and Spanish. The next line add these to the list of available interfaces.

Now retry building:

make sage-shell

At the time of writing, the module nlgf provides catalan, english, german, and spanish interfaces.

Sage shell with command auto-completion

In some systems you can have the commands Sage shell auto-completed by pressing the tab key. This is experimental and you have to make the installation completely by hand.

First you have to build the Python bindings for GF which, for the moment, only work in Linux. You'll find there a shared library called Copy or move it into one of the directories that Python scans when resolving imports. Note that it may be the case that the Python instance run by Sage be different of the one your machine runs by default; To be sure, do as follows:

sage -python -c 'import sys; print sys.path'

it will list all the directories that Sage/python scans.

You'll know it's all right when:

sage -python -c 'import gf'

exits with no complain: The next time you enter into the Sage shell you'll have autocompletion for the GF interfaces.


Shell interface

Start a Sage shell:


and switch to one of the defined natural language interfaces:

sage: %english

will reply with:

--> Switching to Gf <-- 

If you didn't install autocompletion (which is the usual case, auto-completion being experimental), a warning will appear:

No autocompletion available

Now you're ready to issue sage commands in English:

english: compute the summation of x when x ranges from 1 to 100.
english: add 3 to it.
english: let x be the factorial of 6.
english: let y be the factorial of 5.
english: compute the greatest common divisor of x and y.
english: compute the least common multiple of x and y.

Go back to the standard interface by typing ctrl+D or typing quit.

Notebook interface

Sage has a notebook interface that gives a more flexible way to interact with it. To use it, start the shell as above and then:

sage: notebook(secure=true, interface='')
The notebook files are stored in: sage_notebook.sagenb
*                                                  *
* Open your web browser to https://localhost:8000  *
*                                                  *
There is an admin account.  If you do not remember the password,
quit the notebook and type notebook(reset=True).
2012-02-13 12:48:19+0100 [-] Log opened.

In some systems a browser will open simultaneously. Now you can use Sage from the browser.

Click on New Worksheet. You'll be asked to rename the worksheet (this is optional). A single cell will be ready for your input. Write your command and press evaluate. Notice that a cell can contain more than one command, separated by newlines.

Start a new cell by writing:


and add one or more new lines with commands in English.

sage-notebook.jpg95.69 KB