#!/usr/bin/perl # remove_not.pl use strict; use warnings; use MeshGrammar (); sub process_term { our $output; local *output = \$_[0]; my $term = $_[1]; if ($term->[0] eq 'MESH') { $output .= $term->[1]; return; } if ($term->[0] eq 'PAREN') { $output .= '('; process_expr($output, $term->[1]); $output .= ')'; return; } if ($term->[0] eq 'NOT') { warn("WARNING: Behaviour for unary NOT not defined. Unary NOT not removed.\n"); $output .= $term->[0]; $output .= ' '; process_term($output, $term->[1]); return; } die(); # Should never reach here. } sub process_expr { our $output; local *output = \$_[0]; my $expr = $_[1]; my $i = 0; my $n = @$expr; my $term; my $op; $term = $expr->[$i++]; process_term($output, $term); while ($i != $n) { $op = $expr->[$i++]; $term = $expr->[$i++]; next if ($op eq 'NOT'); $output .= ' '; $output .= $op; $output .= ' '; process_term($output, $term); } } my $parser = MeshGrammar->new(); # The following can be put in a loop. my $string = <<'__EOI__'; ("Immunologic and Biological Factors"[MESH] OR "Immunosuppressive Agents"[MESH] OR "Transplantation Immunology"[MESH] OR "Allergy and Immunology"[MESH] OR "Graft vs Host Disease"[MESH]) NOT ("Foo"[MESH] OR "Bar"[MESH]) AND ("Kidney Transplantation"[MESH] OR "Liver Transplantation"[MESH] OR "Heart Transplantation"[MESH]) NOT ("My Term"[MESH] OR "Blah"[MESH]) NOT "foobar"[MESH] __EOI__ my $tree = $parser->parse($string) or die("Bad text.\n"); # require Data::Dumper; # print Data::Dumper::Dumper($tree); my $output = ''; process_expr($output, $tree); print($output, $/);