Java中获取某种语言的unicode字符
Java 有什么方法可以让我获得特定语言(例如孟加拉语或阿拉伯语)的所有 Unicode 字符吗?
Is there any way in Java so that I can obtain all the Unicode characters of a particular language (for example Bengali or Arabic)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
java.lang.Character 类有一个名为 UnicodeBlock 的内部静态类。例如,您可以这样获取阿拉伯语 Unicode 块:
通过迭代所有字符(或更准确地说,Unicode 代码点),可以检查每个字符以找到其 Unicode 块:
The java.lang.Character class has a inner static class called UnicodeBlock. You can, for example, get the Arabic Unicode Block thusly:
By iterating over all characters (or more precisely, Unicode code points) it is possible to check each to find its Unicode Block:
在 1.7 之前,Java 不支持 Unicode 脚本。不过,Java 对 Unicode 属性的支持非常粗略。它基本上停留在 Unicode 千禧年前的化身上。这是一个真正的问题。他们声称他们将通过 JDK7 赶上 Unicode 6,但我还没有看到任何证据表明他们将拥有适当的属性支持。
在 Unicode 6.0 中,总共有 1,051 个代码点算作阿拉伯语,其中 1,020 个代码点位于基本多语言平面中:
有效的原因是
unichars
程序是用 Perl 编写的,而 Perl 始终具有优秀 Unicode 属性支持。我正在针对 Unicode 6.0 运行它;以前版本的 Unicode 中的数量要少一些。事实上,Unicode 6.0 添加了 17 个新的阿拉伯字符:您不能尝试为此使用块。脚本与块不同。并非给定块中的所有代码点都属于同一脚本。同样重要的是,您经常会发现给定脚本的字符分散在奇怪的块中。
例如,希腊语块中有 18 个非希腊字符:
阿拉伯语块中有 13 个非阿拉伯字符:
另外还有 4 个希腊语块和 4(或 5)个阿拉伯语块:
\p{Block:Greek }
和\p{Greek_and_Coptic}
是别名,但其余的都是不同的。但即使你查看了所有这些块,你还是会错过一些。例如:
看到问题了吗?
顺便说一句,您使用
uniprops
不仅仅是列出所有可能的属性。它还可以为您提供任何给定代码点的属性:如果您发现它们有用,您可以下载 uniprops 和 unichars 节目。该组中有第三个,uninames。所有内容均附有说明和示例。
即使其中一些属性在 Java 中还不能直接使用,如果您愿意,也可以使用 Perl 生成 Java 代码;我自己也经常这么做。 :)
Until 1.7, Java has no support for scripts in Unicode. Java has very sketchy Unicode property support, though. It is basically stuck at antemillennial incarnations of Unicode. This is a real problem. They claim they’ll catch up to Unicode 6 with JDK7, but I haven’t seen any evidence yet that they will have proper property support.
In Unicode 6.0, there are 1,051 code points that count as Arabic overall, with 1,020 of those in the Basic Multilingual Plane:
The reason that works is that the
unichars
program is written in Perl, and Perl has always had excellent Unicode property support. I’m running that against Unicode 6.0; there were somewhat fewer in previous releases of Unicode. In fact, 17 new Arabic characters were added for Unicode 6.0:You just cannot try to use blocks for this. Scripts are different from blocks. Not all code points in a given block are of the same script. Equally important, you often find characters of a given script scattered all over in strange blocks.
For example, there are 18 non-Greek characters in the Greek block:
And 13 non-Arabic characters in the Arabic block:
Plus there are 4 Greek blocks and 4 (or 5) Arabic ones:
\p{Block:Greek}
and\p{Greek_and_Coptic}
are aliases, but the rest are all distinct.But even if you look at all those blocks, you’ll miss some. For example:
See the problem?
BTW, you use
uniprops
for more than just listing all possible properties. It can also give you the properties of any given code point:If you find them useful, you can download the source for the uniprops and unichars programs. There’s a third in the group, uninames. All come with instructions and examples.
Even if some of those properties aren’t directly available in Java yet, it’s ok to use Perl to generate Java code if you want; I do it all the time myself. :)