How to verbalise code in Mathematica?
Starting a brain dump of ideas, listening to my inner monologue. Please feel free to edit and add suggestions. Here is a list of most operators.
| sym | example | pronunciation | votes, comments, rants
|––––––|–––––––––––––|–––––––––––––––––––––––––––––––––––––|–––––––––––––––––––––––––
| :: | x::y | x says y |
| # | # | slot |
| | #3 | slot 3 |
| ## | ## | all slots |
| | ##3 | all slots from third |
| & | x& | x done |
| | | x end-of-function |
| % | % | above |
| | | answer |
| | | previous |
| : | x:_ | x-pattern |
| | | anything, call it x |
| /; | x_/;y | x-pattern that y |
| | | anything that y, call it x |
| ? | x_?yQ | x-pattern that is y |
| | | anything y, call it x |
| _ | x_ | x-pattern |
| | | anything, call it x |
| _ | x_y | x-pattern of type y |
| | | anything of type y, call it x |
| __ | x__ | x-patterns |
| | | any sequence, call it x |
| ___ | x___ | x-maybepatterns |
| | | any sequence, even empty, call it x |
| _. | x_. | x-defaultpattern |
| | | anything, call it x, with default |
| : | x_:y | x-pattern defaults to y |
| | | anything, call it x, with default y |
| .. | x.. | one or more x |
| | | x-more |
| ... | x... | zero or more x |
| | | x-maybemore |
| {} | {1,2,3} | list of 1, 2, 3 |
| [[]] | x[[i]] | element i of x |
| ;; | x;;y;;z | from x to y in steps of z |
| == | x==y | x equal to y |
| != | x!=y | x not equal to y |
| === | x===y | x same as y |
| =!= | x=!=y | x not same as y |
| ++ | x++ | x and then increment it |
| | | x-before-increment |
| ++ | ++x | x but increment it first |
| | | x-after-increment |
| -- | x-- | x and then decrement it |
| | | x-before-decrement |
| -- | --x | x but decrement it first |
| | | x-after-decrement |
| [] | f[x] | f of x |
| | f[x,y] | f of x and y |
| @* | x@*y | y then x (read from right to left) |
| // | x//y | x then y |
| /* | x/*y | x then y |
| @ | f@x | f of x |
| ~ | x~f~y | f of x and y |
| /@ | f/@x | f mapped on x |
| | | f of all in x |
| //@ | f//@x | f map-alled on x |
| | | f of everything in x |
| | | f mapped on everything in x |
| @@ | f@@x | f-head on x |
| | | f applied to x |
| @@@ | f@@@x | f-head mapped on x |
| | | f applied to all in x |
| -> | x->y | x becomes y |
| | | x goes to y |
| :> | x:>y | x will become y |
| | | x will go to y |
| /. | x/.y | x where y |
| //. | x//.y | x where repeatedly y |
| = | x=y | x is y |
| := | x:=y | x will be y |
| ^= | x[y]^=z | y remembers x[y] is z |
| ^:= | x[y]^:=z | y remembers x[y] will be z |
| /: | x/:y=z | x remembers y is z |
| /: | x/:y:=z | x remembers y will be z |
| . | x=. | x is cleared |
|––––––|–––––––––––––|–––––––––––––––––––––––––––––––––––––|–––––––––––––––––––
You could try using SpokenString
:
SpokenString[HoldForm[x + y /. x->2]]
"x plus y slash dot x goes to 2"
Addendum
If you don't mind messing with internal functions, it is possible to customize the output of SpokenString
. For this purpose, it is convenient to make use of my ExtractDownValues function reproduced below:
SetAttributes[ExtractDownValues, HoldAll]
ExtractDownValues[sym_[args__]] := ExtractDownValues[sym,args]
ExtractDownValues[sym_,args__] := Cases[
DownValues[sym],_[h_,_] /; MatchQ[Unevaluated[sym[args]],h]
]
The internal function that does most of the heavy lifting is SpokenStringDump`SSText
:
TracePrint[SpokenString[HoldForm[x + y /. x -> 2]], _SpokenStringDump`SSText]
SpokenStringDump`SSText[x+y/. x->2]
SpokenStringDump`SSText[x+y]
SpokenStringDump`SSText[x]
SpokenStringDump`SSText[y]
SpokenStringDump`SSText[2]
SpokenStringDump`SSText[x->2]
SpokenStringDump`SSText[x]
SpokenStringDump`SSText[2]
"x plus y slash dot x goes to 2"
Using ExtractDownValues
we find:
ExtractDownValues @ SpokenStringDump`SSText[x + y /. x -> 2]
{HoldPattern[ SpokenStringDump`SSText[SpokenStringDump`a_ /. SpokenStringDump`b_]] :> SpokenStringDump`SpeakQuantity[SpokenStringDump`a, ReplaceAll, True] ~~ " slash dot " ~~ If[ListQ[Unevaluated[SpokenStringDump`b]], SpokenStringDump`SpeakListContents[SpokenStringDump`b], SpokenStringDump`SpeakQuantity[SpokenStringDump`b, ReplaceAll, True]], HoldPattern[ SpokenStringDump`SSText[SpokenStringDump`f_[SpokenStringDump`a___]]] :> Module[{SpokenStringDump`ans = SpokenStringDump`SSText0[SpokenStringDump`f[SpokenStringDump`a]]}, If[Head[SpokenStringDump`ans] =!= SpokenStringDump`SSText0 && ! MemberQ[SpokenStringDump`ans, $Failed, {0, 1}], SpokenStringDump`ans, SpokenStringDump`SSTextFallThrough[ Unevaluated[SpokenStringDump`f[SpokenStringDump`a]]]]]}
Notice the " slash dot " string. So, modifying this downvalue to:
SpokenStringDump`SSText[a_ /. b_] := SpokenStringDump`SpeakQuantity[a,ReplaceAll,True] ~~
" where " ~~
If[ListQ[Unevaluated[b]],
SpokenStringDump`SpeakListContents[b],
SpokenStringDump`SpeakQuantity[b,ReplaceAll,True]
]
will produce:
SpokenString[HoldForm[x + y /. x->2]]
"x plus y where x goes to 2"