forked from mtlynch/crfpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge-models.pl
executable file
·141 lines (122 loc) · 2.61 KB
/
merge-models.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
#!/usr/bin/perl -w
use strict;
use warnings;
my %model_u;
my %model_b;
my $xsize = -1;
my $version = -1;
my $cost_factor = -1;
my $template = "";
my @class = ();
my $model_size = 0;
die "merge-models.pl files ... > new_text_file\n" if (scalar(@ARGV) == 0);
for my $file (@ARGV) {
print STDERR "reading $file ..\n";
# header
open(F, $file) || die "$file\n";
while (<F>) {
chomp;
last if (/^$/);
if (/xsize: (\S+)/) {
die "xsize $xsize != $1\n" if ($xsize != -1 && $xsize != $1);
$xsize = $1;
} elsif (/version: (\S+)/) {
die "version $version != $1\n" if ($version != -1 && $version != $1);
$version = $1;
} elsif (/cost-factor: (\S+)/) {
die "cost-factor $cost_factor != $1\n" if ($cost_factor != -1 && $cost_factor != $1);
$cost_factor = $1;
}
}
# class
my @tmp = ();
while (<F>) {
chomp;
last if (/^$/);
push @tmp, $_;
}
die "@tmp != @class\n" if (@class && join(" ", @tmp) ne join(" ", @class));
@class = @tmp;
# template
my $templ = "";
while (<F>) {
last if (/^$/);
$templ .= $_;
}
die "$templ != $template\n" if ($template ne "" && $template ne $templ);
$template = $templ;
# dic
my %u;
my %b;
while (<F>) {
chomp;
last if (/^$/);
my ($id, $v) = split;
if ($v =~ /^U/) {
$u{$v} = $id;
} elsif ($v =~ /^B/) {
$b{$v} = $id;
}
}
# weights
my @w;
while (<F>) {
chomp;
push @w, $_;
}
close(F);
## merge
{
my $size = scalar(@class);
for my $v (keys %u) {
my $id = $u{$v};
for (my $i = 0; $i < $size; ++$i) {
$model_u{$v}->[$i] += $w[$id + $i];
}
}
}
{
my $size = scalar(@class) * scalar(@class);
for my $v (keys %b) {
my $id = $b{$v};
for (my $i = 0; $i < $size; ++$i) {
$model_b{$v}->[$i] += $w[$id + $i];
}
}
}
++$model_size;
}
my $size = scalar(@class);
my $maxid = scalar(keys %model_u) * $size + scalar(keys %model_b) * $size * $size;
# output
print "version: $version\n";
print "cost-factor: 1\n";
print "maxid: $maxid\n";
print "xsize: $xsize\n";
print "\n";
print (join "\n", @class);
print "\n\n";
print $template;
print "\n";
my $id = 0;
my @w;
for my $v (sort keys %model_b) {
my $size = scalar(@class) * scalar(@class);
for (my $i = 0; $i < $size; ++$i) {
push @w, 1.0 * $model_b{$v}->[$i] / $model_size;
}
print "$id $v\n";
$id += $size;
}
for my $v (sort keys %model_u) {
my $size = scalar(@class);
for (my $i = 0; $i < $size; ++$i) {
push @w, 1.0 * $model_u{$v}->[$i] / $model_size;
}
print "$id $v\n";
$id += $size;
}
print "\n";
for (@w) {
print "$_\n";
}