#!/usr/bin/perl
# Extracts plain text from TEI XML (JRC Acquis Corpus)
# (c) 2006 Dan Zeman <zeman@ufal.mff.cuni.cz>
# License: GNU GPL

use utf8;
use open ":utf8";
binmode(STDIN, ":utf8");
binmode(STDOUT, ":utf8");
binmode(STDERR, ":utf8");
use XML::Parser;

$parser = new XML::Parser(Handlers =>
{
    Start => \&handle_start,
    End   => \&handle_end,
    Char  => \&handle_char
});
while(<>)
{
    $xml .= $_;
}
$parser->parse($xml, ProtocolEncoding => 'utf-8', ErrorContext => 1);



###############################################################################
# SUBROUTINES
###############################################################################



#------------------------------------------------------------------------------
# Handles the occurrence of a XML start tag.
#------------------------------------------------------------------------------
sub handle_start
{
    my $expat = shift;
    my $element = shift;
    my %attr = @_;
    if($element eq "p")
    {
        $inside = 1;
    }
}



#------------------------------------------------------------------------------
# Handles plain text.
#------------------------------------------------------------------------------
sub handle_char
{
    my $expat = shift;
    my $string = shift;
    if($inside)
    {
        $text .= $string;
    }
}



#------------------------------------------------------------------------------
# Handles the occurrence of a XML end tag.
#------------------------------------------------------------------------------
sub handle_end
{
    my $expat = shift;
    my $element = shift;
    if($element eq "p")
    {
        $text =~ s/^\s+//s;
        $text =~ s/\s+$//s;
        $text =~ s/\s+/ /sg;
        print("$text\n");
        $text = "";
        $inside = 0;
    }
}
