I have been experimenting with GrammarRules in an attempt to create a parser for text downloaded from the web. I have been using Interpreter to help understand the behavior of GrammarApply. Here is a simple grammar:

  dollarPSF = CloudDeploy[
  GrammarRules[{FixedOrder[p : GrammarToken["CurrencyAmount"], 
      " per square foot" ] :> p}

Here are the results of applying this to a chunk of text:

GrammarApply[dollarPSF, "60 per square foot"]
Out: $60
GrammarApply[dollarPSF, "$60 per square foot"]
Out: $Failed

Contrast this (somewhat-surprising result) with the more-expected result from Interpreter:

Out: $60
Out: $60

For anyone who has a deeper understanding of this part of MMA than I, here is a bonus question. Is there a way (as in languages like AWK or SED) to take a chunk of text and run it through a grammar like the one above (or better still a set of grammars), causing MMA to scan the text to find a match with the grammar(s)? I could of course either a) pre-process the text or b) chunk it into sentences and then words and submit them to the interpreter word by word or c) some combination. But this is going to very slow and kludgy. TIA.

  • $\begingroup$ Well this seems like a bug. I'm going to go ahead and report it. As to your question AWK/SED question. No you can't use it like that. These Interpreters are largely semantic and call the Wolfram|Alpha API. They're fuzzy and pretty expensive to computer compared to a regex. Now granted a number of them probably do use regex and if you dug for them you'd probably find them. But that's not a general strategy. Mathematica has a lot of SED/AWK like functionality, this just isn't it. $\endgroup$
    – Searke
    Jul 10, 2018 at 13:36


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.