#!/usr/local/bin/perl
use strict;
use warnings;
use Text::SpellChecker;
my $text = "coördinator";
my $checker = Text::SpellChecker->new( text => $text );
while ( my $word = $checker->next_word ) {
print "Bad word is $word\n";
}
输出:
Bad word is rdinator
所需:
Bad word is coördinator
如果我在
$text
中有 Unicode,则模块会中断.知道如何解决吗?我安装了该模块正在使用的 Aspell 0.50.5。我认为这可能是罪魁祸首。
编辑:作为
Text::SpellChecker
需要 Text::Aspell
或 Text::Hunspell
, 我删除了 Text::Aspell
并安装Hunspell
, Text::Hunspell
, 然后:$ hunspell -d en_US -l < badword.txt
coördinator
显示正确的结果。这意味着我的代码或 Text::SpellChecker 有问题。
考虑到米勒的建议,我做了以下
#!/usr/local/bin/perl
use strict;
use warnings;
use Text::SpellChecker;
use utf8;
binmode STDOUT, ":encoding(utf8)";
my $text = "coördinator";
my $flag = utf8::is_utf8($text);
print "Flag is $flag\n";
print "Text is $text\n";
my $checker = Text::SpellChecker->new(text => $text);
while (my $word = $checker->next_word) {
print "Bad word is $word\n";
}
输出:
Flag is 1
Text is coördinator
Bad word is rdinator
这是否意味着模块无法正确处理 utf8 字符?
最佳答案
这是 Text::SpellChecker 错误 - 当前版本假定只有 ASCII 单词。
http://cpansearch.perl.org/src/BDUGGAN/Text-SpellChecker-0.11/lib/Text/SpellChecker.pm
#
# next_word
#
# Get the next misspelled word.
# Returns false if there are no more.
#
sub next_word {
...
while ($self->{text} =~ m/([a-zA-Z]+(?:'[a-zA-Z]+)?)/g) {
恕我直言,最好的解决方法是使用每种语言/语言环境的分词正则表达式 或 将分词留给使用的底层库。
aspell list
报告 coördinator
作为一个词。
关于perl - Text::SpellChecker 模块和 Unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26707917/