2
$\begingroup$

I want to find all of the Unicode input forms with a certain property, (say, the Unicode input forms for infix operators). How can I do this?

$\endgroup$

1 Answer 1

2
$\begingroup$

So the idea will be to scrape from the UnicodeCharacters.tr file. Mr.Wizard shows some of that here.

First we get the file and split it by line:

ucTR =
  FrontEndExecute@
   FrontEnd`FindFileOnPath["UnicodeCharacters.tr", 
    "PrivatePathsTextResources"];
ucLines = Rest[StringSplit[ucTR // Import[#, "Text"] &, "\n"]];

Then we parse the file into a Dataset (hopefully I didn't miss anything):

ucDataset =
  Dataset@Flatten@
    StringCases[ucLines,
     codePoint : ("0x" ~~ WordCharacter ..) ~~ 
       Whitespace ~~
       ("\\[" ~~ name : WordCharacter ... ~~ 
         "]") ~~ Whitespace ~~
       "(" ~~ 
       inputAliases : Except[")"] ... ~~ ")" ~~ Whitespace ~~

       formattingType : (WordCharacter ..) ~~ Whitespace | "" ~~

         precedence : (NumberString | "") ~~ Whitespace | "" ~~

        associativity : (WordCharacter ...) ~~ Whitespace | "" ~~

          leftWhiteSpace : (NumberString | "") ~~ Whitespace | "" ~~

             rightWhiteSpace : (NumberString | "") ~~ 
       Whitespace | "" ~~

       interPaddingMaybe : (NumberString | "") ~~ Whitespace | "" ~~

             partnerCharacter : (WordCharacter ...) :>
      <|
       "CodePoint" -> codePoint,
       "DisplayForm" ->
        Quiet@
         Check[

          ToExpression[
           "\"" <> StringReplace[codePoint, "0x" -> "\\:"] <> "\""],
          None
          ],
       "MathematicaName" -> name,
       "InputAliases" -> 
        StringCases[inputAliases, 
         "$" ~~ inner : Except["$"] .. ~~ "$" :> inner],
       "FormattingType" -> formattingType,
       "OperatorPrecedence" ->
        If[StringLength@precedence === 0,
         Missing["NotAvailable"],
         ToExpression[precedence]
         ],
       "OperatorAssociativity" ->
        If[StringLength@associativity === 0,
         Missing["NotAvailable"],
         ToExpression[associativity]
         ],
       "WhitespacePaddingLeft" ->
        If[StringLength@leftWhiteSpace === 0,
         Missing["NotAvailable"],
         ToExpression[leftWhiteSpace]
         ],
       "WhitespacePaddingRight" ->
        If[StringLength@rightWhiteSpace === 0,
         Missing["NotAvailable"],
         ToExpression[rightWhiteSpace]
         ],
       "WhitespacePaddingInner" ->
        If[StringLength@interPaddingMaybe === 0,
         Missing["NotAvailable"],
         ToExpression[interPaddingMaybe]
         ],
       "PartnerCharacter" ->
        If[StringLength@partnerCharacter === 0,
         Missing["NotAvailable"],
         "0x" <>
          ToUpperCase@

           StringTrim[partnerCharacter, 
            StartOfString ~~ ("0" | "") ~~ "x"]
         ]
       |>
     ];

Then finally we can just use a simple Select:

ucDataset[Select[#FormattingType == "Infix" &], "MathematicaName"] // 
  Normal // RandomSample[#, 25] &

{"DotEqual", "LeftTeeVector", "DownRightTeeVector", \
"NotLeftTriangleBar", "Cap", "UpTeeArrow", "NotSucceedsTilde", \
"NotLeftTriangle", "NotElement", "Distributed", "Times", \
"NotGreaterTilde", "NotLess", "NotGreaterGreater", \
"DoubleLongLeftArrow", "NotLessEqual", "TildeTilde", \
"LeftArrowRightArrow", "Equilibrium", "Coproduct", \
"InvisibleApplication", "DownLeftTeeVector", "DoubleUpArrow", \
"DownTee", "DoubleLeftTee"}

And here's a way to get those infix-types that are more hard-coded:

ucDataset[
   Select[
    StringStartsQ[#MathematicaName, "Raw"] && #FormattingType == 
       "Alias" &],
   "MathematicaName"
   ] // Normal // StringTrim[#, "Raw"] &

{"Exclamation", "DoubleQuote", "NumberSign", "Dollar", "Percent", \
"Ampersand", "Quote", "LeftParenthesis", "RightParenthesis", "Star", \
"Plus", "Comma", "Dash", "Dot", "Slash", "Colon", "Semicolon", \
"Less", "Equal", "Greater", "Question", "At", "LeftBracket", \
"Backslash", "RightBracket", "Wedge", "Underscore", "Backquote", \
"LeftBrace", "VerticalBar", "RightBrace", "Tilde"}

Note that we can use this for other things, too, say for all the open-close forms:

Map[
  With[{openChar = #},
    <|
     "Opener" ->
      If[openChar["MathematicaName"] =!= "",
       openChar["MathematicaName"],
       openChar["DisplayForm"]
       ],
     "Closer" ->
      With[{
        closeChar =
         Select[Normal@ucDataset,
           #["CodePoint"] === openChar["PartnerCharacter"] &
           ][[1]]
        },
       If[closeChar["MathematicaName"] =!= "",
        closeChar["MathematicaName"],
        closeChar["DisplayForm"]
        ]
       ]
     |>
    ] &,
  Normal@ucDataset[Select[#FormattingType == "Open" &]]
  ] // Dataset

bloop

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.