Using Wolfram|Alpha from the command line

Wolfram|Alpha ?

If you don’t know this famous tool, just go visit the main page: www.wolframalpha.com/.

Thanks to Romain Vernoux for introducing this tool to me!

  • Wolfram|Alpha is © and ® of the Wolfram Research Company;

  • wolf (ruby cli) is © Gabriel Horner;

  • wolfram (ruby cli) is also © Gabril Horner.

  • ruby and nokogiri are the property of their respective authors.


wa.sh: a first cli client for Wolfram|Alpha, lightweight and in Bash?

Advantages and drawbacks

The advantage of this first tool is its simplicity: it is very tiny (47 lines of code, avec comments!), and does not have any dependency, except usual GNU tools grep, curl, tr and sed, already installed on any Linux distribution, and available on Mac OS X (with brew) and on Windows (with cygwin).

However, as this tool is quite simple and minimalist, it is much simpler than wolf (presented below), and does not offer any option.

Source

The original version is here wa.sh (original).

My version, without any colors in the output, is available here wa_nocolor.sh. And an up-to-date version is available here wa.sh.

Obtaining an application key

For more details, see below.

You have to save your application key in a file named ~/.wolfram_api_key (in your $HOME) with this form :

export API_KEY="3HHP2W-UUPQUT6997"

For instance with this bash command:

mv ~/.wolfram_api_key /tmp/  # To not erase a file that should already be there!
echo 'export API_KEY="3HHP2W-UUPQUT6997"' > ~/.wolfram_api_key
# Then make sure the file has the good reading permission for the current user:
chmod +r ~/.wolfram_api_key

Examples

Here are some examples, to compare the two cli tools (cf. below for examples with wolf) :

  1. A dummy test: 0+0 :

    $ wa_nocolor.sh "0+0"
    ## Input
    0 + 0
    ## Result
    0
    ## Number line
    ## Number name
    zero
    
  2. Computing a distance:

    $ wa_nocolor.sh "distance Paris Hyderabad"
    ## Input interpretation
    distance | from | Paris, Ile-de-France
    to | Hyderabad, Telangana, India
    ## Result
    7563 km (kilometers)
    ## Unit conversions
    4699 miles
    7563 km (kilometers)
    7.563×10^6 meters
    4084 nmi (nautical miles)
    ## Direct travel times
    aircraft (550 mph) | 8 hours 35 minutes
    sound | 6 hours 10 minutes
    light in fiber | 35 ms (milliseconds)
    light in vacuum | 25 ms (milliseconds)
    (assuming constant-speed great-circle path)
    ## Map
    ## Fraction of Earth circumference
    0.19 ≈ 1 / 5
    
  3. Solving a numerical equation:

    $ wa_nocolor.sh "x^3 - sinx = e^-x"
    ## Input
    x^3 - sin(x) = e^(-x)
    ## Plot
    ## Numerical solution
    x ≈ 1.06853181316479...
    
  4. And a funnier example, a Pokémon :

    $ wa_nocolor.sh pikachu | sed 's/\\#/#/g'
    ## Input interpretation
    Pikachu
    ## Basic properties
    name | Pikachu
    Japanese name | ピカチュウ (Pikachu)
    Pokédex number | 25
    type | Electric
    Pokédex color | yellow
    generation | Generation I
    icon | 
    footprint | 
    ## Physical characteristics
    species | Mouse Pokémon
    height | 0.41 m
    weight | 5.99 kg
    proportion of females | 50%
    proportion of males | 50%
    evolves from | Pichu
    evolves into | Raichu
    natural abilities | Static
    Dream World abilities | Lightning Rod
    ## Base statistics
    hit points | 35
    attack | 55
    defense | 40
    special attack | 50
    special defense | 50
    speed | 90
    total stats | 320
    (statistics vary based on level, effort values and individual values)
    ## Image
    ## Popular curve
    ## Wikipedia summary
    Pikachu is a species of Pokémon, fictional creatures that appear in an assortment of media of the Pokémon franchise by The Pokémon Company. Pikachu is a yellow mouse-like Pokémon with powerful electrical abilities. In most vocalized appearances, including the anime and certain video games, it is primarily voiced by Ikue Ōtani. A Pikachu also appears as part of the main cast in the live-action animated film Pokémon Detective Pikachu, played in CGI and voiced by Ryan Reynolds.
    
  5. We can ask the current exchange rate between Euro (€) and the Indian rupee:

    $ echo -e "Request to Wolfram|Alpha being processed…"
    $ echo -e "The $(date), 1 € was worth $(wa_nocolor.sh "1 EUR in INR" | grep -o "₹.*$")."
    Request to Wolfram|Alpha being processed…
    The mer. 25 août 2021 12:53:26 CEST, 1 € was worth ₹87.06 (Indian rupees) (24/08/2021)
    ₹85.48 (26/03/2021 | 5 months ago)
    ₹90.60 (21/04/2021 | 4 months ago)
    ₹88.06 (annualized volatility: 5.7%).
    

Warning

One could complain about the impressive slowness of this solution. Some requests can take up-to a few seconds, it’s not that quick!


wolf: a second client for Wolfram|Alpha, in ruby ?

The main page is github.com/cldwalker/wolf.

