Debian Codegraph @

stat/Distributions_and_Strings.cpp

raw symbols SCIP
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
19 #include "Distributions_and_Strings.h"
20
21 autoStrings Distributions_to_Strings (Distributions me, integer column, integer numberOfStrings) {
22 try {
23 autoStrings thee = Thing_new (Strings);
24 thy numberOfStrings = numberOfStrings;
25 thy strings = autoSTRVEC (numberOfStrings);
26 for (integer istring = 1; istring <= numberOfStrings; istring ++) {
27 conststring32 string;
28 Distributions_peek (me, column, & string, nullptr);
29 thy strings [istring] = Melder_dup (string);
30 }
31 return thee;
32 } catch (MelderError) {
33 Melder_throw (me, U": Strings not generated.");
34 }
35 }
36
37 autoStrings Distributions_to_Strings_exact (Distributions me, integer column) {
38 try {
39 integer total = 0;
40 integer istring = 0;
41 if (column > my numberOfColumns)
42 Melder_throw (U"No column ", column, U".");
43 if (my numberOfRows < 1)
44 Melder_throw (U"No candidates.");
45 for (integer irow = 1; irow <= my numberOfRows; irow ++) {
46 double value = my data [irow] [column];
47 if (value != Melder_roundDown (value))
48 Melder_throw (U"Non-integer value ", value, U" in row ", irow, U".");
49 if (value < 0.0)
50 Melder_throw (U"Found a negative value ", value, U" in row ", irow, U".");
51 total += value;
52 }
53 if (total <= 0)
54 Melder_throw (U"Column total not positive.");
55 autoStrings thee = Thing_new (Strings);
56 thy numberOfStrings = total;
57 thy strings = autoSTRVEC (total);
58 for (integer irow = 1; irow <= my numberOfRows; irow ++) {
59 integer number = my data [irow] [column];
60 conststring32 string = my rowLabels [irow].get();
61 if (! string)
62 Melder_throw (U"No string in row ", irow, U".");
63 for (integer i = 1; i <= number; i ++)
64 thy strings [++ istring] = Melder_dup (string);
65 }
66 Strings_randomize (thee.get());
67 return thee;
68 } catch (MelderError) {
69 Melder_throw (me, U": Strings not generated.");
70 }
71 }
72
73 autoDistributions Strings_to_Distributions (Strings me) {
74 try {
75 autoDistributions thee = Distributions_create (my numberOfStrings, 1);
76 integer idist = 0;
77 for (integer i = 1; i <= my numberOfStrings; i ++) {
78 conststring32 string = my strings [i].get();
79 integer where = 0;
80 integer j = 1;
81 for (; j <= idist; j ++) {
82 if (str32equ (thy rowLabels [j].get(), string)) {
83 where = j;
84 break;
85 }
86 }
87 if (where) {
88 thy data [j] [1] += 1.0;
89 } else {
90 thy rowLabels [++ idist] = Melder_dup (string);
91 thy data [idist] [1] = 1.0;
92 }
93 }
94 thy numberOfRows = idist;
95 TableOfReal_sortByLabel (thee.get(), 1, 0);
96 return thee;
97 } catch (MelderError) {
98 Melder_throw (me, U": distribution not computed.");
99 }
100 }
101
102 /* End of file Distributions_and_Strings.cpp */