Distributions_and_Strings.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Distributions_and_Strings.cpp
  2. *
  3. * Copyright (C) 1997-2011,2014,2015,2016,2017 Paul Boersma
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "Distributions_and_Strings.h"
  19. autoStrings Distributions_to_Strings (Distributions me, integer column, integer numberOfStrings) {
  20. try {
  21. autoStrings thee = Thing_new (Strings);
  22. thy numberOfStrings = numberOfStrings;
  23. thy strings = autostring32vector (numberOfStrings);
  24. for (integer istring = 1; istring <= numberOfStrings; istring ++) {
  25. conststring32 string;
  26. Distributions_peek (me, column, & string, nullptr);
  27. thy strings [istring] = Melder_dup (string);
  28. }
  29. return thee;
  30. } catch (MelderError) {
  31. Melder_throw (me, U": Strings not generated.");
  32. }
  33. }
  34. autoStrings Distributions_to_Strings_exact (Distributions me, integer column) {
  35. try {
  36. integer total = 0;
  37. integer istring = 0;
  38. if (column > my numberOfColumns)
  39. Melder_throw (U"No column ", column, U".");
  40. if (my numberOfRows < 1)
  41. Melder_throw (U"No candidates.");
  42. for (integer irow = 1; irow <= my numberOfRows; irow ++) {
  43. double value = my data [irow] [column];
  44. if (value != Melder_roundDown (value))
  45. Melder_throw (U"Non-integer value ", value, U" in row ", irow, U".");
  46. if (value < 0.0)
  47. Melder_throw (U"Found a negative value ", value, U" in row ", irow, U".");
  48. total += value;
  49. }
  50. if (total <= 0)
  51. Melder_throw (U"Column total not positive.");
  52. autoStrings thee = Thing_new (Strings);
  53. thy numberOfStrings = total;
  54. thy strings = autostring32vector (total);
  55. for (integer irow = 1; irow <= my numberOfRows; irow ++) {
  56. integer number = my data [irow] [column];
  57. conststring32 string = my rowLabels [irow].get();
  58. if (! string)
  59. Melder_throw (U"No string in row ", irow, U".");
  60. for (integer i = 1; i <= number; i ++)
  61. thy strings [++ istring] = Melder_dup (string);
  62. }
  63. Strings_randomize (thee.get());
  64. return thee;
  65. } catch (MelderError) {
  66. Melder_throw (me, U": Strings not generated.");
  67. }
  68. }
  69. autoDistributions Strings_to_Distributions (Strings me) {
  70. try {
  71. autoDistributions thee = Distributions_create (my numberOfStrings, 1);
  72. integer idist = 0;
  73. for (integer i = 1; i <= my numberOfStrings; i ++) {
  74. conststring32 string = my strings [i].get();
  75. integer where = 0;
  76. integer j = 1;
  77. for (; j <= idist; j ++) {
  78. if (str32equ (thy rowLabels [j].get(), string)) {
  79. where = j;
  80. break;
  81. }
  82. }
  83. if (where) {
  84. thy data [j] [1] += 1.0;
  85. } else {
  86. thy rowLabels [++ idist] = Melder_dup (string);
  87. thy data [idist] [1] = 1.0;
  88. }
  89. }
  90. thy numberOfRows = idist;
  91. TableOfReal_sortByLabel (thee.get(), 1, 0);
  92. return thee;
  93. } catch (MelderError) {
  94. Melder_throw (me, U": distribution not computed.");
  95. }
  96. }
  97. /* End of file Distributions_and_Strings.cpp */