-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractBinaryFeatureTrainset.pl
255 lines (219 loc) · 5.68 KB
/
extractBinaryFeatureTrainset.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
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
#!/usr/perl -w
use strict;
=pod
@author :Senthilkumar Soundararajan
@usage :extractBinaryFeatureTrainset.pl <input train file> <out file>
@Purpose :To generate features and output labels from the train set for Table identification challenge.
=cut
die << 'USAGE' unless @ARGV == 2;
1. input train file
2. out file
USAGE
$| = 1;
$_.=',';
sub get_feature
{
my $table = shift;
my $link = 0;
my $row_count = 0;
# Features
my $has_links = 0; # contains more {First colum has hyper link}
my $has_attribs = 0; # all the first column is bold
my $has_dimensions = 0; # { length : width : height : color : shape : type }
my $has_details = 0; # { material : quantity : brand : case : Warranty }
my $has_book_detals = 0; # { ISBN : author }
my $has_product_detals = 0; # { Dimensions : Frame : fork : manual }
my $has_electronics = 0; # { Camera : Bluetooth : WiFi : SIM : 2G : 3G : 4G : OS : RAM : Operating System }
my $dec_flag = 1;
while ( $table =~ /<tr.*?>(.*?)<\/tr>/gs )
{
++$row_count;
my $row = $1;
if ( $row =~ /<th.*?>(.*?)<\/th>/s )
{
$has_attribs = 1;
}
else
{
$dec_flag = 0;
}
if ( $table =~ /<a /si )
{
++$link;
}
if ( $row =~ /<th.*?>(.+?)<\/th>|<td.*?>(.+?)<\/td>/is )
{
my $content = $1;
if ( $content )
{
if ( $content =~ /length|width|height|color|shape|type/is )
{
$has_dimensions = 1;
}
if ( $content =~ /material|quantity|brand|case|Warranty/is )
{
$has_details = 1;
}
if ( $content =~ /ISBN|author/is )
{
$has_book_detals = 1;
}
if ( $content =~ /Dimensions|Frame|fork|manual/is )
{
$has_product_detals = 1;
}
if ( $content =~ /Camera|Bluetooth|WiFi|SIM|2G|3G|4G|OS|RAM|Operating\s+System/is )
{
$has_electronics = 1;
}
}
}
}
if ( $dec_flag == 0 )
{
$has_attribs = 1;
}
if ( $row_count > 2 && $link > 3 )
{
$has_links = 1;
}
return ( $has_attribs,$has_links,$has_dimensions,$has_details,$has_book_detals,$has_product_detals,$has_electronics );
}
sub extractfeature
{
my $table = shift;
# Features
my $has_table = 0; # equal columns in all row
my $has_title = 0; # contains any of {Setting Information : Specifications : Components : Specs : Contributors : Book Details : FEATURES}
my $has_nested_table = 0; # { TABLE insde table }
my $has_no_row_col = 0; # { only table tag }
my $has_one_row = 0; # { row 1 }
my $has_one_col = 0; # { col 1 }
#functions
my ( $has_attribs,$has_links,$has_dimensions,$has_details,$has_book_detals,$has_product_detals,$has_electronics ) = &get_feature($table);
# variables
my ( $row_count, $head_count) = ( 0,0 );
my $col_count = 0;
my $tmp_head_count = 0;
my $total_col = 0;
my %h_row_col;
my $row_flag = 0;
my $has_attribs_flag = 0;
while ( $table =~ /<tr.*?>(.*?)<\/tr>/gs )
{
my $col = 0;
my $col_hd = 0;
++$row_count;
my $row = $1;
if ( $row_flag == 0 )
{
$row_flag = 1;
}
if ( $row =~ /<table.*?<\/table>/s )
{
$has_nested_table = 1;
last;
}
while ( $row =~ /<th.*?>(.*?)<\/th>/gs )
{
my $head_cont = $1;
if ( $head_cont =~ /Setting\s+Information|Specifications|Components|Specs|Contributors|Book\s+Details|FEATURES/si )
{
$has_title = 1;
}
if ( $row_count == 1)
{
++$head_count;
}
else
{
++$col_hd;
++$tmp_head_count;
if ( $col_hd == 1 )
{
my $col_cont = $1;
}
}
}
while ( $row =~ /<td.*?>(.*?)<\/td>/gs )
{
++$col;
if ( $col == 1 )
{
my $col_cont = $1;
}
}
if ( $col_count != 0 )
{
if ( $col_count == $col )
{
$has_table = 1;
}
else
{
$has_table = 0;
last;
}
}
elsif ( defined $col )
{
$col_count = $col;
}
if ( defined $col )
{
$total_col += $col;
}
}
if ( $row_flag == 0)
{
$has_no_row_col = 1;
}
if ( $row_count == 2 )
{
if ( ( $head_count == $col_count or $head_count = $tmp_head_count ) ) # && $has_table eq 0
{
$has_table = 1;
}
$total_col += $head_count;
}
# Removing this feature
# my $avg_table_col = 0;
# if ( $row_count > 0 )
# {
# $avg_table_col = (1.0 * $total_col) / $row_count;
# }
if ( $row_count == 1 ) # or ( $col_count == 1 && $row_count > 1 )
{
$has_one_row = 1;
}
if ( $col_count == 1 )
{
$has_one_col = 1;
}
return ( $has_table, $has_nested_table, $has_no_row_col, $has_one_row,$has_one_col,$row_count,$col_count,$head_count,$has_title,$has_attribs,$has_links,$has_dimensions,$has_details,$has_book_detals,$has_product_detals,$has_electronics);
}
my ($in_file, $out_file ) = @ARGV;
open ( OUT, ">$out_file.train.feature" ) || die $!;
print OUT "#has_table\thas_neted_table\thas_no_row_col\thas_one_row\thas_one_col\trow_count\tcol_count\thead_count\thas_title\thas_attribs\thas_links\thas_dimensions\thas_details\thas_book_detals\thas_product_detals\thas_electronics\tanswer_label\n";
open ( TRAIN ,'<', $in_file ) || die $!;
local $/;
my $text = <TRAIN>;
close (TRAIN);
my $i = 0;
my $count = 0;
while ($text =~ /(no|yes),"(<table.*?<\/table>)"/sig)
{
my $label = $1;
if ( $label eq "no" )
{
$label = 0;
}
elsif ( $label eq "yes" )
{
$label = 1;
}
my $tabel = $2;
my ($has_table, $has_nested_table, $has_no_row_col, $has_one_row, $has_one_col,$row_count,$col_count,$head_count,$has_title,$has_attribs,$has_links,$has_dimensions,$has_details,$has_book_detals,$has_product_detals,$has_electronics) = &extractfeature($tabel);
print OUT "$has_table\t$has_nested_table\t$has_no_row_col\t$has_one_row\t$has_one_col\t$row_count\t$col_count\t$head_count\t$has_title\t$has_attribs\t$has_links\t$has_dimensions\t$has_details\t$has_book_detals\t$has_product_detals\t$has_electronics\t$label\n";
# print OUT1 "$label\t$tabel\t$url\n";
}