This repository was archived by the owner on Sep 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmat2java.pl
More file actions
executable file
·115 lines (88 loc) · 2.42 KB
/
Copy pathmat2java.pl
File metadata and controls
executable file
·115 lines (88 loc) · 2.42 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
#!/usr/bin/perl -w
# mat2java - converts a .mat matrix into a form suitable for use in a
# ScoreMatrix subclass.
use Getopt::Std;
getopts ("mn:t:");
$classname = $opt_n || "NewMatrix";
$template = $opt_t || "ComparisonMatrix.tmpl";
################################################################
# Read the .mat file and build the letter-addressed hashes of values
# skip the comments
while (($line = <>) =~ /^\#/) {}
# store the letters (order seems weird...)
$line =~ s/\s+//g;
@letters = split (//, $line);
$nletters = @letters;
# now read each line, building an intermediate hash with only the
# letters featured in the .mat file
foreach (@letters) # read 1 line for each letter
{ # assuming they're in the same order
$line = <>;
@scores = split (/\s+/, $line);
warn ("horizontal and vertical orders differ!") if shift (@scores) ne $_;
# print "scores: " . join (', ', @scores) . "\n";
# now fill hash: $_ is the vertical letter, $letters[ $i ] the horizontal
for ($i = 0; $i < $nletters; $i++)
{
$scores_h{ $_ }{ $letters[ $i ] } = $scores[ $i ];
}
}
# time to fill the real hash (`matrix`), with all letters in the
# alphabet
@alphabet = qw (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z *);
foreach $out (@alphabet)
{
foreach $in (@alphabet)
{
if (! exists $scores_h{ $out }{ $in })
{
# at least 1 unknown letter
if ($out eq $in)
{
# when it's the same unkown letter
$matrix{ $out }{ $in } = $scores_h{ '*' }{ '*' };
}
else
{
# two different unkown letters
$matrix{ $out }{ $in } = $scores_h{ '*' }{ 'A' };
}
}
else
{
# ok, both letters are known
$matrix{ $out }{ $in } = $scores_h{ $out }{ $in };
}
}
}
# Now generate the java code, unless the user wants only the matrix
# (-m)
unless ($opt_m)
{
print <<"END";
// $classname.java
public class $classname extends ComparisonMatrix
{
$classname ()
{
int tmp [][] =
{
END
}
# print it
$format = "\t\t{" . ("%3d," x 26) . "%3d},\n"; # 26: # of letters in the Roman alphabet
foreach $out (@alphabet)
{
my @vals = ();
foreach $in (@alphabet)
{
push (@vals, $matrix{$out}{$in});
}
printf ($format, @vals);
}
unless ($opt_m)
{
open ('TMPL', "< $template") or
warn ("Could not open $template for reading ($!), printing matrix only.\n");
foreach (<TMPL>) { print; }
}