-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpreprocessing_classDependent.pl
More file actions
282 lines (228 loc) · 7.05 KB
/
preprocessing_classDependent.pl
File metadata and controls
282 lines (228 loc) · 7.05 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
use Cwd;
###########################################
# A script that goes through the ALC corpus and extracts the text from the .hlb files. It will create
# one output file for .hlb file - having the same name - that contains the text as plain text. Additionally
# at the end dummy words for the irregularity featues will be appended (e.g. '#stutters#'). The files will be written
# to a folder structure that is convenient to WEKA StringToWordVector class that can be used for BOW creation.
#
# Type of output: will create the raw data that is separated in document classes
#
############################################
#----------------------------------------------------------------
# Variablen Deklarationen
#----------------------------------------------------------------
#ordnerstruktur durchlaufen und jeweilige Unterordner abspeichern
my @blocks;
my @sessions;
my @files;
#die orginalfilenames nachher wiederverwenden
my $originalFilenames = {};
#globalen Pfade des Systems
my $basePath = "/home/bas-alc";
my $corpusBasePath = $basePath . "/corpus";
my $outputPath = $basePath . "/test/rawDataMappingProblem/separate_all";
main();
sub main{
collectFiles();
loopThroughFilesAndExtractText();
}
#----------------------------------------------------------------
# Ordner durchlaufen, um Dateien zu sammeln
#----------------------------------------------------------------
sub collectFiles
{
chdir($corpusBasePath);
#leere das BlockArray und wechsle in das entsprechende Verzeichnis
chdir("EMU/LAB") or die "path EMU/LAB not found!!!!!\n";
@blocks = ();
my $blockpath = `ls`;
while($blockpath =~ m/BLOCK/){
if($blockpath =~ s/(BLOCK\d+)//){
push @blocks, $1;
}
}
loopThroughBlocks();
#wechsle zurück in den bas-alc ordner
chdir('../../..') or die "cd back in dvds defect\n";
}
sub loopThroughBlocks
{
foreach my $block (@blocks){
#leere das SessionArray und wechsle in das entsprechende Verzeichnis
@sessions = ();
chdir($block) or die "$block not found!!!\n";
my $sessionpath = `ls`;
while($sessionpath =~ m/SES/){
if($sessionpath =~ s/(SES\d+)//){
push @sessions, $1;
}
}
loopThroughSessions();
#wechsle zurück in den BLOCKxy ordner
chdir('../')or die "cd back in blocks defect\n";
}
}
sub loopThroughSessions
{
foreach my $session (@sessions){
chdir($session) or die "$session not found\n";
my $filenames = `ls`;
while($filenames =~ m/.hlb/){
if($filenames =~ s/(\d+_h_\d+.hlb)//){
my $originalFilename = $1;
my $originalFilenameQualified = getcwd() . '/' . $originalFilename;
push @files, $originalFilenameQualified; #need an absolute path here
$originalFilenames->{$originalFilenameQualified} = $originalFilename;
}
}
#wechsle zurück in den SESxxyy ordner
chdir('../') or die "cd back in sessions defect\n";
}
}
#----------------------------------------------------------------
# Was wird bei den jeweiligen Dateien ausgeführt
#----------------------------------------------------------------
sub loopThroughFilesAndExtractText{
print "Now looping through all (" . scalar @files . ") HLB files to converting them to continuous text...";
foreach my $filename (@files){
convertFile($filename);
}
print "\tFINISHED\n";
}
sub convertFile{
my $filename = shift;
#open file and check for errors
my $rc = open(FILE,"<",$filename);
if(!defined($rc)){
print "Problem opening file " . $filename . "\n";
return;
}
#process the fourth line
<FILE>;
<FILE>;
<FILE>;
my $fourthLine = <FILE>;
my $class = recognizeClass($fourthLine);
my $type = recognizeItemType($fourthLine);
<FILE>;
<FILE>;
my $outputText = '';
#Now save the file into a contious text string
while(my $line = <FILE>){
#stop if the line contains only whitespaces because then the phonetics follow...
if($line =~ m/^\s+$/){
last; #stop processing this file.
}
#get the word from this line
$outputText .= extractWord($line);
}
$outputText .= appendIrregularieties($outputText, $fourthLine);
#produce the outputfile
printOutput($outputText, $class, $type, $filename);
}
####################################################
#Helper methods
####################################################
sub appendIrregularieties
{
my ($outputText, $line) = @_;
#Erkenne die irrequalirites
if($line =~ m/\d+\|(\d+)\|(\d+)\|(\d+)\|(\d+)\|(\d+)\|(\d+)\|(\d+)\|(\d+)/){
#append the irregularity as often as it appeared
$outputText = appendCurrentIrregularity($outputText, $1, 'hesitation');
$outputText = appendCurrentIrregularity($outputText, $2, 'pauseShort');
$outputText = appendCurrentIrregularity($outputText, $3, 'pauseLong');
$outputText = appendCurrentIrregularity($outputText, $4, 'delayedPhones');
$outputText = appendCurrentIrregularity($outputText, $5, 'pronounceErrors');
$outputText = appendCurrentIrregularity($outputText, $6, 'stutters');
$outputText = appendCurrentIrregularity($outputText, $7, 'repairs');
$outputText = appendCurrentIrregularity($outputText, $8, 'interrupteds');
}
return $outputText;
}
sub appendCurrentIrregularity
{
my ($outputText, $number, $irregularity) = @_;
for(my $i = 0; $i < $number; $i++){
$outputText .= '#'.$irregularity.'# ';
}
return $outputText;
}
#extracts the word from this line and inserts a space at the end of the word
sub extractWord{
my $line = shift;
if($line =~ s/^\d+\s(\S+)\s//){
my $word = $1;
if($word eq '#GARBAGE#'){
return ' ';
}else{
return $word.' ';
}
}
}
sub printOutput
{
my ($string, $class, $type, $filename) = @_;
#create the outputfilename
my $outputFile;
if($class eq "alc"){
$outputFile = $outputPath . '/' . $type . '/alc/' . $originalFilenames->{$filename};
}else{
$outputFile = $outputPath . '/' . $type . '/nonalc/' . $originalFilenames->{$filename};
}
#write the string to the desired location, which is based on the class and type
my $rc2 = open(OUTFILE,">", $outputFile);
if(!defined($rc2)){
createDirs($outputPath, $type);
open(OUTFILE,">", $outputFile);
print OUTFILE $string;
close(OUTFILE);
}else{
print OUTFILE $string;
close(OUTFILE);
}
}
sub createDirs
{
my($path, $type) = @_;
chdir($path);
mkdir($type);
chdir($type);
mkdir('alc');
mkdir('nonalc');
}
#erkenne den ITEM Typ in HLB file
sub recognizeItemType{
my $fourthLine = shift;
#erkenne den ITEM Typ
if($fourthLine =~ m/(\w) (\w)\s*$/){
return $1.$2;
}else{
die "Problem beim Erkennen des Item Typs.";
}
}
#erkenne die Klasse des Files
sub recognizeClass{
my $fourthLine = shift;
my $class;
#erkenne die Klasse
if($fourthLine =~ m/\w+ \w+ \w+ \w+ \w+ \w+ \w+ (a|na|cna)/){
#konvertiere klasse von a => alc und von na => nonalc
if($1 eq 'a'){
$class = 'alc';
}elsif($1 eq 'na'){
$class = 'nonalc';
}elsif($1 eq 'cna'){
$class = 'nonalc';
}else{
die "Fehler die Klasse zu erkennen in $fourthLine. Exiting...\n";
}
return $class;
}else{
print "No Class recognized, skipping.\n";
print "Recognization string was " . $fourthLine . "\n";
}
}