0

Interpreter fails on Association with mixed type Values:

<|"a" -> 1, "b" -> "99%"|> // Map[Interpreter["Number" | "Percent"]]

MapThread::mptd: "Object !(InterpreterPackageScopeparserSemantic[...

(long list of warnings)

<|"a" -> Replace[
   Verbatim[Alternatives][
     Interpreter`DependentTypes`PackagePrivate`a_] :> 
    Interpreter`DependentTypes`PackagePrivate`a, {0, \[Infinity]}], 
 "b" -> Quantity[99, "Percent"]|>

man pages say Alternative forms are tried in sequence. Without that sequence, of course the percent will not be parsed, but the 1 is.

<|"a" -> 1, "b" -> "99%"|> // Map[Interpreter["Number"]]

<|"a" -> 1, "b" ->Failure[...enter a valid number...]] |>

MarcoB
  • 67,153
  • 18
  • 91
  • 189
alancalvitti
  • 15,143
  • 3
  • 27
  • 92

1 Answers1

5

This has nothing to do with Association.

{1, "99%"} // Map[Interpreter["Number" | "Percent"]] 

fails as well.

Originally, I thought the answer could be found (more or less) in the documentation where the first line of the Details and Option section says:

Interpreter[...][input] applies the interpreter to a particular input, which is typically a string.

That "typically a string" suggests that the input might be something different than a string, but I don't believe that there is one example among the multitude of examples that is is using anything other than a string. And indeed, if you change your 1 to a "1" your problem goes away:

<|"a" -> "1", "b" -> "99%"|> //Map[Interpreter["Number" | "Percent"]]
(* <|"a" -> 1, "b" -> Quantity[99, "Percent"]|> *)

So, documentation error? Well, no. This is not the end of the story, as both

Interpreter["Number"][1]
(* 1 *)

and

Interpreter["Percent"][1]
(* Quantity[1, "Percent"] *)

do work without error messages. So, non-string input is altogether possible.

It is the combination with Alternatives that doesn't work.

Interpreter["Number" | "Percent"][1]

gives the voluminous set of error messages again, with the string version working OK:

Interpreter["Number" | "Percent"]["1"]
(* 1 *)

For now, I'd call it a bug. A workaround would be to apply ToString where necessary.

Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323
  • As you say, Alternatives within Interpreter indeed seems to be the root cause. Note that <|"a" -> 1, "b" -> "99%"|> // Map[Interpreter["SemanticExpression"]] works just fine to return: <|"a" -> 1, "b" -> Quantity[99, "Percent"]|>. (+1) – MarcoB Jun 08 '15 at 21:32