postprocess.awk 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #
  2. # Copyright (C) 2005, 2006 Stephen Jungels
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but
  10. # WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # General Public License for more details.
  13. #
  14. # See COPYING for the full text of the license.
  15. # setup: get and optionally shuffle the hints
  16. state==0 {
  17. srand();
  18. numhints = split (hints, hint, "::");
  19. for (i=1; i<=numhints; i++)
  20. {
  21. hintindex[i] = i;
  22. if (order=="stripe2")
  23. {
  24. j = int (rand() * numhints) + 1;
  25. k = hint[i];
  26. hint[i] = hint[j];
  27. hint[j] = k;
  28. }
  29. }
  30. state=1;
  31. }
  32. # first pass: gather hint statistics
  33. state==1 {
  34. for (i=1; i<=numhints; i++)
  35. {
  36. if (tolower($0) ~ tolower(hint[i]))
  37. {
  38. hintmatches[i] += 1;
  39. }
  40. }
  41. }
  42. # end of first pass: calculate summary statistics
  43. # and probability factors
  44. state==2 {
  45. min=999999;
  46. max=0;
  47. total=0;
  48. goodhints=0;
  49. for (i=1; i<=numhints; i++)
  50. {
  51. if (hintmatches[i] < min && hintmatches[i] > 0)
  52. {
  53. min = hintmatches[i];
  54. }
  55. if (hintmatches[i] > max)
  56. {
  57. max = hintmatches[i];
  58. }
  59. total += hintmatches[i];
  60. if (hintmatches[i] > 0) goodhints++;
  61. }
  62. if (min==999999) min = 0;
  63. if (goodhints==0) goodhints=1;
  64. avg = total / goodhints;
  65. if (tracks==-1) mma = avg;
  66. else if (tracks==-2) mma = min;
  67. else if (tracks==-3) mma = max;
  68. else if (tracks==-4) mma = min;
  69. else mma = (tracks / goodhints);
  70. for (i=1; i<=numhints; i++)
  71. {
  72. if (hintmatches[i] > 0)
  73. hintfactors[i] = (mma / hintmatches[i]);
  74. else
  75. hintfactors[i] = 0;
  76. hintattempts[i] = 1;
  77. hintprints[i] = hintfactors[i];
  78. }
  79. if (order=="stripe2")
  80. {
  81. for (i=0; i<mma; i++) pos[i] = i;
  82. for (i=0; i<mma; i++)
  83. {
  84. j = int (rand() * mma);
  85. k = pos[i];
  86. pos[i] = pos[j];
  87. pos[j] = k;
  88. }
  89. for (i=1; i<=numhints; i++) posindex[i]=0;
  90. }
  91. state=3;
  92. }
  93. # generate a normally distributed random number
  94. function boxmuller(x1, x2, w)
  95. {
  96. if (gaussindex==1)
  97. {
  98. gaussindex = 0;
  99. return gauss1;
  100. }
  101. else
  102. {
  103. w = 1.0;
  104. while (w >= 1.0)
  105. {
  106. x1 = 2.0 * rand() - 1.0;
  107. x2 = 2.0 * rand() - 1.0;
  108. w = x1 * x1 + x2 * x2;
  109. }
  110. w = sqrt((-2.0 * log(w)) / w );
  111. gauss1 = x1 * w;
  112. gaussindex = 1;
  113. return (x2 * w);
  114. }
  115. }
  116. # print a track with a leading index such that the
  117. # tracks will be ordered properly when the list is
  118. # sorted
  119. function printtrack(track, i, songindex, n, tries)
  120. {
  121. if (order=="stripe")
  122. {
  123. songindex = hintindex[i];
  124. hintindex[i] += numhints;
  125. print songindex "\t" track;
  126. }
  127. else if (order=="stripe2")
  128. {
  129. if (posindex[i]<mma)
  130. songindex = pos[posindex[i]];
  131. else
  132. songindex = posindex[i];
  133. posindex[i] += 1;
  134. songindex += (i / numhints);
  135. print songindex "\t" track;
  136. }
  137. else if (order=="stripe3")
  138. {
  139. n = int (rand() * mma);
  140. tries = 0;
  141. while (slots[n,i]==1 && tries < 40)
  142. {
  143. n = int (rand() * mma);
  144. printf "." > "/dev/stderr";
  145. tries++;
  146. }
  147. tries = 0;
  148. while (slots[n,i]==1 && tries < 40)
  149. {
  150. n = int (rand() * max);
  151. printf "." > "/dev/stderr";
  152. tries++;
  153. }
  154. if (slots[n,i]==1)
  155. {
  156. for (n=0; n<=max; n++)
  157. {
  158. if (slots[n,i]==0) break;
  159. printf "." > "/dev/stderr";
  160. }
  161. }
  162. slots[n,i] = 1;
  163. songindex = n + (i / numhints);
  164. print songindex "\t" track;
  165. }
  166. else if (order=="fade")
  167. {
  168. songindex = boxmuller() + (1.2 * i);
  169. print songindex "\t" track;
  170. }
  171. else if (order=="random")
  172. {
  173. songindex = rand();
  174. print songindex "\t" track;
  175. }
  176. else if (order=="sort")
  177. {
  178. print track;
  179. }
  180. else if (order=="group")
  181. {
  182. songindex = rand() + i;
  183. print songindex "\t" track;
  184. }
  185. }
  186. # second pass: use statistics and random fudge
  187. # to decide whether to print each track
  188. state==3 {
  189. for (i=1; i<=numhints; i++)
  190. {
  191. if (tolower($0) ~ tolower(hint[i]))
  192. {
  193. if (tracks==-4)
  194. {
  195. printtrack($0, i);
  196. break;
  197. }
  198. else
  199. {
  200. hf = (2 * hintfactors[i] - (hintprints[i] / hintattempts[i]));
  201. hintattempts[i]++;
  202. r = rand();
  203. if (hf > r)
  204. {
  205. while (hf > r)
  206. {
  207. hintprints[i]++;
  208. printtrack($0, i);
  209. hf--;
  210. }
  211. break;
  212. }
  213. }
  214. }
  215. }
  216. }