2016-10-13 4 views
1

私は単語のリストを含むカスタム辞書を作成しました。今では、Perlで単語をとり、その辞書から与えられた単語に最も近い(字句順の)単語のリスト(例えば5つ)を生成するプログラムを作成する予定です。今私はそれをするのに役立つモジュールがすでにあるかどうかを知りたいと思います。そうでない場合、どうすればそのようなことを達成することができますか?事前に感謝Perlで指定された単語の候補リストを生成するには?

+1

「足」があれば、「飼育者、飼育者、映像、フットバック、フットボール」が必要でしょうか? – Schwern

答えて

2

私はあなたが自動修正機能に取り組んでいると思います。私は似たような機能に取り組み、Text::SpellCheckerは本当に私を助けました。このモジュールはバックグラウンドでaspellまたはhunspellを使用するため、多言語サポートもあります。スペルミスで単語をチェックし、同じことを提案します。

use Text::SpellChecker; 
($Text::SpellChecker::pre_hl_word, 
$Text::SpellChecker::post_hl_word) = (qw([ ])); 

my $checker = Text::SpellChecker->new(text => "Foor score and seven yeers ago"); 

while (my $word = $checker->next_word) { 
    print $checker->highlighted_text, 
     "\n", 
     "$word : ", 
     (join "\t", @{$checker->suggestions}), 
     "\nChoose a new word : "; 
    chomp (my $new_word = <STDIN>); 
    $checker->replace(new_word => $new_word) if $new_word; 
} 

希望します。

1

はい、Search::Dictは、辞書ファイル内の単語または最も近い単語を効率的に検索します。このプログラムは、入力された単語の周りの単語を検索します。例えば

#!/usr/bin/perl 
use strict; 
use warnings; 
use autodie; 
use v5.10; 

use Search::Dict; 
use POSIX qw(ceil floor); 

my $target = shift; 
my $Window_Size = 5; 
my @window; 

open my $fh, "/usr/share/dict/words"; 

# Find the spot in the file where the word 
# is >= our word. 
# Use dictinoary order and ignore case. 
my $pos = look $fh, $target, 1, 1; 

# Add the next words 
for(1..ceil($Window_Size/2)) { 
    my $word = <$fh>; 
    chomp $word; 

    redo if $word eq $target; 

    push @window, $word; 
} 

# Read the previous block of words 
my $string; 
seek $fh, $pos - 256, 0; 
read $fh, $string, 256; 
my @previous_words = split /\n/, $string; 

# Add them to our list at the front. 
for(1..floor($Window_Size/2)) { 
    unshift @window, pop @previous_words; 
} 

say join ", ", @window; 

...あなたの言葉が "A" または "Zyzzogetonは" 運動として残っているもののような

$ perl ~/tmp/test.plx foot 
fooster, foosterer, footage, footback, football 

エッジケース。

関連する問題