Advertisement
Matthen

Confusion Networks from Speech Recognition

Dec 8th, 2011
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. cnet = {
  2. {{"Hi", 0.7}, {"My", 0.1}, {"I", 0.1}, {"-", 0.1}},
  3. {{"my", 0.5}, {"-", 0.3}, {"I'm", 0.2}},
  4. {{"name", 0.8}, {"named", 0.2}},
  5. {{"is", 0.6}, {"uh", 0.3}, {"-", 0.1}},
  6. {{"Matt", 0.5}, {"not", 0.3}, {"-", 0.2}},
  7. {{"-", 0.9}, {"eh", 0.1}}
  8. };
  9.  
  10. PlotCNet[c_] :=
  11. Module[{cnet = Map[SortBy[#, Last] &, c], l}, Graphics[{
  12. Table[Table[l = Length@cnet[[i]]; {
  13. Thickness[Exp[cnet[[i, j, 2]]]/300],
  14. Blend[{Red, Blue}, cnet[[i, j, 2]]],
  15. BezierCurve[{{i, 0}, {i + 0.5, j - (l + 1)/2}, {i + 1, 0}}],
  16. EdgeForm[Black], FaceForm[White],
  17. Rectangle[{i + 0.5, (j - (l + 1)/2)/2} - {0.3,
  18. 0.1}, {i + 0.5, (j - (l + 1)/2)/2} + {0.3, 0.1}],
  19. Black,
  20. Text[cnet[[i, j, 1]], {i + 0.5, (j - (l + 1)/2)/2}]
  21. }, {j, Length@cnet[[i]]}], {i, Length@cnet}]
  22. }]];
  23.  
  24. NBest[cnet_] := Module[{top = {{0, {""}}}, newtop = {}},
  25. Do[
  26. newtop = {};
  27. Do[
  28. AppendTo[
  29. newtop, {top[[i, 1]] + Log[cnet[[j, k, 2]]],
  30. Join[top[[i, 2]], {cnet[[j, k, 1]]}]}]
  31. ,
  32. {i, Length@top}, {k, Length@cnet[[j]]}];
  33. newtop = Reverse@SortBy[newtop, First];
  34. top = newtop[[;; Min[20, Length@newtop]]];
  35. , {j, Length@cnet}];
  36. top = top[[;; Min[10, Length@top]]];
  37. Table[{Exp[3*First[t]]/Total[Exp[3*First /@ top]], Last[t]}, {t,
  38. top}]
  39. ];
  40.  
  41. nbest = NBest[cnet];
  42.  
  43. DrawHyp[c_, hyp_, t_, n_] :=
  44. Module[{i, cnet = Map[SortBy[#, Last] &, c], points},
  45. points =
  46. Table[{{i,
  47. 0}, {i +
  48. 0.5, (Position[First /@ cnet[[i]], hyp[[2, i + 1]]])[[1,
  49. 1]] - (Length[cnet[[i]]] + 1)/2}, {i + 1, 0}}, {i,
  50. Length@cnet}];
  51. points = Map[f[#, t, n] &, points, {2}];
  52. Show[
  53. PlotCNet[cnet],
  54. Graphics[{Thickness[Exp[hyp[[1]]]/300],
  55. Blend[{Red, Blue}, hyp[[1]]],
  56. BezierCurve[points], Black,
  57. i = 1;
  58. Table[i++;
  59. Text[hyp[[2, i]],
  60. p + {0, 0.1 t}], {p, ({First@#, Last@#/(2 - t)}) & /@
  61. Partition[Flatten[Map[#[[2]] &, points]], 2]}]}]
  62. ]
  63. ];
  64. f[p_, t_, n_] := (1 - t) p + t {p[[1]]/2, 2.5 - n/4};
  65.  
  66. NBestAnim[cnet_, nbest_, t_] := Module[{n, \[Tau], bg},
  67. n = Floor[t] + 1;
  68. \[Tau] = t - Floor[t];
  69. bg = Show[
  70. Join[{Graphics[{}],
  71. Table[DrawHyp[cnet, nbest[[i]], 1, i], {i, n - 1}]}]];
  72. Show[bg, DrawHyp[cnet, nbest[[n]], \[Tau], n]]
  73. ];
  74. Manipulate[NBestAnim[cnet, nbest, t], {t, 0, 5}]
  75.  
  76.  
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement