Nathan Fellman と mobrule はどちらも、一般的な方法である正規化を提案しています。
プログラムまたはサブルーチンの主な目的である実際の計算を行う前に、内容と構造の期待される規範に準拠するようにデータを処理する方が簡単な場合がよくあります。
マルコフ連鎖プログラムが面白かったので、それで遊んでみました。
これは、マルコフ連鎖のレイヤー数を制御できるバージョンです。変更$DEPTH
することで、シミュレーションの順序を調整できます。
コードを再利用可能なサブルーチンに分割しました。正規化ルーチンを変更することで、正規化ルールを変更できます。定義された一連の値に基づいてチェーンを生成することもできます。
多層状態テーブルを生成するコードは、最も興味深いビットでした。Data::Diver を使用することもできましたが、自分で解決したかったのです。
単語の正規化コードは、ノーマライザーが単一の単語ではなく、処理する単語のリストを返すことを実際に許可する必要があります-しかし、私は今それを修正すると単語のリストを返すことができるとは思いません..処理されたものをシリアル化するなどの他のことコマンド ライン スイッチに Getopt::Long を使用することはまだ残っています。私は楽しい部分だけをしました。
オブジェクトを使用せずにこれを書くのは、私にとって少し挑戦でした。これは、マルコフ生成オブジェクトを作成するのに適した場所のように感じました。私はオブジェクトが好きです。しかし、元の精神を保持するために、コードを手続き型のままにすることにしました。
楽しむ。
#!/usr/bin/perl
use strict;
use warnings;
use IO::Handle;
use constant NONWORD => "-";
my $MAXGEN = 10000;
my $DEPTH = 2;
my %state_table;
process_corpus( \*ARGV, $DEPTH, \%state_table );
generate_markov_chain( \%state_table, $MAXGEN );
sub process_corpus {
my $fh = shift;
my $depth = shift;
my $state_table = shift || {};;
my @history = (NONWORD) x $depth;
while( my $raw_line = $fh->getline ) {
my $line = normalize_line($raw_line);
next unless defined $line;
my @words = map normalize_word($_), split /\s+/, $line;
for my $word ( @words ) {
next unless defined $word;
add_word_to_table( $state_table, \@history, $word );
push @history, $word;
shift @history;
}
}
add_word_to_table( $state_table, \@history, NONWORD );
return $state_table;
}
# This was the trickiest to write.
# $node has to be a reference to the slot so that
# autovivified items will be retained in the $table.
sub add_word_to_table {
my $table = shift;
my $history = shift;
my $word = shift;
my $node = \$table;
for( @$history ) {
$node = \${$node}->{$_};
}
push @$$node, $word;
return 1;
}
# Replace this with anything.
# Return undef to skip a word
sub normalize_word {
my $word = shift;
$word =~ s/[^A-Z]//g;
return length $word ? $word : ();
}
# Replace this with anything.
# Return undef to skip a line
sub normalize_line {
return uc shift;
}
sub generate_markov_chain {
my $table = shift;
my $length = shift;
my $history = shift || [];
my $node = $table;
unless( @$history ) {
while(
ref $node eq ref {}
and
exists $node->{NONWORD()}
) {
$node = $node->{NONWORD()};
push @$history, NONWORD;
}
}
for (my $i = 0; $i < $MAXGEN; $i++) {
my $word = get_word( $table, $history );
last if $word eq NONWORD;
print "$word\n";
push @$history, $word;
shift @$history;
}
return $history;
}
sub get_word {
my $table = shift;
my $history = shift;
for my $step ( @$history ) {
$table = $table->{$step};
}
my $word = $table->[ int rand @$table ];
return $word;
}
更新:normalize_word()
上記のコードを修正して、ルーチン
から返される複数の単語を処理できるようにしました。
大文字と小文字を区別せずに句読点を単語として扱うには、 and を次のように置き換えnormalize_line()
ますnormalize_word()
。
sub normalize_line {
return shift;
}
sub normalize_word {
my $word = shift;
# Sanitize words to only include letters and ?,.! marks
$word =~ s/[^A-Z?.,!]//gi;
# Break the word into multiple words as needed.
my @words = split /([.?,!])/, $word;
# return all non-zero length words.
return grep length, @words;
}
もう 1 つの大きな-
落とし穴は、NONWORD キャラクターとして使用したことです。ハイフンを句読点記号として含めたい場合は、8 行目の NONWORD 定数定義を変更する必要があります。決して単語にならないものを選択してください。