CMiniLexicon::FindMajorSignatures(): use log file routines
[linguistica.git] / DCNnetworksyl.cpp
blob5eb2232faca5556436be7b60f041edeee5ab9758
1 // Implementation of networksyl methods
2 // Copyright © 2009 The University of Chicago
3 #include "DCNnetworksyl.h"
4 #include <QTextStream>
5 #include <QIODevice>
6 #include <QString>
7 #include <QLabel>
9 //////////////////////////////////////////////////////////////////////
10 // Construction/Destruction
11 //////////////////////////////////////////////////////////////////////
13 networksyl::networksyl()
15 converged = false;
18 networksyl::~networksyl()
20 // delete node;
21 // delete sonority;
25 void networksyl::setGrammar(grammarsyl* theGrammar)
27 this->theGrammar = theGrammar;
30 void networksyl::setWord(QString word)
32 this->word = word;
36 void networksyl::equilibrium()
38 //definitions of network values
39 float alpha = theGrammar->getAlpha();
40 float beta = theGrammar->getBeta();
41 int syl = syllable();
42 converged = false;
44 // the following is the meat of DCNs
46 // oldIteration lets us know when we've converged
47 // also initialize node[] and sonority[]
48 float* oldIteration = new float[syl];
49 node = new float[syl];
50 sonority = new float[syl];
51 for (int i = 0; i < syl; i++)
53 oldIteration[i] = -2.0;
54 node[i] = 0.0;
55 sonority[i] = theGrammar->getSonority(word.at(i));
59 // the DCN goes for 255 cylces before giving up
60 for (int cycle = 0; cycle < 256; cycle++)
62 //run the network
63 for (int s = 0; s < syl; s++)
65 if (s == 0)
66 node[s] = alpha * node[s+1] + sonority[s];
67 else if (s == syl-1)
68 node[s] = beta * node[s-1] + sonority[s];
69 else
70 node[s] = beta * node[s-1] + alpha * node[s+1] + sonority[s];
73 //calculate the distance
74 float distance = 0.0;
75 for (int i = 0; i < syl; i++)
76 distance += ((node[i] - oldIteration[i]) * (node[i] - oldIteration[i]));
78 //has the network converged?
79 float DELTA = 0.0001f;
80 if (distance < DELTA)
82 converged = true;
83 break;
86 //store this iteration as old iteration
87 for (int j = 0; j < syl; j++)
88 oldIteration[j] = node[j];
90 //last cycle, the network has failed to converge
91 if (cycle == 255)
92 converged = false;
95 delete[] oldIteration;
99 void networksyl::print(QLabel* label)
101 //definitions of network values
102 float alpha = theGrammar->getAlpha();
103 float beta = theGrammar->getBeta();
104 int syl = syllable();
106 QString totalString;
107 QTextStream s(&totalString, QIODevice::Append);
109 s << "The values of the grammar:\n";
110 s << "\talpha:\t\t" << alpha << '\n';
111 s << "\tbeta:\t\t" << beta << '\n';
113 s << "The value of the nodes:\n";
114 s << "\tletter\t\tinherant sonority\t\tderived sonority\n";
116 for (int i = 0; i < syl; i++) {
117 s << "\t" << word.at(i) << "\t\t";
118 s << sonority[i] << " \t\t\t";
119 s << node[i] << "\n";
122 s << "\n\n";
124 s << "The syllabification of the word is:\t\t";
125 if (isConverged())
126 s << sylPrintout();
127 else
128 s << "not converged!";
130 label->setText(totalString);
133 // a complicated printing function
134 QString networksyl::sylPrintout() {
135 QString output;
136 QTextStream out(&output, QIODevice::Append);
138 out << "\t";
140 int syl = syllable();
142 //gold.smith
143 bool b = false;
144 for (int r = 1; r < syl; ++r) {
145 if (node[r-1] < node[r]) {
146 if (b)
147 out << ".";
148 b = false;
149 } else
150 b = true;
151 out << word.at(r-1);
153 out << word.at(word.length()-1) << "\t\n\n";
155 out << "phonemes:\t\t";
156 //g o l d s m I th
157 for (int w = 1; w < syl; ++w)
158 out << word.at(w-1) << "\t";
160 out << word.at(word.length()-1) << "\t\n\n";
162 out << "min/max/other:\t\t";
163 //L H O O L O H L
164 for (int s = 0; s < syl; ++s) {
165 if (s == 0) {
166 if (node[s] > node[s+1])
167 out << "H\t";
168 else if (node[s] < node[s+1])
169 out << "L\t";
170 else
171 out << "O\t";
172 } else if (s == syl-1) {
173 if (node[s] > node[s-1])
174 out << "H\t";
175 else if (node[s] < node[s-1])
176 out << "L\t";
177 else
178 out << "O\t";
179 } else {
180 if ((node[s] > node[s-1]) && (node[s] > node[s+1]))
181 out << "H\t";
182 else if ((node[s] < node[s-1]) && (node[s] < node[s+1]))
183 out << "L\t";
184 else
185 out << "O\t";
188 out << "\n\n";
190 out << "upward/downward:\t";
191 //U D D D U U D d
192 for (int t = 1; t < syl; ++t) {
193 if (node[t-1] < node[t])
194 out << "U\t";
195 else
196 out << "D\t";
198 out << "d\n\n";
200 out << "onset/nucleus/coda:\t";
201 //O N C C O O N C
202 bool inCoda = false;
203 for (int q = 0; q < syl; ++q) {
204 if (q == 0) {
205 if (node[q] > node[q+1]) {
206 out << "N\t";
207 inCoda = true;
208 } else /* if (node[q] < node[q+1])*/
209 out << "O\t";
210 } else if (q == syl-1) {
211 if (node[q] > node[q-1]) {
212 out << "N\t";
213 inCoda = true;
214 } else /* if (node[q] < node[q-1])*/
215 out << "C\t";
216 } else {
217 if ((node[q] > node[q-1]) && (node[q] > node[q+1])) {
218 out << "N\t";
219 inCoda = true;
220 } else if ((node[q] < node[q-1]) && (node[q] < node[q+1])) {
221 out << "O\t";
222 inCoda = false;
223 } else {
224 if (inCoda)
225 out << "C\t";
226 else
227 out << "O\t";
231 out << "\n\n";
233 return output;
236 QString networksyl::getMaxima() const
238 QString output;
239 QTextStream out(&output, QIODevice::Append);
241 //L H O O L O H L
242 for (int s = 0; s < syllable(); ++s) {
243 if (s == 0) {
244 if (node[s] > node[s+1])
245 out << "H";
246 else if (node[s] < node[s+1])
247 out << "L";
248 else
249 out << "O";
250 } else if (s == syllable()-1) {
251 if (node[s] > node[s-1])
252 out << "H";
253 else if (node[s] < node[s-1])
254 out << "L";
255 else
256 out << "O";
257 } else {
258 if ((node[s] > node[s-1]) && (node[s] > node[s+1]))
259 out << "H";
260 else if ((node[s] < node[s-1]) && (node[s] < node[s+1]))
261 out << "L";
262 else
263 out << "O";
267 return output;
270 QString networksyl::getSyllabifiedWord() const
272 QString output;
273 QTextStream out(&output, QIODevice::Append);
275 //gold.smith
276 bool b = false;
277 for (int r = 1; r < syllable(); r++) {
278 if (node[r-1] < node[r]) {
279 if (b)
280 out << ".";
281 b = false;
282 } else
283 b = true;
284 out << word.at(r-1);
286 out << word.at(word.length()-1);
288 return output;