- 论坛徽章:
- 0
|
又照原文写了个测试
sub spelltest {
my %test = @_;
my $start = time;
my $n = 0;
my $bad = 0;
my $unknown = 0;
for my $word (keys %test) {
for my $wrong ((split ' ', $test{$word})) {
$n += 1;
my $w = correct($wrong);
if ($w ne $word) {
$bad += 1;
$unknown += !(exists $nwords{$word});
}
}
}
my $secs = time - $start;
my $pct = int(100 - 100 * $bad/$n);
return "bad= $bad, unknown= $unknown, secs= $secs, pct= $pct, n= $n\n";
}
|
def spelltest(tests, bias=None, verbose=False):
import time
n, bad, unknown, start = 0, 0, 0, time.clock()
if bias:
for target in tests: NWORDS[target] += bias
for target,wrongs in tests.items():
for wrong in wrongs.split():
n += 1
w = correct(wrong)
if w!=target:
bad += 1
unknown += (target not in NWORDS)
if verbose:
print 'correct(%r) => %r (%d); expected %r (%d)' % (
wrong, w, NWORDS[w], target, NWORDS[target])
return dict(bad=bad, n=n, bias=bias, pct=int(100. - 100.*bad/n),
unknown=unknown, secs=int(time.clock()-start) ) |
运行结果
- >perl -w spell.pl
- bad= 68, unknown= 15, secs= 85, pct= 74, n= 270
- bad= 130, unknown= 43, secs= 132, pct= 67, n= 400
- >Exit code: 0
- >pythonw -u "spell.py"
- {'bad': 68, 'bias': None, 'unknown': 15, 'secs': 17, 'pct': 74, 'n': 270}
- {'bad': 130, 'bias': None, 'unknown': 43, 'secs': 29, 'pct': 67, 'n': 400}
- >Exit code: 0
复制代码
用perl写这个慢了好多,不知道原因在哪里 
[ 本帖最后由 cobrawgl 于 2008-5-31 09:22 编辑 ] |
|