WOLFRAM

New in the Wolfram Language: GrammarRules

The Wolfram Language provides tools for programmatic handling of free-form input. For example, Interpreter, which was introduced in Version 10.0, converts snippets of text into computable Wolfram Language expressions. In smart form fields, this functionality can automatically translate input like “forty-two” into a Wolfram Language expression like “42.”

But what does it take to perform more complicated operations or customize responses and actions? For that you need a grammar. The grammar indicates the structure that should be matched and the action that should be taken using information extracted from the match.

A grammar gives you natural language control over your computer so that you can process language snippets to yield functions that perform commands. For example, telling your computer to “open a website” requires mapping snippets like “open” and “a website” to the Open command and the URL of a website.

Once a grammar is constructed, it is uploaded to the Wolfram Cloud where it can be used from anywhere to process natural language inputs.

In the following example, I’ve implemented natural language commands for visiting websites and saving notebooks:

Natural language commands for visiting websites and saving notebooks

With the grammar deployed to the cloud, I can visit the Wolfram company website with the natural language command “open the wolfram website”:

Visit the Wolfram company website with the natural language command open the wolfram website

And I can save the notebook I’m working on with the command “save”:

Save the notebook being worked on with command save

Notice the use of GrammarToken to recognize the “the wolfram website” as a URL, in the same fashion as Interpreter. This match is fed to the Print statement and SystemOpen via the grammar action on the right-hand side of the rule.

Many parsers fail in the presence of ambiguity, but the Wolfram Language’s GrammarRules and GrammarApply thrive on it. For example, consider a grammar that helps schedule flights between specified locations:

A grammar that helps schedule flights between specified locations

The grammar parses a natural language request, returning the GeoPosition of the departure and destination locations:

Grammar parses a natural language request, returning the GeoPosition of the departure and destination locations

But not all city names are as unambiguous as “Chicago” and “London.” The Wolfram Language knows about five cities named “Shelbyville” and 28 named “Springfield” in the United States. Yet GrammarApply chooses a Shelbyville and a Springfield that are likely to be most appropriate for me, using the same logic that Wolfram|Alpha does:

GrammarApply chooses the most likely Shelbyville and Springfield

To see or choose from all the choices, use AmbiguityFunction:

Choose from all Springfields and Shelbyvilles using AmbuguityFunction

As you can see, GrammarRules accomplishes a lot with small amount of code.

See the Interpreter documentation for the rich list of built-in interpreters that your grammars can use to understand natural language expressions for dates, math, currencies, and much more. GrammarRules is supported in Version 10.1 of the Wolfram Language and Mathematica, and is rolling out soon in all other Wolfram products.


Download this post as a Computable Document Format (CDF) file
.

Comments

Join the discussion

!Please enter your comment (at least 5 characters).

!Please enter your name.

!Please enter a valid email address.

4 comments

  1. Jeremy

    Your blog is very interesting. Is it possible to get the notebook version so that I can study it further?

    Thanks
    Michael

    Reply
  2. Hello

    These GrammarRules look promising. Although it’s great that you *can* CloudDeploy them, it looks like you *must* CloudDeploy them, which is inefficient in a learning and development phase.

    Is that correct? Or could you show how to use a GrammarRule as a local object?

    Thanks
    Christian

    Reply
  3. Being able to interpret written language is important. Do you guys have plans to implement interpreting *spoken* language? If yes, than when?

    Reply