-
Notifications
You must be signed in to change notification settings - Fork 1
/
arff2snns.pl
executable file
·201 lines (171 loc) · 4.88 KB
/
arff2snns.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
#!/usr/bin/perl -s
#*************************************************************************
#
# Program: arff2snns
# File: arff2snns.pl
#
# Version: V1.0
# Date: 14.06.21
# Function: Convert ARFF file to SNNS .pat format
#
# Copyright: (c) Prof. Andrew C. R. Martin, UCL, 2021
# Author: Prof. Andrew C. R. Martin
# Address: Institute of Structural and Molecular Biology
# Division of Biosciences
# University College
# Gower Street
# London
# WC1E 6BT
# EMail: andrew@bioinf.org.uk
#
#*************************************************************************
#
# This program is not in the public domain, but it may be copied
# according to the conditions laid out in the accompanying file
# COPYING.DOC
#
# The code may be modified as required, but any modifications must be
# documented so that the person responsible can be identified. If
# someone else breaks this code, I don't want to be blamed for code
# that does not work!
#
# The code may not be sold commercially or included as part of a
# commercial product except as described in the file COPYING.DOC.
#
#*************************************************************************
#
# Description:
# ============
#
#*************************************************************************
#
# Usage:
# ======
# Converts an ARFF file to SNNS .pat format
#
#*************************************************************************
#
# Revision History:
# =================
# V1.0 14.06.21 Original
#
#*************************************************************************
use strict;
my $input = "";
# Check for -h - help request
UsageDie() if(defined($::h));
my $inData = 0;
my @data = ();
while(<>)
{
chomp;
s/^\s+//;
if(length)
{
if(/\@relation/)
{
}
elsif(/\@attribute/)
{
my @fields = split;
if(BadAttributes($fields[2]))
{
print STDERR "\nThe current version does not handle non-Boolean class attributes:\n";
print STDERR " $_\n\n";
exit 1;
}
}
elsif(/\@data/)
{
$inData = 1;
}
elsif($inData)
{
if(/\?/)
{
printf STDERR "\nLine containing undefined values skipped\n";
printf STDERR " $_\n";
}
else
{
my $dataLine = ConvertLine($_);
push @data, "$dataLine";
}
}
}
}
PrintSNNSHeader(\@data);
PrintData(\@data);
#*************************************************************************
sub PrintData
{
my($aData) = @_;
my $count = 1;
foreach my $datum (@$aData)
{
my @fields = split(/\s+/,$datum);
my $out = pop(@fields);
print "# Input line $count\n";
print "@fields\n";
print "# Output\n";
print "$out\n";
$count++;
}
}
#*************************************************************************
sub BadAttributes
{
my($field) = @_;
$field =~ s/numeric//i; # Remove numeric
$field =~ s/\,//g; # Remove commas
$field =~ s/\{//; # Remove {
$field =~ s/\}//; # Remove }
$field =~ s/TRUE//i; # Remove TRUE
$field =~ s/FALSE//i; # Remove FALSE
return(1) if(length($field));
return(0);
}
#*************************************************************************
sub ConvertLine
{
my($line) = @_;
$line =~ s/FALSE/0/g;
$line =~ s/TRUE/1/g;
$line =~ s/\,/ /g;
return($line);
}
#*************************************************************************
sub PrintSNNSHeader
{
my($aData) = @_;
my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my @days = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
my $dateString = localtime();
my @fields = split(/\s+/, @$aData[0]);
print "SNNS pattern definition file V3.2\n";
print "generated at $dateString\n";
print "\n";
print "\n";
print "\n";
printf("No. of patterns : %d\n", scalar(@$aData));
printf("No. of input units : %d\n", scalar(@fields) - 1);
print "No. of output units : 1\n";
print "\n";
}
#*************************************************************************
sub UsageDie
{
print <<__EOF;
arff2snns V1.0 (c) UCL, Prof. Andrew C.R. Martin
Usage: arff2snns [input.arff] > snns.pat
This is a very simple program to convert Weka ARFF files to SNNS .pat
pattern files. If no input file is specified, it reads from standard
input.
Limitations:
- It can only handle numeric and Boolean attributes (including for
outputs)
- It can only handle a single output which must be the last field in the
.arff file
__EOF
exit 0;
}