#!/usr/bin/perl use strict; use warnings; # Limpa os caracteres < e > de um ficheiro XML # que não dizem respeito a uma tag válida do # mesmo e especificada como parâmetro da função # # Invoca-se da seguinte forma: # xmlclean [-tag tag1,tag2,...,tagN] # # Resultado da invocação: # ficheiro outfilecorpus.cor devidamente limpo # my $corpus_file = shift or die("./xmlclean [-tag tag1,tag2,...,tagN]"); my $option = shift; my @tags = @ARGV; my %hash_tags = (); my $i = 0; my $pat; $pat = "(" . join("|",@tags) . ")"; if ($option =~ "-tag") { open(FILE, "$corpus_file"); open(OFILE, ">outfilecorpus.cor"); while () { cleanTags($pat,$_); print OFILE $_; } } # Função que recebe como parâmetros o padrão das tags # separadas por | e a expressão a substituir. # sub cleanTags { my $pattern = shift; my $expToSubstitute = shift; $expToSubstitute =~ s/<($pattern\b[^>]*)>/save($1)/ge; $expToSubstitute =~ s/<(\/$pattern\b)>/save($1)/ge; $expToSubstitute =~ s/<($pattern\b[^>]*\/)>/save($1)/ge; $expToSubstitute =~ s//}/g; $expToSubstitute =~ s/__(\d+) __/<$hash_tags{"__$1 __"}>/g; } # Função que recebe uma expressão e guarda-a na hash de tags # sub save { my $exp = shift; $i++; $hash_tags{"__$i __"} = $exp; return "__$i __"; }