-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpredict.pl
102 lines (98 loc) · 2.5 KB
/
predict.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
$| = 1;
$labdir = "./fulllab";
$que = "./questions.hed";
$outdir = "./fulllab_answers_perl";
mkdir $outdir, 0755;
opendir DIR, $labdir;
$i = 1;
Get_Ques2Regular($que);
foreach $_ ( sort {$a <=> $b} readdir DIR )
{
if (/(.*)\.lab/)
{
$filename = $1;
print $filename, "\t", "$i\n";
$i++;
$lab = sprintf("$labdir/$filename.lab");
$out = sprintf("$outdir/$filename.dat");
feature( $lab, $que, $out );
}
}
closedir DIR;
sub Get_Ques2Regular
{
open( question, "<@_[0]" ) or die "questions.hed can't be open";
%Ques2Regular;
while ( $line_question = <question> )
{
if ( $line_question =~ /^\s*$/ )
{
next;
}
else
{
if ( $line_question =~ /\{(.*)\}/ )
{
@phoneall = split( /,/, $1 );
}
foreach $phoneone (@phoneall)
{
$original_phoneone = $phoneone;
$phoneone =~ s/\*/\.\*/g; #(*->.*)
$phoneone =~ s/\?/\./g; #(?->. )
$phoneone =~ s/\$/\\\$/g; #($->\$)
$phoneone =~ s/\+/\\\+/g; #(+->\+)
$phoneone =~ s/\|/\\\|/g; #(|->\|)
$phoneone =~ s/\^/\\\^/g; #(^->\^)
$phoneone =~ s/^([a-z])/\^$1/; # such as m-* -> ^m-*
$phoneone =~ s/([^*])$/$1\$/; # such as *m -> *m$
$phoneone = qr/$phoneone/i; # Compile regex pattern
$Ques2Regular{$original_phoneone} = $phoneone unless exists $Ques2Regular{$original_phoneone};
}
}
}
close(question);
}
sub feature
{
open( label, "<@_[0]" ) or die "00000001.lab can't be open";
open( question, "<@_[1]" ) or die "questions.hed can't be open";
open( output, ">@_[2]" ) or die "output.txt is null";
binmode output;
while ( $line_label = <label> )
{
seek question, 0, 0;
$line_label =~ s/\s+$//; # similar to line_label.rstrip() in Python
while ( $line_question = <question> )
{
if ( $line_question =~ /^\s*$/ )
{
next;
}
else {
if ( $line_question =~ /\{(.*)\}/ )
{
@phoneall = split( /,/, $1 );
}
$temp = 0;
foreach $phoneone (@phoneall)
{
$phoneone = $Ques2Regular{$phoneone};
if ( $line_label =~ /$phoneone/ )
{
$temp = 1;
print output pack( "f", 1 );
last;
}
}
if ( $temp == 0 )
{
print output pack( "f", 0 );
}
}
}
}
close(label);
close(question);
close(output);
}