A recursive-descent parser might be overkill here, but don't discount it completely yet. A naive left-to-right token match and replacement may actually work here, though. The language used is pretty simple.
The left-to-right replacements would be simple with a hash. You could use a different top-level hash for each attribute group or you could use a hash of hashes. Two thousand entries is not that large. You could use YAML or XML for the specification if you didn't want to make constant hashes on disk. Just grab a token, see if it exists as a hash key, and replace it with its value. The code for this is simple and often seen (more often than it should be, as many times as the wheel has bene reinvented actually). The below is a simplified place to start, but you probably want to populate the full data structure in some other way for clarity.
use strict; use warnings; my $string = q(*IF *VALUE ManagedSystem.Product *EQ NT *AND *VALUE Man +agedSystem.Status *EQ '*OFFLINE'); my %dict = ( 'ManagedSystem' => { 'Product' => 'Product Code', 'Status' => 'Status', }, '*IF' => 'If', '*VALUE' => 'the value of', '*EQ' => 'is equal to', '*AND' => 'and', q('*OFFLINE') => q('OFFLINE'), ); my @parts = split /\s+/, $string; foreach my $p ( @parts ) { if ( $p =~ m/\./ ) { my ( $attr_grp, $attr ) = split /\./, $p; $p = $dict{ $attr_grp }{ $attr } if exists $dict{ $attr_grp }{ $at +tr }; } else { $p = $dict{ $p } if exists $dict{ $p }; } print $p . ' '; } print "\n";
If using a database, just make a table in the DB for each attribute group. Then make a char column for the attribute and make an index on it. Make another column for the replacement text. Then something simple like "select replacement from ManagedSystem where attribute = 'Product';" will get you the right replacement text. DBI, Rose::DBI, Class:DBI, or DBIx::Class would each be able to help you with that in its own way.
In reply to Re: Search and Replace with a Large Dictionary
by mr_mischief
in thread Search and Replace with a Large Dictionary
by THuG
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |