/branches/LIUM/sphinx4/src/edu/cmu/sphinx/decoder/search/Partitioner.java

# · Java · 151 lines · 67 code · 18 blank · 66 comment · 14 complexity · fe6441a23ed07267625311fed4974dc4 MD5 · raw file

  1. /*
  2. * Copyright 1999-2002 Carnegie Mellon University.
  3. * Portions Copyright 2002 Sun Microsystems, Inc.
  4. * Portions Copyright 2002 Mitsubishi Electric Research Laboratories.
  5. * All Rights Reserved. Use is subject to license terms.
  6. *
  7. * See the file "license.terms" for information on usage and
  8. * redistribution of this file, and for a DISCLAIMER OF ALL
  9. * WARRANTIES.
  10. *
  11. */
  12. package edu.cmu.sphinx.decoder.search;
  13. /**
  14. * Partitions a list of tokens according to the token score.
  15. */
  16. public class Partitioner {
  17. /**
  18. * Partitions sub-array of tokens around the rth token.
  19. *
  20. * @param tokens the token array to partition
  21. * @param p the starting index of the subarray
  22. * @param r the pivot and the ending index of the subarray, inclusive
  23. *
  24. * @return the index (after partitioning) of the element
  25. * around which the array is partitioned
  26. */
  27. private int partitions(Token[] tokens, int p, int r) {
  28. Token pivot = tokens[r];
  29. int i = p - 1;
  30. for (int j = p; j < r; j++) {
  31. Token current = tokens[j];
  32. if (current.getScore() >= pivot.getScore()) {
  33. i++;
  34. setToken(tokens, j, tokens[i]);
  35. setToken(tokens, i, current);
  36. }
  37. }
  38. i++;
  39. setToken(tokens, r, tokens[i]);
  40. setToken(tokens, i, pivot);
  41. return i;
  42. }
  43. /**
  44. * Partitions sub-array of tokens around the x-th token by
  45. * selecting the midpoint of the token array as the pivot.
  46. *
  47. * @param tokens the token array to partition
  48. * @param p the starting index of the subarray
  49. * @param r the ending index of the subarray, inclusive
  50. *
  51. * @return the index of the element around which the array is partitioned
  52. */
  53. private int midPointPartition(Token[] tokens, int p, int r) {
  54. int i = (p + r)/2;
  55. Token temp = tokens[r];
  56. setToken(tokens, r, tokens[i]);
  57. setToken(tokens, i, temp);
  58. return partitions(tokens, p, r);
  59. }
  60. /**
  61. * Partitions the given array of tokens in place, so that the highest
  62. * scoring n token will be at the beginning of the array, not in any order.
  63. *
  64. * @param tokens the array of tokens to partition
  65. * @param size the number of tokens to partition
  66. * @param n the number of tokens in the final partition
  67. *
  68. * @return the index of the last element in the partition
  69. */
  70. public int partition(Token[] tokens, int size, int n) {
  71. /* if (tokens.length > n) {
  72. return midPointSelect(tokens, 0, size - 1, n);
  73. } else {
  74. int r = -1;
  75. float lowestScore = Float.MAX_VALUE;
  76. for (int i = 0; i < tokens.length; i++) {
  77. * je ne comprends pas ce code puisqu'il y a au plus size token dans le tableau. je l'ai remplace par :
  78. */
  79. if (size > n) {
  80. return midPointSelect(tokens, 0, size - 1, n);
  81. } else {
  82. int r = -1;
  83. float lowestScore = Float.MAX_VALUE;
  84. for (int i = 0; i < size; i++) {
  85. Token current = tokens[i];
  86. float currentScore = current.getScore();
  87. if (currentScore <= lowestScore) {
  88. lowestScore = currentScore;
  89. r = i; // "r" is the returned index
  90. }
  91. }
  92. // exchange tokens[r] <=> last token,
  93. // where tokens[r] has the lowest score
  94. int last = size - 1;
  95. if (last >= 0) {
  96. Token lastToken = tokens[last];
  97. setToken(tokens, last, tokens[r]);
  98. setToken(tokens, r, lastToken);
  99. }
  100. // return the last index
  101. return last;
  102. }
  103. }
  104. private void setToken(Token[] list, int index, Token token) {
  105. list[index] = token;
  106. token.setLocation(index);
  107. }
  108. /**
  109. * Selects the token with the ith largest token score.
  110. *
  111. * @param tokens the token array to partition
  112. * @param p the starting index of the subarray
  113. * @param r the ending index of the subarray, inclusive
  114. * @param i the token with the i-th largest score
  115. *
  116. * @return the index of the token with the ith largest score
  117. */
  118. private int midPointSelect(Token[] tokens, int p, int r, int i) {
  119. if (p == r) {
  120. return p;
  121. }
  122. int q = midPointPartition(tokens, p, r);
  123. int k = q - p + 1;
  124. if (i == k) {
  125. return q;
  126. } else if (i < k) {
  127. return midPointSelect(tokens, p, q - 1, i);
  128. } else {
  129. return midPointSelect(tokens, q + 1, r, i - k);
  130. }
  131. }
  132. }