混合文本文件中语言 X 的字符数?

Dɑv*_*vïd 5 perl shell-script regular-expression unicode

我有混合语言的文本文件,想计算其中一种语言的可打印字符的简单总数。语言位于不同的 unicode 范围内是有帮助的。

我的具体用例涉及希伯来语、多调希腊语和英语——但我想这个问题的解决方案也可以推广到其他上下文。

我只想计算希伯来语字符 - 那是 Unicode [\u0590-\u05ff]。这是一个简短的示例输入文件(根据我的手动计数,它包含 62 个希伯来字符):

[ Ps117 ]?
h1: ?  ???? ?? ???? ?? ???? ?????? ?? ????? ?
r1: Praise the LORD, all nations! Extol him, all peoples!
g1: ?????????. ??????? ??? ??????, ????? ?? ????, ?????????? ?????, ?????? ?? ????,
b1: Alleluia. Praise the Lord all you nations: praise him all you peoples.

h2: ?  ?? ??? ????? ???? ???? ???? ????? ???? ?? ?
r2: For great is his steadfast love toward us; and the faithfulness of the LORD endures for ever. Praise the LORD!
g2: ??? ?????????? ?? ????? ????? ??' ????, ??? ? ??????? ??? ?????? ????? ??? ??? ?????.
b2: For his mercy has been abundant toward us: and the truth of the Lord endures for ever.
Run Code Online (Sandbox Code Playgroud)

如果有帮助,我使用的是 Ubuntu 16.04.2 LTS。我想 perl 在这里可能是一个可能的选择,或者一些 shell 脚本......但我不知道这些东西,这就是我问的原因!


出于好奇,我输入中的行是:h= Hebrew; r= 修订标准版;g= 希腊七十士译本;b= 七十士译本的布伦顿译本;在每种情况下,后跟一个经文编号。

Dav*_*Six 4

确定 Unicode 字符串的长度可能存在问题。有关规范化的更多详细信息,请参阅Twitter 开发人员文档中的此页面

字符数将取决于您配置的区域设置。您可以运行locale来验证是否已配置 UTF-8 区域设置。完成此操作后,@stephen-rauch 的代码应该可以工作。

根据您使用的正则表达式库,您也许还可以使用命名脚本,例如\p{Hebrew}以下是使用删除所有非希伯来字符的\P{Greek} 示例:链接\P{Hebrew}

编辑:初始结果是由于区域设置配置错误造成的