21
$\begingroup$

I've recently discovered that ToUpperCase is quite unreliable on non-ASCII input:

see code below

In[31]:= ToUpperCase@{"éàÇœßþσς", "ijķnjđӽծ", "ÿ"}
Out[31]= {"ÉÀÇŒSSÞ∑∑", "ijķnjđӽծ", "Y"}

So: it handles some accented ASCII, ligatures, Western European stuff, greek and cyrillic, while it fails on others (returning the lowercase characters themselves when they have valid uppercase variants). In at least one case, it fails utterly (the uppercase version of ÿ is Ÿ, not Y).

Mathematica’s documentation is very short on specifics on Unicode support. It broadly states that:

Mathematica has efficient systemwide 16-bit Unicode support, allowing a full range of international, technical and other character sets and character encodings.

which of course I very much doubt after seeing the above. More specifically, the ToUpperCase doc does not include a mention of Unicode.


So, after that bleak assessment, the question is the following: how do I get better Unicode-compliant string manipulation functions? (mostly ToUpperCase and ToLowerCase for now, but I'm sure others will be a problem for me in the future). Are there internal functions that do this, or external packages? Have I missed something in M’s documentation?


On a humorous note: the documentation for ToUpperCase even uses my example of a buggy transform (ÿ) in an example. It is, however, used about an unrelated issue, to warn users that “for a few characters, ToLowerCase is not the inverse of ToUpperCase” (as might be expected by Unicode-unaware programmers):

enter image description here


$\endgroup$
6
  • 2
    $\begingroup$ Since this issue is listed in the 'Possible Issues' section of the documentation I don't think it's likely there will be an internal solution, otherwise they would have noted that. If the list of problem cases is restricted couldn't you just redefine ToLowerCase for those cases? $\endgroup$ Commented Mar 27, 2012 at 21:16
  • 2
    $\begingroup$ First, the “problem” noted in the documentation is not my bug: the doc states that lowercase then uppercase is not necessarily a round trip, which is expected. I just mentioned that they unfortunately use one buggy case as part of their remark… I'll try to make my question clearer. $\endgroup$
    – F'x
    Commented Mar 27, 2012 at 21:21
  • 1
    $\begingroup$ Perhaps this W3.org note is related: "The CSS2 specification only requires that text-transform work for 'the Latin1 repertoire'. It is not clear what characters the term 'Latin1' covers. This test assumes that it means the Basic Latin and Latin-1 Supplement blocks of the Unicode Standard (equivalent to ISO-8859-1). There is one character in this set, ÿ, that only occurs in the lower-case form." $\endgroup$ Commented Mar 27, 2012 at 21:29
  • 1
    $\begingroup$ @SjoerdC.deVries yes, I actually tried ÿ because I know it's the only Latin-1 character whose uppercase variant is not included in Latin-1, so it the past it was always an interesting corner case. But Mathematica's string support is not Latin-1 based, as far as I understand. Or is it? $\endgroup$
    – F'x
    Commented Mar 27, 2012 at 21:32
  • 1
    $\begingroup$ And Wikipedia: "The ISO 8859-1 character encoding includes the letters ä, ë, ï, ö, ü, and their respective capital forms, as well as ÿ in lower case only, with Ÿ added in the revised edition ISO 8859-15." $\endgroup$ Commented Mar 27, 2012 at 21:34

3 Answers 3

19
$\begingroup$

See if this helps:

Needs["JLink`"];
ClearAll[toUpperCase];
toUpperCase[s_String] :=
    JavaBlock[JavaNew["java.lang.String", s]@toUpperCase[]];
$\endgroup$
6
  • $\begingroup$ It works for me $\endgroup$ Commented Mar 27, 2012 at 21:37
  • $\begingroup$ … and it works for me too! Clever use of Java link… $\endgroup$
    – F'x
    Commented Mar 27, 2012 at 21:39
  • $\begingroup$ @Sjoerd, F'x Great! $\endgroup$ Commented Mar 27, 2012 at 21:44
  • 1
    $\begingroup$ Another way of doing the same thing could be with MakeJavaObject[s] instead of JavaNew $\endgroup$
    – Rojo
    Commented Mar 28, 2012 at 0:11
  • 2
    $\begingroup$ @Rojo I prefer explicit class name, since I am using a particular method of the class, so anyone interested can jump right to Javadocs for Java String. $\endgroup$ Commented Mar 28, 2012 at 10:34
7
$\begingroup$

I had started working on a homegrown solution to this issue, directly by downloading Unicode data from the source. I’ll post it here, as it may be expanded to other functions were Java might not come and save the day!

unicodeData = StringSplit[#, ";"] & /@
   StringSplit[Import["ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt"], "\n"];
upperCaseData = (FromDigits[#, 16] & /@ # &) /@
  Select[unicodeData, (Length[#] > 12) && (StringLength[#[[13]]] > 0) &][[;; , {1, 13}]];

unichar[s_] := FromCharacterCode[FromDigits[s, 16], "Unicode"];
upperCaseChar[i_] := Module[{r},
  r = Select[upperCaseData, #[[1]] == i &];
  Return[If[Length[r] > 0, FromCharacterCode[r[[1, 2]]], FromCharacterCode[i]]];
  ]
upperCase[s_] := StringJoin[upperCaseChar /@ ToCharacterCode[s, "Unicode"]];

Which works:

In[127]:= upperCase["foéàçÿœÆijķnjđӽծÿ"]
Out[127]= "FOÉÀÇŸŒÆIJĶNJĐӼԾŸ"
$\endgroup$
0
4
$\begingroup$

It's on my list of things to tackle as part of the Incremental Language Development project.

In the meantime, there is a built-in solution that is fairly fast, which is:

Needs["MachineLearning`"];
ToUpperCaseUnicode[{"éàÇœßþσς", "ijķnjđӽծ", "ÿ"}]

{"ÉÀÇŒSSÞΣΣ", "IJĶNJĐӼԾ", "Ÿ"}

(not sure how to copy-paste the sigma etc without it turning into \σ)

$\endgroup$
1
  • $\begingroup$ You can use this tool to convert many mathematica character codes to unicode: meta_1044 $\endgroup$
    – shrx
    Commented Sep 9, 2014 at 16:41

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.