It is also a command line client tool for the Wolfram|Alpha website.

ruby1.9.1

To install wolf on Ubuntu 11.10 (or more recent), you have to install the packets ruby1.9.1 and ruby1.9.1-dev :

sudo apt-get install ruby1.9.1 ruby1.9.1-dev

Avertissement

Ruby 1.9.1 is not available anymore from Ubuntu 15.10 !

So I can no longer test for real the example of use of wolf, included below, sorry.

nokogiri

Then, Nokogiri is needed.

First, install its dependencies (a few ruby gems), and two librairies :

sudo apt-get install ri1.9.1 rdoc1.9.1 irb1.9.1
sudo apt-get install libreadline-ruby1.9.1 libruby1.9.1 libopenssl-ruby1.9.1
# nokogiri requirements
sudo apt-get install libxslt-dev libxml2-dev

Warning, the last step changes from the official installation tutorial. Warning also, this step seems to be very long, as the gem apparently needs one highly-CPU consuming compilation step.

# Warning : here it changes from the official tutorial
sudo gem1.9.1 install nokogiri

wolf

And then, FINALLY, you can install the wolf gem:

sudo gem1.9.1 install wolf

Obtaining an application key (bis)

As it is explained on the GitHub page for wolf, you have to create an account, and then obtain a developer key by clicking on the button « Get an AppID ».

For your information, this key is a small string of characters that looks like this:

3HHP2W-UUPQUT6997

Avertissement

Of course, this is NOT a valid key! You have to get yourse!

Save the key

The simpler is to create a file .wolfrc (in your $HOME) with this content "Wolfram.appid=3HHP2W-UUPQUT6997" :

mv ~/.wolfrc /tmp/  # To not erase a file that should already be there!
echo "Wolfram.appid=3HHP2W-UUPQUT6997" > ~/.wolfrc
# Then make sure the file has the good reading permission for the current user:
chmod +r ~/.wolfrc

Test if the installation worked

A simple test can be to run wolf 0+0, to be sure that the wolf binary application was correctly installed.

Usually, the error messages returned by Ruby should be understandable, so if the first try yells at you and fails, it should gives you required information to fix the installation.

Avertissement

The most common problem in this installation process is the confusion between ruby1.8 and ruby1.9.1.


Examples: directly embedded in this webpage?

With the runblock plug-in for Sphinx, it is possible to embed the input and output of a call to the wolf tool, directly in this page !

$ wolf 0+0
## Input
0 + 0
## Result
0
## Number line
## Number name
zero

A first example

$ wolf "distance Paris Hyderabad"
## Input interpretation
distance | from | Paris, Ile-de-France
to | Hyderabad, Telangana, India
## Result
7563 km (kilometers)
## Unit conversions
4699 miles
7563 km (kilometers)
7.563×10^6 meters
4084 nmi (nautical miles)
## Direct travel times
aircraft (550 mph) | 8 hours 35 minutes
sound | 6 hours 10 minutes
light in fiber | 35 ms (milliseconds)
light in vacuum | 25 ms (milliseconds)
(assuming constant-speed great-circle path)
## Map
## Fraction of Earth circumference
0.19 ≈ 1 / 5

Other examples?

Solving numerically an equation

$ wolf "x^3 - sinx = e^-x"
## Input
x^3 - sin(x) = e^(-x)
## Plot
## Numerical solution
x ≈ 1.06853181316479...

Read the Pokémon database

A funny example that was famous when it first came out!

$ wolf pikachu | sed s/"\\\'"/""/ | sed s/"\\\%"/"%"/
## Input interpretation
Pikachu
## Basic properties
name | Pikachu
Japanese name | ピカチュウ (Pikachu)
Pokédex number | 25
type | Electric
Pokédex color | yellow
generation | Generation I
icon | 
footprint | 
## Physical characteristics
species | Mouse Pokémon
height | 0.41 m
weight | 5.99 kg
proportion of females | 50%
proportion of males | 50%
evolves from | Pichu
evolves into | Raichu
natural abilities | Static
Dream World abilities | Lightning Rod
## Base statistics
hit points | 35
attack | 55
defense | 40
special attack | 50
special defense | 50
speed | 90
total stats | 320
(statistics vary based on level, effort values and individual values)
## Image
## Popular curve
## Wikipedia summary
Pikachu is a species of Pokémon, fictional creatures that appear in an assortment of media of the Pokémon franchise by The Pokémon Company. Pikachu is a yellow mouse-like Pokémon with powerful electrical abilities. In most vocalized appearances, including the anime and certain video games, it is primarily voiced by Ikue Ōtani. A Pikachu also appears as part of the main cast in the live-action animated film Pokémon Detective Pikachu, played in CGI and voiced by Ryan Reynolds.

Other examples

This file wolfram.example shows for instance the raw output of the request « distance Paris Montreal ».

For more examples, consult the examples page on wolframalpha.com.


About this page

To embed a wolf command and its output, I use the following snippet or rST code:

.. runblock:: console

   $ wolf "my question"

A Sphinx plugin?

We could imagine a Wolfram|Alpha plug-in for Sphinx, similar to the GNUplot plugin, allowing to embed graphs and arrays produced by Wolfram|Alpha. But I am not motivated enough to implement it myself!