File: postprocess.awk

package info (click to toggle)
plait 1.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 276 kB
  • sloc: sh: 2,010; awk: 709; makefile: 10
file content (238 lines) | stat: -rwxr-xr-x 4,680 bytes parent folder | download | duplicates (5)
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
#
# Copyright (C) 2005, 2006 Stephen Jungels
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.  
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# See COPYING for the full text of the license.


# setup: get and optionally shuffle the hints

state==0 {
  srand();
  numhints = split (hints, hint, "::");
  for (i=1; i<=numhints; i++)
  {
    hintindex[i] = i;
    if (order=="stripe2")
    {
      j = int (rand() * numhints) + 1;
      k = hint[i];
      hint[i] = hint[j];
      hint[j] = k;
    }
  }
  state=1;
}


# first pass: gather hint statistics

state==1 {
  for (i=1; i<=numhints; i++) 
  {
    if (tolower($0) ~ tolower(hint[i]))
    {
      hintmatches[i] += 1;
    }
  }
}


# end of first pass: calculate summary statistics
# and probability factors

state==2 {
  min=999999;
  max=0;
  total=0;
  goodhints=0;

  for (i=1; i<=numhints; i++)
  {
    if (hintmatches[i] < min && hintmatches[i] > 0)
    {
      min = hintmatches[i];
    }
    if (hintmatches[i] > max)
    {
      max = hintmatches[i];
    }
    total += hintmatches[i];
    if (hintmatches[i] > 0) goodhints++;
  }
  if (min==999999) min = 0;
  if (goodhints==0) goodhints=1;
  avg = total / goodhints;

  if (tracks==-1) mma = avg;
  else if (tracks==-2) mma = min;
  else if (tracks==-3) mma = max;
  else if (tracks==-4) mma = min;
  else mma = (tracks / goodhints);
  for (i=1; i<=numhints; i++)
  {
    if (hintmatches[i] > 0)
      hintfactors[i] = (mma / hintmatches[i]);
    else
      hintfactors[i] = 0;
    hintattempts[i] = 1;
    hintprints[i] = hintfactors[i];
  }

  if (order=="stripe2")
  {
    for (i=0; i<mma; i++) pos[i] = i;
    for (i=0; i<mma; i++)
    {
      j = int (rand() * mma);
      k = pos[i];
      pos[i] = pos[j];
      pos[j] = k;
    }
    for (i=1; i<=numhints; i++) posindex[i]=0;
  }

  state=3;
}


# generate a normally distributed random number

function boxmuller(x1, x2, w)
{
  if (gaussindex==1)
  {
    gaussindex = 0;
    return gauss1;
  }
  else
  {
    w = 1.0;
    while (w >= 1.0) 
    {
      x1 = 2.0 * rand() - 1.0;
      x2 = 2.0 * rand() - 1.0;
      w = x1 * x1 + x2 * x2;
    } 
    w = sqrt((-2.0 * log(w)) / w );
    gauss1 = x1 * w;
    gaussindex = 1;
    return (x2 * w);
  }
}


# print a track with a leading index such that the
# tracks will be ordered properly when the list is
# sorted

function printtrack(track, i, songindex, n, tries)
{
  if (order=="stripe")
  {
    songindex = hintindex[i];
    hintindex[i] += numhints;
    print songindex "\t" track;
  }
  else if (order=="stripe2")
  {
    if (posindex[i]<mma)
      songindex = pos[posindex[i]];
    else 
      songindex = posindex[i];
    posindex[i] += 1;
    songindex += (i / numhints);
    print songindex "\t" track;
  }
  else if (order=="stripe3")
  {
    n = int (rand() * mma);
    tries = 0;
    while (slots[n,i]==1 && tries < 40)
    {
      n = int (rand() * mma);
      printf "." > "/dev/stderr";
      tries++;
    } 
    tries = 0;
    while (slots[n,i]==1 && tries < 40)
    {
      n = int (rand() * max);
      printf "." > "/dev/stderr";
      tries++;
    } 
    if (slots[n,i]==1)
    {
      for (n=0; n<=max; n++)
      {
        if (slots[n,i]==0) break;
        printf "." > "/dev/stderr";
      } 
    }
    slots[n,i] = 1;
    songindex = n + (i / numhints);
    print songindex "\t" track;
  }
  else if (order=="fade")
  {
    songindex = boxmuller() + (1.2 * i);
    print songindex "\t" track;
  }
  else if (order=="random")
  {
    songindex = rand();
    print songindex "\t" track;
  }
  else if (order=="sort")
  {
    print track;
  }
  else if (order=="group")
  {
    songindex = rand() + i;
    print songindex "\t" track;
  }
}


# second pass: use statistics and random fudge
# to decide whether to print each track

state==3 {
  for (i=1; i<=numhints; i++)
  {
    if (tolower($0) ~ tolower(hint[i]))
    {
      if (tracks==-4)
      {
        printtrack($0, i);
        break;
      }
      else
      {
        hf = (2 * hintfactors[i] - (hintprints[i] / hintattempts[i]));
        hintattempts[i]++;
        r = rand();
        if (hf > r)
        {
          while (hf > r)
          {
            hintprints[i]++;
            printtrack($0, i);
            hf--;
          }
	  break;
        }
      }
    }
  }
}