my $dic_file = 'main.dic'; my $out_file = 'out.dic'; my ($key, $res, @keys, $word, @word); open DB,"$dic_file"; open OUT, ">>$out_file"; while () { next unless /:#/; ($key,$res) = split /:#/; @keys = &key_conv($key); foreach $key (@keys) { $word = $key . ':#' . $res; print OUT $word; } } close DB; close OUT; sub key_conv { my $string = $_[0]; my $i = 0; local @stock = (); my $delete; my $string_ref; my $match = '(&&)?([^\(\)&]*(\([^\(\)]+\)[^\(\)&]*)+)(&&)?'; while ($string =~ /$match/) { push (@stock, $2); $delete = $2; $delete =~ s/([\[\\\]\^\{\|\}\~\(\)\.\*])/\\$1/g; $string =~ s/$delete/dummy$i/; ++$i; } $string_ref = &conv($string); return @{$string_ref}; } sub conv { my $string = $_[0]; my @store = (); my @strings = (); if ($string =~ /\(.*\)/) { @strings = &rearrange($string); } else { @strings = split /&&/, $string; } foreach (@strings) { $_ =~ s/dummy([0-9]+)/($stock[$1])/; } foreach $string2 (@strings) { if ($string2 =~ /&&|\(|\)/) { $string2 = &conv($string2); push @store, @{$string2}; } else { push @store, $string2; } } @strings = @store; return \@strings; } sub rearrange { my $string = $_[0]; my @strings = split /\(|\)\(|\)/, $string; my @strings2 = (''); my @store = (); my $fragment; my @fragments; my $fragment3; while (@strings) { $fragment = shift @strings; next unless $fragment; $fragment = '&&'.$fragment if $fragment =~ s/&&$//; @fragments = split /&&/, $fragment; foreach $string2 (@strings2) { foreach $fragment2 (@fragments) { $fragment3 = $string2.$fragment2; push @store, $fragment3; } } @strings2 = @store; @store = (); } return (@strings2); }