#!/usr/bin/perl
use strict;
#use warnings;
use utf8;

my $word  = "__UNDEF__";
my $lemma = "__UNDEF__";
my $pos   = "__UNDEF__";
my $msd   = "__UNDEF__";
my $counter = "1";

# from cwb-encode -h 
#  -U <str>  insert <str> for missing columns [default: "__UNDEF__"]

#print '<?xml version="1.0" encoding="UTF-8"?>'."\n";
print '<text title="fad_corpus_nob">'."\n";
print '<sentence n="'.$counter.'">'."\n";

while (<>) {
  chomp;
  if (/^</) {
    ($word) = $_ =~ m/>(.*)</;
    #print "\n wordform $word\n";
  } elsif (/^\t/) {
    if(/^\t+(\"[^\t]+\")\s([^\s]+).*$/) {
      #print "==> $1 AND $2\n";
      $lemma = qqtrim($1);
      $pos = $2;
      
    if ($pos =~ m/^<(.+)>$/) {
      $pos = $1;
    }

      if (($lemma =~ m/.+-$/) && ($pos eq "ukjent")) {
	$pos = "fp-cmp";
      }
      
      if ($lemma ne "£") {
	print $word."\t".$lemma."\t".$pos."\n";
      }
      
      if ($lemma eq "£") {
	print '</sentence>'."\n";
	$counter++;
	if (! eof)  { print '<sentence n="'.$counter.'">'."\n" }
      }
    } else {
      #print "OTHER  choices: $_\n";
      next;
    }
  }
}

print '</text>';

# Declare the subroutines
sub trim($);
sub ltrim($);
sub rtrim($);
sub qqtrim($);

# Perl trim function to remove whitespace from the start and end of the string
sub dtrim($)
{
	my $string = shift;
	$string =~ s/^\$//;
	return $string;
}

# Perl trim function to remove whitespace from the start and end of the string
sub qqtrim($)
{
	my $string = shift;
	$string =~ s/^\"//;
	$string =~ s/\"$//;
	$string =~ s/ /_/g;
	return $string;
}

# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
	my $string = shift;
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	return $string;
}
# Left trim function to remove leading whitespace
sub ltrim($)
{
	my $string = shift;
	$string =~ s/^\s+//;
	return $string;
}
# Right trim function to remove trailing whitespace
sub rtrim($)
{
	my $string = shift;
	$string =~ s/\s+$//;
	return $string;
}

