Debian Codegraph @

scripts/external_text.pl

raw
1 #!/usr/bin/perl
2 # -*- mode: cperl; indent-tabs-mode: nil; tab-width: 3; cperl-indent-level: 3; -*-
3 BEGIN {
4 $| = 1;
5 };
6 use warnings;
7 use strict;
8 use utf8;
9 #use feature 'unicode_strings';
10
11 use IO::Handle;
12 autoflush STDOUT 1;
13 autoflush STDERR 1;
14
15 use FindBin;
16 use lib $FindBin::Bin.'/';
17 use CG3_External qw(check_protocol read_window write_window write_null_response);
18
19 #my @af_cmd = ('sed', '-u', 's/w/WXU/g; s/ma/am/g;');
20 my @af_cmd = ('cat'); # Change to something meaningful
21
22 use IPC::Run qw(start pump finish timeout);
23 my $af_in;
24 my $af_out;
25 my $af_err;
26 my $af_h;
27 my $af_started = 0;
28
29 sub initSubChain {
30 if ($ENV{'DEBUG'}) { print STDERR "$0 initSubChain enter\n"; }
31 $af_h = start \@af_cmd, \$af_in, \$af_out, \$af_err;
32 $af_started = 1;
33 if ($ENV{'DEBUG'}) { print STDERR "$0 initSubChain exit\n"; }
34 }
35
36 sub callSubChain {
37 if (!$af_started) {
38 initSubChain();
39 }
40 my ($input) = @_;
41 if ($ENV{'DEBUG'}) { print STDERR "$0 callSubChain input: $input\n"; }
42 utf8::encode($input);
43 $af_in .= $input;
44 $af_in .= "\n\n<STREAMCMD:FLUSH>\n\n";
45 pump $af_h until $af_out =~ /<STREAMCMD:FLUSH>/g;
46
47 my $out = $af_out;
48 $af_out = '';
49
50 utf8::decode($out);
51 $out =~ s@</s>@@g;
52 $out =~ s/<STREAMCMD:FLUSH>//g;
53 $out =~ s@^\s+@@g;
54 $out =~ s@\s+$@@g;
55 if ($ENV{'DEBUG'}) { print STDERR "$0 callSubChain output: $out\n"; }
56
57 return $out;
58 }
59
60 binmode(STDIN);
61 binmode(STDOUT);
62
63 if (!check_protocol(*STDIN)) {
64 die("Out of date protocol!\n");
65 }
66
67 while (my $w = read_window(*STDIN)) {
68 my $out = '';
69 foreach my $c (@{$w->{'cohorts'}}) {
70 $out .= $c->{'wordform'}."\n";
71 foreach my $r (@{$c->{'readings'}}) {
72 $out .= "\t".$r->{'baseform'};
73 foreach my $t (@{$r->{'tags'}}) {
74 $out .= ' '.$t;
75 }
76 $out .= "\n";
77 }
78 }
79
80 my $in = callSubChain($out);
81
82 $out =~ s@^\s+@@g;
83 $out =~ s@\s+$@@g;
84
85 if ($in eq $out) { # No change, so just skip the rest.
86 write_null_response(*STDOUT);
87 next;
88 }
89
90 my @out = split /\n/, $out;
91
92 my @in = split /\n/, $in;
93
94 my $lout = @out;
95 my $lin = @in;
96 if ($lout != $lin) {
97 print STDERR "Mismatch in number of lines!\n";
98 write_null_response(*STDOUT);
99 next;
100 }
101
102 my $cc = 0;
103 for (my $i = 0 ; $i<$lin ; $i++) {
104 if ($in[$i] !~ /\t/) { # Found a cohort line, start looking for readings
105 $cc++;
106 my $c = @{$w->{'cohorts'}}[$cc-1];
107
108 $in[$i] =~ s/^\s+//g;
109 $in[$i] =~ s/\s+$//g;
110 if ($in[$i] ne $out[$i]) { # Wordform changed
111 $c->{'wordform'} = $in[$i];
112 }
113
114 my $rc = 0;
115 my $j;
116 for ($j = $i+1 ; $j<$lin ; $j++) {
117 if ($in[$j] !~ /\t/) { # Found a cohort line, so stop looking for readings
118 last;
119 }
120 $rc++;
121 if ($in[$j] eq $out[$j]) { # Skip if the reading has no changes
122 next;
123 }
124 my $r = @{$c->{'readings'}}[$rc-1];
125 $r->{'flags'} |= (1 << 0);
126
127 $in[$j] =~ s/^\s+//g;
128 $in[$j] =~ s/\s+$//g;
129 my @tags = split /\s+/, $in[$j];
130 $r->{'baseform'} = shift(@tags);
131 @{$r->{'tags'}} = @tags;
132 }
133 $i = $j-1;
134 }
135 }
136
137 write_window(*STDOUT, $w);
138 }