【发布时间】:2021-11-25 09:01:24
【问题描述】:
我想在我的 Perl 程序中添加拼写检查。看起来Text::Aspell 应该可以满足我的需要,但它只提供了检查单个单词的功能。
use strict;
use warnings;
use Text::Aspell;
my $input = "This doesn't look too bad. Me&you. with/without. 1..2..3..go!";
my $aspell = Text::Aspell->new();
$aspell->set_option('lang', 'en');
print "$input: ", $aspell->check($input), "\n";
打印出来:
This doesn't look too bad. Me&you. with/without. 1..2..3..go!: 0
很明显它只需要单个单词,那么我如何将文本分成单词?一个简单的split 在空白处:
foreach my $word (split /\s/, $input) {
next unless($word =~ /\w/);
print "$word: ", $aspell->check($word), "\n";
}
这会导致没有空格的标点符号出现问题:
This: 1
doesn't: 1
look: 1
too: 1
bad.: 0
Me&you.: 0
with/without.: 0
1..2..3..go!: 0
我想我可以提一下标点符号:
foreach my $word (split qr{[,.;!:\s#"\?&%@\(\)\[\]/\d]}, $input) {
next unless($word =~ /\w/);
print "$word: ", $aspell->check($word), "\n";
}
这会得到合理的输出:
This: 1
doesn't: 1
look: 1
too: 1
bad: 1
Me: 1
you: 1
with: 1
without: 1
go: 1
但看起来很笨拙,我想知道是否有更简单(我要编写的代码更少,不那么脆弱)的方式。
如何对文本进行拼写检查?
【问题讨论】:
标签: perl spell-checking aspell