-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbrjoin.pl
153 lines (137 loc) · 3.7 KB
/
nbrjoin.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
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use Math::Trig;
binmode STDIN, ":utf8";
binmode STDOUT, ":utf8";
binmode STDERR, ":utf8";
my %d;
my %actives; # not-yet-paired nodes
my %edges;
my %vertices;
my $newnode = 0; # labels of interior nodes
my %iso;
my %colorbyfam;
my %colorbylang;
open(COLORS, "<:utf8", "colors.txt") or die "Could not open colors.txt: $!";
while (<COLORS>) {
chomp;
(my $familyname, my $c) = /^([^ ]+) ([^ ]+)$/;
$colorbyfam{$familyname} = $c;
}
close COLORS;
open(LANGS, "<:utf8", "langs.txt") or die "Could not open langs.txt: $!";
while (<LANGS>) {
chomp;
(my $langcode, my $isocode, my $familyname) = /^([^ ]+) ([^ ]+) ([^ ]+)$/;
$colorbylang{$langcode} = $colorbyfam{$familyname};
$iso{$langcode} = $isocode;
}
close LANGS;
# come into this with a d; compute Q, find min pair, add node, recompute d's
# keys of %actives are the as-yet unpaired nodes == labels on rows/cols of Q
sub compute_Q {
my $minQ = 1000000;
my $minL;
my $minR;
my $r = scalar keys %actives;
my %colsum; # $colsum{i} = sum_k d(i,k)
for my $i (keys %actives) {
my $tot = 0;
for my $k (keys %actives) {
$tot += $d{"$i|$k"};
}
$colsum{$i} = $tot;
}
for my $i (keys %actives) {
for my $j (keys %actives) {
if ($i lt $j) {
my $Qvalue = ($r-2)*$d{"$i|$j"} - $colsum{$i} - $colsum{$j};
if ($Qvalue < $minQ) {
$minQ = $Qvalue;
$minL = $i;
$minR = $j;
}
}
}
}
# create new node with label $newnode; remove its two children
$actives{$newnode} = 1;
$vertices{$newnode}++;
delete($actives{$minL});
delete($actives{$minR});
print STDERR "New node $newnode joins $minL and $minR...\n";
# compute distance between new node and the two it joins
if ($r == 2) {
$d{"$minL|$newnode"} = 0.5*$d{"$minL|$minR"}; # formula below reduces to this if 0/0 = 0
}
else {
$d{"$minL|$newnode"} = 0.5*($d{"$minL|$minR"} + ($colsum{$minL} - $colsum{$minR})/($r - 2));
}
$d{"$newnode|$minL"} = $d{"$minL|$newnode"};
$d{"$minR|$newnode"} = $d{"$minL|$minR"} - $d{"$minL|$newnode"};
$d{"$newnode|$minR"} = $d{"$minR|$newnode"};
$edges{"$newnode|$minL"} = $d{"$minL|$newnode"};
$edges{"$newnode|$minR"} = $d{"$minR|$newnode"};
# compute distance between new node and all the others
for my $n (keys %actives) {
$d{"$newnode|$n"} = 0.5*($d{"$minL|$n"} + $d{"$minR|$n"} - $d{"$minR|$minL"});
$d{"$n|$newnode"} = $d{"$newnode|$n"};
}
$newnode++;
return ($r > 2);
}
my $first;
chomp($first = <STDIN>);
$first =~ s/^[^,]*,//;
my @langlist;
for my $l (split(/,/,$first)) {
push @langlist, $l;
$vertices{$l}++;
}
while (<STDIN>) {
chomp;
(my $lang, my $cosines) = m/^([^,]+)(,.+)$/;
$actives{$lang} = 1;
for my $other (@langlist) {
$cosines =~ s/^,([^,]+)//;
my $val = $1;
my $thedist = 0;
unless ($val eq '-') {
$thedist = acos($val); # true distance in high dim S^n
}
$d{"$lang|$other"} = $thedist;
$d{"$other|$lang"} = $thedist;
}
}
while (compute_Q()) {
1;
}
#my $url = 'http://www.ethnologue.com/show_language.asp?code=';
my $url = 'http://www.sil.org/iso639-3/documentation.asp?id=';
print "digraph G {\n";
print " edge [arrowhead=none];\n";
for my $v (keys %vertices) {
if ($v =~ /^[0-9]+$/) {
print " \"$v\" [shape=point];\n"
}
else {
my $c = $colorbylang{$v};
my $iso = $iso{$v};
print " \"$v\" [shape=box, width=0.1, height=0.1, style=filled, color=$c, label=\"$v\", URL=\"$url$iso\", target=\"_blank\"];\n"
}
}
for my $e (keys %edges) {
my $toprint = $e;
$toprint =~ s/^([^|]+)\|(.+)$/"$1" -> "$2"/g;
my $len = $edges{$e};
if ($len < 0.001) { # for perspective, acos(0.99) = 0.14153
$len = 0.001;
}
$len = sprintf("%.3f", 20*$len);
print " $toprint [len=$len];\n"
#print " $toprint [label=$len, len=$len];\n"
}
print "}\n";
exit 0;