在Perl中,看到é是e,E的变体

前端之家收集整理的这篇文章主要介绍了在Perl中,看到é是e,E的变体前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在Perl中处理以utf-8编码的字符串.一个任务是我需要一种方法来知道以带有变音符号的字母开头的单词,例如“écrit”,以与“elephant”相同的字母开头,以及“England”.我需要一个通用的解决方案,因为我将使用多种语言.我需要知道这一点,因为我正在为索引创建字母标题.我刚提到的每个单词都存储在“E”下.

有一种直截了当的方法吗?

解决方法

我假设您按照英语校对规则排序并使用字母文本.下面的代码是一个良好的开端,但现实世界比这更复杂. (例如,中文文本根据上下文有不同的词典规则,例如通用词典,卡拉OK歌曲列表,电子门铃名单……)我无法提供完美的解决方案,因为这个问题的信息很少.

use 5.010;
use utf8;
use Unicode::Collate::Locale 0.96;
use Unicode::Normalize qw(normalize);

my $c = Unicode::Collate::Locale->new(locale => 'en');
say for $c->sort(qw(
    eye
    egg
    estate
    etc.
    eleven
    e.g.
    England
    ensure
    educate
    each
    equipment
    elephant
    ex-
    ending
    écrit
));
say '-' x 40;
for my $word (qw(écrit Ëmëhntëhtt-Rê Ênio ècole Ēadƿeard Ėmma Ędward Ẽfini)) {
    say sprintf '%s should be stored under the heading %s',$word,ucfirst substr normalize('D',$word),1;
}

__END__
each
écrit
educate
e.g.
egg
elephant
eleven
ending
England
ensure
equipment
estate
etc.
ex-
eye
----------------------------------------
écrit should be stored under the heading E
Ëmëhntëhtt-Rê should be stored under the heading E
Ênio should be stored under the heading E
ècole should be stored under the heading E
Ēadƿeard should be stored under the heading E
Ėmma should be stored under the heading E
Ędward should be stored under the heading E
Ẽfini should be stored under the heading E

猜你在找的Perl相关文章