-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_long2short.pl
149 lines (107 loc) · 3.37 KB
/
find_long2short.pl
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
149
#!/usr/bin/perl
use strict;
use File::Find;
my $dirname = shift;
my @flist = <$dirname/*.txt>;
my $outfile = "output.txt";
open my $fh, '>', $outfile or die "Cannot open $outfile\n";
foreach my $fname ( @flist ) {
open FP, $fname or die "Cannot open $fname\n";
my $str = "";
while( <FP> ) {
$str .= $_;
}
my @lines = split "\n", $str;
my $min = 0;
my $max = 1;
my $dep;
my $item;
my $child_sentence = 0; # flag variable that is false initially
my $length = 0;
my $difference = 0;
my @phrase_idxs = ();
my @lengths = ();
my @output = ();
my @stack = ();
my %cols = (); # hash of arrays
my @mins = ();
my $child_long2short_count = 0;
my $long2short_count = 0; # len1 - len2 > 0
# get length of sentence
for( my $k = 0; $k < @lines; $k++ ) {
@{$cols{$k}} = split "\t", $lines[$k];
if( $cols{$k}->[0] =~ /^\d/ ) {
$min++;
}
if( $lines[$k] =~ /\-\-\-\-\-/ ) {
push @mins, $min;
$min = 0;
}
}
my $idx = 0;
for( my $i = 0; $i < @lines; $i++ ) {
push @output, $lines[$i];
if ( $cols{$i}->[3] =~ /prep/ ) {
push @phrase_idxs, $cols{$i}->[0];
if( $cols{$i - 1}->[3] =~ /adv/ ) {
push @phrase_idxs, $cols{$i - 1}->[0]; # if immediately preceded by adv, push adv index
}
for( my $j = $i + 1; $j < @lines; $j++ ) {
if( $cols{$j}->[7] =~ /POBJ/ ) {
push @phrase_idxs, $cols{$j}->[0];
push @stack, $cols{$j}->[0];
last; # break
}
push @phrase_idxs, $cols{$j}->[0];
}
$min = $mins[$idx];
while( @stack ) {
$item = pop @stack;
if( $item > $max ) {
$max = $item;
}
if( $item < $min ) {
$min = $item;
}
if( @phrase_idxs ) {
$dep = pop @phrase_idxs;
push @stack, $dep;
}
}
$length = $max - $min;
push @lengths, $length;
$max = 0;
}
# if child utterance, set flag variable
if( index( $lines[$i], "*CHI" ) != -1 ) {
$child_sentence = 1;
}
if( $lines[$i] =~ /\-\-\-\-\-/ ) {
$difference = $lengths[0] - $lengths[1];
$idx++;
if( $difference > 0 ) {
print $fh "difference in length: $difference\n";
if( $child_sentence == 1 ) {
print $fh "child_long2short_count + 1\n";
$child_long2short_count++;
print $fh "child long2short: $child_long2short_count\n";
}
else {
print $fh "long2short count + 1\n";
$long2short_count++;
print $fh "long2short: $long2short_count\n";
}
for( my $k = 1; $k < @output; $k++ ) {
print $fh "$output[$k]\n";
}
print $fh "\n";
}
# clear arrays for next utterance
@lengths = ();
@output = ();
# reset flag for next utterance
$child_sentence = 0;
next;
}
}
}