Welcome to mirror list, hosted at ThFree Co, Russian Federation.

tagger-german-chunk.perl « wrappers « training « scripts - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0b707a579524558a66178d0075be4345e637cc66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env perl
#
# This file is part of moses.  Its use is licensed under the GNU Lesser General
# Public License version 2.1 or, at your option, any later version.

use warnings;
use strict;
use Getopt::Long "GetOptions";

# split -a 5 -d  ../europarl.clean.5.de
# ls -1 x????? | ~/workspace/coreutils/parallel/src/parallel /home/s0565741/workspace/treetagger/cmd/run-tagger-chunker-german.sh
# cat x?????.out > ../out

my $chunkedPath;
my $treetaggerPath;

GetOptions('chunked=s' => \$chunkedPath,
          'tree-tagger=s' => \$treetaggerPath);

binmode(STDIN, ":utf8");
binmode(STDOUT, ":utf8");

#my $TMPDIR= "/tmp/chunker.$$";
my $TMPDIR= "chunker.$$";
print STDERR "TMPDIR=$TMPDIR\n";
print STDERR "chunkedPath=$chunkedPath\n";
`mkdir $TMPDIR`;

my $inPath = "$TMPDIR/in";

open(IN, ">$inPath");
binmode(IN, ":utf8");

while(my $line = <STDIN>) {
  chomp($line);
  print IN "$line\n";
}
close(IN);

# call chunker
if (!defined($chunkedPath)) {
  if (!defined($treetaggerPath)) {
    print STDERR "must defined -tree-tagger \n";
    exit(1);
  }

  $chunkedPath = "$TMPDIR/chunked";
  print STDERR "chunkedPath not defined. Now $chunkedPath \n";
  my $cmd = "$treetaggerPath/cmd/tagger-chunker-german-utf8  < $inPath > $chunkedPath";
  `$cmd`;
}

# convert chunked file into Moses XML
open(CHUNKED, "$chunkedPath");
open(IN, "$inPath");
binmode(CHUNKED, ":utf8");
binmode(IN, ":utf8");

my $sentence = <IN>;
chomp($sentence);
my @words = split(/ /, $sentence);
my $numWords = scalar @words;
my $prevTag = "";
my $wordPos = -1;

while(my $chunkLine = <CHUNKED>) {
  chomp($chunkLine);
  my @chunkToks = split(/\t/, $chunkLine);

  if (substr($chunkLine, 0, 1) eq "<") {
    if (substr($chunkLine, 0, 2) eq "</") {
      # end of tag
      print "</tree> ";
      $prevTag = "";

  	  if ($wordPos == ($numWords - 1)) {
	    # closing bracket of last word in sentence
	    print "\n";
            $sentence = <IN>;
	    chomp($sentence);
	    @words = split(/ /, $sentence);
	    $numWords = scalar @words;
	    $wordPos = -1;
	  }
    }
    else {
      # beginning of tag
      if ($wordPos == ($numWords - 1)) {
        # closing bracket of last word in sentence
        print "\n";
        $sentence = <IN>;
        chomp($sentence);
        @words = split(/ /, $sentence);
        $numWords = scalar @words;
        $wordPos = -1;
      }

      $prevTag = $chunkToks[0];
      $prevTag = substr($prevTag, 1, length($prevTag) - 2);
      print "<tree label=\"$prevTag\">";
    }
  }
  else {
    # word
    ++$wordPos;

    if (scalar(@chunkToks) != 3) {
      # parse error
      print STDERR "CHUNK LINES SHOULD BE 3 TOKS\n";
      exit(1);
    }

    if ($wordPos >= $numWords) {
      # on new sentence now
      if (length($prevTag) > 0) {
        print "</tree>";
      }
      print "\n";
      if (length($prevTag) > 0) {
	  print "<tree label=\"$prevTag\">";
      }

      $sentence = <IN>;
      chomp($sentence);
      @words = split(/ /, $sentence);
      $numWords = scalar @words;
      $wordPos = 0;
    }

    if ($chunkToks[0] ne $words[$wordPos]) {
      # word in chunk input and sentence should match
      print STDERR "NOT EQUAL:" .$chunkToks[0] ." != " .$words[$wordPos] ."\n";
      exit(1);
    }

    print $chunkToks[0] . " ";

  }

}

print "\n";

close(IN);
close(CHUNKED);

`rm -rf $TMPDIR`;