#!/usr/bin/perl

use warnings;
use strict;

use FL3 'es';
use Data::Dumper;

##
## This is the FreeLing APIs/Perl/sample.perl file converted to
## Lingua::FreeLing3 syntax
##

# initialize the morphologic analyser
morph(LocutionsFile   => 'locucions.dat',
      QuantitiesFile  => 'quantities.dat',
      AffixFile       => 'afixos.dat',
      ProbabilityFile => 'probabilitats.dat',
      DictionaryFile  => 'dicc.src',
      NPdataFile      => 'np.dat',
      PunctuationFile => '../common/punct.dat',
      CorrectorFile   => 'corrector/corrector.dat');

## read input text and analyze it.
while (<STDIN>) {
    chomp;
    my $l = tokenizer->tokenize($_);	# tokenize
    my $ls = splitter->split($l);	# split sentences
    $ls = morph->analyze($ls);	        # morphological analysis
    $ls = hmm->tag($ls);                # PoS tagging
    $ls = nec->analyze($ls);            # NE classification

    print "** print tagged Sentence\n";
    printSent($ls);
    $ls = chart->parse($ls);

    print "** print shallow Tree\n";
    printTree($ls);
    $ls = txala->parse($ls);

    print "** print Dependency Tree\n";
    printDepTree($ls);
}


sub printSent {
  my $ls = shift;

  for my $s (@$ls) {
    for my $w (@{ $s->get_words() }) {
      print $w->get_form()." ".$w->get_lemma()." ".$w->get_tag()."\n";
    }
    print "\n";
  }
}

sub rec_printTree {
  my ($n, $depth) = @_;

  print ' ' x ($depth*2);
  my $info = $n->get_info();
  print "+" if ($info->is_head());
  if ($n->num_children() == 0) {
    my $w = $info->get_word();
    printf("(%s %s %s)", $w->get_form(), $w->get_lemma(), $w->get_tag());
  } else {
    print $info->get_label() . "_[\n";
    for (my $d = $n->sibling_begin(); $d != $n->sibling_end(); ++$d) {
      rec_printTree($d, $depth + 1);
    }
    print ' ' x ($depth*2);
    print "]";
  }
  print "\n";
}

sub printTree {

  my $ls = shift;

  foreach my $is (@{ $ls }) {
    my $tr = $is->get_parse_tree();
    &rec_printTree($tr->begin(), 0);
  }
}

sub rec_printDepTree {

  my ($n, $depth) = @_;

  print ' ' x ($depth*2);

  my $info = $n->get_info();
  my $link = $info->get_link();
  my $linfo = $link->get_info();
  printf("%s/%s/", $link->get_info()->get_label(), $info->get_label());
  my $w = $n->get_info()->get_word();
  printf("(%s %s %s)", $w->get_form(), $w->get_lemma(), $w->get_tag());

  if ($n->num_children() > 0) {
    print " [\n";
    for (my $d = $n->sibling_begin(); $d != $n->sibling_end(); $d++) {
      rec_printDepTree($d, $depth + 1) unless $d->get_info()->is_chunk();
    }
    my %ch;
    for (my $d = $n->sibling_begin(); $d != $n->sibling_end(); $d++) {
	my $dinfo = $d->get_info();
	next unless $dinfo->is_chunk();
	my $i = $dinfo->get_chunk_ord();
	$ch{ $i } = $d;
    }
    foreach my $i (sort { $a <=> $b } keys %ch) {
	rec_printDepTree($ch{$i}, $depth + 1);
    }

    print ' ' x ($depth*2);
    print "]";
  }
  print "\n";
}


sub printDepTree {

  my $ls = shift;

  foreach my $is (@{ $ls }) {
    my $dep = $is->get_dep_tree();
    &rec_printDepTree($dep->begin(), 0);
  }
}


sub debug {

  print ref($_[0])."\n";
  print Dumper($_[0])."\n";
  print join(" ", keys %{ $_[0] })."\n";
}




__END__
