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.
With the grammar deployed to the cloud, I can 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”:
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:
The 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:
To see or choose from all the choices, use AmbiguityFunction:
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.