#!/usr/bin/perl
use strict;

# cg2moses.pl
# Script that transforms the cg-format into moses-format
# to be refined

my $newClause = 1;
my $newLemma = 1;
my $posOnly = 1;
my $gizaLine = "";
my $wordForm = "";

while (<>) {
  
  chomp;
  
  if ($newClause) {
    m/\"\<(.+)\>\"/;
    $gizaLine = lc($1);
    $gizaLine =~ s/\#//g;
    $newClause = 0;
  } else {
    if (m/\"\<(.+)\>\"/) {
      if ($1 ne "Ł") {
	$wordForm = lc($1);
	$wordForm =~ s/\#//g;
	$gizaLine = $gizaLine." ".$wordForm;
	$newLemma = 1;
      }
    } else {
      (my $lemma, my $rest) = m/^\s*\"(.+)\"\s+(.+)\s*$/;
      if ($lemma ne "Ł") {
	$lemma = lc($1);
	$lemma =~ s/\#//g;
	if ($newLemma) {
	  my @features = split(' ', $rest);
	  my $pos = $features[0];
	  $pos = lc($pos);
	  $pos =~ s/\*//g;
	  $gizaLine = $gizaLine."|".$lemma."|".$pos;
	  $newLemma = 0;
	}
      } else {
	print "$gizaLine\n";
	$newClause = 1;
	$newLemma = 1;
      }
    }
  }
}
