#! perl -sw use strict; local $\=$/; #! Added leading and removed trailing /'s for consistancy my %answers = ( '/WRB1/JJ1/VBZ1/NP1/IN1/NP2' => '/NP1/VBZ1(so)/JJ1 : /NP1/IN1/NP2/VBZ1(really)/JJ1', '/WP/NP1/VB1/VBN1/PP1' => '/PP1/PP2/NP1(are)/VB1', ); my $question = 'How/WRB1 hot/JJ1 is/VBZ1 the core/NP1 of/IN1 the earth/NP2 ?/'; my @keys = $question =~ m!(/[A-Z0-9]+)!g; # Parse markers # Update: Fixed problem not capturing all words between tags #my @values = $question =~ m!(\w+)(?=/)!g; # Parse values my @values = $question =~ m!(.*?)(?:/[A-Z0-9]+\s+)!g; my %lookup; @lookup{@keys} = @values; # Build lookup my $key = join'',@keys; my $answer = $answers{$key}; $answer =~ s!\s*$_\s*! $lookup{$_} !g for keys %lookup; $answer =~ s!\s+:\s+! and the !g; #$answer = 'The' . $answer; # Update: No longer needed. print $answer;