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
andnokogiri
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
andsed
, 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¶
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
) :
A dummy test:
0+0
:Computing a distance:
Solving a numerical equation:
And a funnier example, a Pokémon :
We can ask the current exchange rate between Euro (€) and the Indian rupee:
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 packetsruby1.9.1
andruby1.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-devWarning, 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 thewolf
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 !
A first example¶
Other examples?¶
Solving numerically an equation¶
Read the Pokémon database¶
A funny example that was famous when it first came out!
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!