-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmdl.cpp
More file actions
858 lines (769 loc) · 36.8 KB
/
Copy pathmdl.cpp
File metadata and controls
858 lines (769 loc) · 36.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
#include "qfile.h"
#include "mdl.h"
#include "Lexicon.h"
#include "WordCollection.h"
#include "log2.h"
// ============================== MDL ============================================ //
double CLexicon::compute_MDL(){
/*
Grammar:
List of affixes, each with a pointer to it and each with phonologicla content;
List of signatures:
List of pointers to affixes
List of stems, with each stem's content
List of words, with token counts
Total number of word topens
Derived:
Number of word types
Number of word tokens
Count of analyzed word types
Count of analyzed word tokens
*/
MDL MDL(this);
QFile distributions("MDL_distributions.txt");
distributions.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out_distributions(&distributions);
QFile summaries("MDL_summary_reports.txt");
summaries.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out_summaries(&summaries);
QFile corpus_costs("MDL_report_on_corpus_costs.txt");
corpus_costs.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out_corpus(&corpus_costs);
QFile grammar_file("MDL_grammar.txt");
grammar_file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out_grammar(&grammar_file);
CSignatureCollection * pSigs;
if (m_suffix_flag){
pSigs = m_Signatures;
} else{
pSigs = m_PrefixSignatures;
}
//------------------------------------------------------//
pSigs->sort(SIGS);
MDL.reports(out_summaries);
MDL.process_signatures(pSigs, out_distributions, out_summaries);
MDL.compute_stem_pointer_lengths_within_signature_Choice(out_distributions, out_summaries);
MDL.compute_signature_pointer_lengths_Choice(out_distributions, out_summaries);
MDL.process_stems_Content( out_distributions, out_summaries);
MDL.process_unanalyzed_words(out_distributions, out_summaries);
MDL.weigh_analyzed_and_unanalyzed_words(out_distributions, out_summaries);
MDL.process_affixes(out_distributions, out_summaries);
MDL.process_signatures_bis_Architecture(out_distributions, out_summaries);
MDL.corpus_description(out_corpus, out_summaries);
out_summaries << "Final report." << Qt::endl;
MDL.reports(out_summaries);
MDL.print_grammar(out_grammar);
grammar_file.close();
return MDL.this_MDL;
}
MDL::MDL(CLexicon* lexicon)
{
m_Lexicon = lexicon;
m_suffix_flag = lexicon->get_suffix_flag();
total_type_count_of_words = 0;
m_total_token_count_of_words = 0;
m_type_count_of_unanalyzed_words = 0.0;
m_token_count_of_unanalyzed_words= 0;
m_token_word_count_in_all_signatures = 0;
type_word_count_in_all_signatures = 0;
m_total_flagging_of_analyzed_words=0.0;
m_total_flagging_of_unanalyzed_words=0.0;
this_MDL = 0.0;
m_corpus_description_from_analyzed_words = 0.0;
m_corpus_description_from_unanalyzed_words= 0.0;
bits_per_letter = 0.0;
total_letter_count_in_affixes = 0.0;
total_letter_count_in_stems = 0.0;
signature_to_affix_pointers_Architexture_8 = 0.0;
stem_content_in_bits = 0.0;
stem_plus_affix_content_in_bits_Architecture_1 = 0.0;
grammar_length_in_bits = 0.0;
choice_of_stems_within_sigs_Choice_4 = 0.0;
total_cost_of_affix_choice_within_all_signatures_Choice_2 = 0.0;
}
/*
void MDL::analyze_all_words(){
QString word, signature, affix;
QMapIterator<QString, QString> iter1(word2sig);
while (iter1.hasNext()){
}
}
*/
double MDL::get_grammar_length_in_bits(){
return stem_plus_affix_content_in_bits_Architecture_1 +
total_cost_of_affix_choice_within_all_signatures_Choice_2 +
pointers_to_signatures_Choice_3 +
choice_of_stems_within_sigs_Choice_4 +
m_total_flagging_of_analyzed_words +
m_total_flagging_of_unanalyzed_words +
signature_to_affix_pointers_Architexture_8;
}
// #1
void MDL::process_signatures(CSignatureCollection* sigs, QTextStream & out_distributions, QTextStream & out_summaries){
for (int signo =0; signo < sigs->get_count(); signo++){
CSignature* sig = sigs->get_at_sorted(signo);
process_a_signature(sig,out_distributions, out_summaries);
}
out_summaries << "1 process signatures" << Qt::endl;
reports(out_summaries);
}
// #1a
void MDL::process_a_signature(CSignature* sig,QTextStream & out_distributions, QTextStream& out_summaries){
bool verbose_flag (true);
QString sigstring = sig->get_key();
if (!signatures.contains(sigstring)){
signatures.insert(sigstring,signatures.count());
}
signature2corpus_count[sigstring] = 0;
signature2stems[sigstring] = new QStringList;
QStringList stem_list;
m_affix2corpus_count_given_signature[sigstring] = new QMap<QString, double>;
foreach(QString stem, sig->get_stem_strings(stem_list)){
process_a_stem_given_a_signature_and_its_words(sig, stem, out_summaries);
}
if (verbose_flag){out_distributions << "1a Choice of affixes within a signature." << Qt::endl;}
double running_total = 0;
m_affix2pointer_length_affix_choice_within_signature[sigstring] = new QMap<QString, double>;
out_distributions <<
qSetFieldWidth(10) << sigstring <<
qSetFieldWidth(10) << "count" <<
qSetFieldWidth(10) << "ratio" <<
qSetFieldWidth(15) << "running total" <<
qSetFieldWidth(20) << "pointer length" <<
qSetFieldWidth(0) << Qt::endl;
foreach(QString affix, m_affix2corpus_count_given_signature[sigstring]->keys()){
int count = m_affix2corpus_count_given_signature[sigstring]->value(affix);
double ratio = double(count)/ double(signature2corpus_count[sigstring]);
double pointer_length = -1.0 * base2log( ratio );
m_affix2pointer_length_affix_choice_within_signature[sigstring]->insert(affix, pointer_length); // this variable is only affected here
total_cost_of_affix_choice_within_all_signatures_Choice_2 += pointer_length;
running_total += ratio;
if (verbose_flag){
QString affix1;
if (affix == "") {affix1 = "NULL";} else {affix1 = affix;}
out_distributions <<
qSetFieldWidth(10) << affix1 <<
qSetFieldWidth(10) << count <<
qSetFieldWidth(10) << ratio <<
qSetFieldWidth(15) << running_total <<
qSetFieldWidth(20) << pointer_length <<
qSetFieldWidth(0) << Qt::endl;
}
}
if (verbose_flag){out_distributions << Qt::endl<< " -------------------------------------------------" << Qt::endl;}
}
QString build_a_word(bool suffix_flag, QString stem, QString affix){
if (affix == "NULL" ) {affix = "";}
if (suffix_flag){
return stem + affix;
} else{
return affix + stem;
}
}
void MDL::add_letters_to_alphabet(QString word){
foreach(QString letter, word){
if (!alphabet.contains(letter)){
alphabet[letter]=1;
}else{
alphabet[letter] += 1;
}
}
}
// #1b
void MDL::process_a_stem_given_a_signature_and_its_words( CSignature * sig, QString stem, QTextStream& out_summaries){
QString word, sigstring;
sigstring = sig->get_key();
foreach (QString affix, sig->get_affix_string_list()){
QString word = build_a_word(m_suffix_flag, stem, affix);
CWord * pWord = m_Lexicon->get_words()->find_or_fail(word);
if (!pWord){
out_summaries << "process a stem given a signature:" << word << " doesn't exist!";
continue;
}
if (m_word_token_counts.contains(word)){
// this means we have already analyzed this word in a prior signature, and we ignore it now
// As a consequence a word is only analyzed once.
// This raises questions!! How can we ignore one word for a stem but not others for the same stem?
// This is how we guarantee a word has only one analysis in this current implementation.
// However I am not sure that this is not creating problems.
} else{
int this_word_token_count = pWord->count();
adjust_word_token_count(word, this_word_token_count); // stems is affected only here.
adjust_stem_token_count(stem, this_word_token_count);
adjust_affix_token_count_For_Choice(affix, word, this_word_token_count, sigstring);
if (! signature2stems[sigstring]->contains(stem)){
signature2stems[sigstring]->append(stem);
}
qDebug() << 157 << word << "analyzed";
word2sig[word] = sigstring;
word2stem[word] = stem;
word2affix[word] = affix;
signature2corpus_count[sigstring] += this_word_token_count; // this variable is changed only here.
add_letters_to_alphabet(word);
}
}
total_letter_count_in_stems += stem.length() + 1;
}
// #1c
void MDL::adjust_stem_token_count(QString stem, int word_count )
{ if (!stem2corpus_count.contains(stem)){
stem2corpus_count[stem] = 0;
}
stem2corpus_count[stem] += word_count;
stems[stem] += 1;
}
// #1d
void MDL::adjust_affix_token_count_For_Choice(QString affix, QString word, int this_word_token_count, QString sigstring){
if (!m_affix2corpus_count_given_signature.contains(sigstring)){
m_affix2corpus_count_given_signature[sigstring]= new QMap<QString, double>;
}
if (!m_affix2corpus_count_given_signature[sigstring]->contains(affix)){
m_affix2corpus_count_given_signature[sigstring]->insert(affix,0);
}
double old_value = m_affix2corpus_count_given_signature[sigstring]->value(affix);
m_affix2corpus_count_given_signature[sigstring]->insert(affix,old_value + m_word_token_counts[word]);
m_affixes[affix] += 1;
if (!m_affix2corpus_count.contains(affix)){ m_affix2corpus_count[affix] = 0.0;}
m_affix2corpus_count[affix] += this_word_token_count;
}
// #1e
void MDL::adjust_word_token_count(QString word, int word_token_count){
type_word_count_in_all_signatures += 1; // this variable is changed only here.
m_word_token_counts[word] = word_token_count; // analyzed words are affected here; unanalyzed words are affected elsewhere.
m_token_word_count_in_all_signatures += word_token_count; // this variable is changed only here
}
CSignatureCollection* MDL::get_signatures(){
if (m_suffix_flag){
return m_Lexicon->get_signatures();
}else {
return m_Lexicon->get_prefix_signatures();
}
}
// #2
void MDL::compute_stem_pointer_lengths_within_signature_Choice(QTextStream& out_distributions, QTextStream& out_summaries){
out_distributions << Qt::endl << "2. Compute stem pointer lengths (choice)." << Qt::endl;
//bool verbose_flag(true);
CSignatureCollection * sigs = get_signatures();
out_distributions <<
qSetFieldWidth(15) << "" <<
qSetFieldWidth(15) << "stem"<<
qSetFieldWidth(10 ) << "count"<<
qSetFieldWidth(12) << "ratio" <<
qSetFieldWidth(15) << "running total" <<
qSetFieldWidth(20) << "pointer length" <<
qSetFieldWidth(0 ) << Qt::endl;
for (int signo =0; signo< sigs->get_count(); signo++){
CSignature* sig = sigs->get_at_sorted(signo);
QString sigstring = sig->get_key();
QStringList stem_list;
double running_ratio = 0.0;
foreach(QString stem, sig->get_stem_strings(stem_list)){
if (signature2corpus_count[sigstring] == 0){continue;}
int count = stem2corpus_count[stem];
double ratio (count / signature2corpus_count[sigstring] );
if (ratio == 0){continue;}
running_ratio += ratio;
double pointer_length = -1.0 * base2log(ratio);
choice_of_stems_within_sigs_Choice_4 += pointer_length;
stem2pointer_length_given_signature[stem]= pointer_length;
//out.setFieldAlignment(QTextStream::AlignLeft);
out_distributions <<
qSetFieldWidth(15) << sigstring <<
qSetFieldWidth(15) << stem <<
qSetFieldWidth(10 ) << count <<
qSetFieldWidth(12) << ratio <<
qSetFieldWidth(15) << running_ratio <<
qSetFieldWidth(20) << pointer_length <<
qSetFieldWidth(0 ) << Qt::endl;
}
out_distributions << Qt::endl;
}
out_distributions << "----------------------------------------" << Qt::endl;
out_summaries << "2 compute stem pointers within signatures (choice)" << Qt::endl;
reports(out_summaries);
}
// #3
void MDL::compute_signature_pointer_lengths_Choice(QTextStream & out_distributions, QTextStream & out_summaries){
bits_per_letter = base2log(alphabet.size());
// 1b compute pointer length to each signature.
// Add to running the cost of each signature-pointer.
// This means that the major loop is over signatures.
// And that means only one occurrence of a pointer to each signature, rather than
// One pointer to a signature for each analyzed word in the word-list.
// Also, add an end mark for the list of signatures (sentry).
double running_total = 0.0;
out_distributions << "3. Compute signature pointer lengths (choice of signature)"<< Qt::endl<<Qt::endl;
pointers_to_signatures_Choice_3 = 0;
out_distributions <<
qSetFieldWidth(20) << "signature" <<
qSetFieldWidth(10) << "count" <<
qSetFieldWidth(12) << "ratio" <<
qSetFieldWidth(15) << "running total" <<
qSetFieldWidth(20) << "pointer length" <<
qSetFieldWidth(0) << Qt::endl;
foreach (QString sigstring, signature2corpus_count.keys()){
if (signature2corpus_count[sigstring] == 0){
out_distributions << ">> No members! " << sigstring << Qt::endl<<Qt::endl; // TODO
continue;
}
int count = signature2corpus_count[sigstring];
double fraction = count / m_token_word_count_in_all_signatures;
double this_sig_pointer_length = -1* base2log(fraction);
signature_plogs[sigstring] = this_sig_pointer_length; // information content on signature pointer is based on corpus-counts of signatures
running_total += fraction;
pointers_to_signatures_Choice_3 += this_sig_pointer_length;
out_distributions <<
qSetFieldWidth(20) << sigstring <<
qSetFieldWidth(10) << count <<
qSetFieldWidth(12) << fraction <<
qSetFieldWidth(15) << running_total <<
qSetFieldWidth(20) << this_sig_pointer_length <<
//qSetFieldWidth(10) << pointers_to_signatures_Choice_3 <<
qSetFieldWidth(0) << Qt::endl;
}
out_distributions << Qt::endl << "-------------------------------------"<< Qt::endl;
out_summaries << "3. compute signature pointer length (choice)" << Qt::endl;
reports(out_summaries);
}
// #4
void MDL::process_stems_Content(QTextStream & out, QTextStream& out_summaries){
Q_UNUSED(out);
foreach (QString sigstring, signature2corpus_count.keys()) {
//out << "Process stems " << sigstring << Qt::endl;
foreach (QString stem, *signature2stems[sigstring]){
double bits = (stem.length() + 1) * bits_per_letter;
stem_content_in_bits += bits;
}
}
stem_plus_affix_content_in_bits_Architecture_1 += stem_content_in_bits;
out_summaries << "4. Process stems (content)" << Qt::endl;
reports(out_summaries);
}
// # 5
void MDL::process_unanalyzed_words(QTextStream & out_distributions, QTextStream & out_summaries){
bool verbose_flag = true;
// Unanalyzed words
//double unanalyzed_words_bits = 0.0;
double running_word_bits = 0.0;
out_distributions << Qt::endl;
out_distributions << Qt::endl<< "5. Process unanalyzed words." << Qt::endl;
m_token_count_of_unanalyzed_words = 0;
m_type_count_of_unanalyzed_words =0;
for (int i = 0; i < m_Lexicon->get_words()->get_count(); i++){
CWord* pWord = m_Lexicon->get_words()->get_word(i);
if (! word2sig.contains(pWord->get_key())){
qDebug() << 321 << "unanalyzed";
m_token_count_of_unanalyzed_words += pWord->count();
m_type_count_of_unanalyzed_words++;
}
}
double running_total = 0.0;
out_distributions <<
qSetFieldWidth(20) << "word" <<
qSetFieldWidth(10) << "count" <<
qSetFieldWidth(12) << "ratio" <<
qSetFieldWidth(15) << "running total" <<
qSetFieldWidth(20) << "word pointer length" <<
qSetFieldWidth(0) << Qt::endl;
for (int i = 0; i < m_Lexicon->get_words()->get_word_count(); i++){
CWord* pWord = m_Lexicon->get_words()->get_word(i);
//bool analyzed_flag = false;
QString word = pWord->get_key();
if (! word2sig.contains(word) ){
int count = pWord->count();
m_word_token_counts[word] = count; // unanalyzed words are affected here; analyzed words are affected elsewhere
unanalyzed_words.insert(word, unanalyzed_words.count());
double ratio = count / m_token_count_of_unanalyzed_words;
double this_word_pointer_length = -1.0 * base2log(ratio);
running_total += ratio;
double word_bits = (word.length()+1) * bits_per_letter;
running_word_bits += word_bits;
if (verbose_flag){
out_distributions <<
qSetFieldWidth(20) << word <<
qSetFieldWidth(10) << count <<
qSetFieldWidth(12) << ratio <<
qSetFieldWidth(15) << running_total <<
qSetFieldWidth(20) << this_word_pointer_length <<
qSetFieldWidth(0) << Qt::endl;
}
}
}
this_MDL += running_word_bits;
out_summaries << "5. Process unanalyzed words"<<Qt::endl;
reports(out_summaries);
}
// #6
void MDL::weigh_analyzed_and_unanalyzed_words(QTextStream& out_distributions, QTextStream& out_summaries){
out_distributions << Qt::endl << "---------------------------------------"<< Qt::endl <<
"6. Weigh analyzed and unanalyzed words"<< Qt::endl;
double ratio1, ratio2;
ratio1= 0;
ratio2= 0;
m_total_flagging_of_analyzed_words=0.0;
m_total_flagging_of_unanalyzed_words=0.0;
if (m_total_token_count_of_words){
ratio1 = m_token_word_count_in_all_signatures / m_total_token_count_of_words;
ratio2 = m_token_count_of_unanalyzed_words / m_total_token_count_of_words;
}
if (ratio1 > 0.0 && ratio2 > 0.0){
m_flag_to_mark_an_analyzed_word = -1.0 * base2log(ratio1);
m_flag_to_mark_an_unanalyzed_word = -1.0 * base2log(ratio2);
m_total_flagging_of_analyzed_words = m_token_word_count_in_all_signatures * m_flag_to_mark_an_analyzed_word ;
m_total_flagging_of_unanalyzed_words = m_token_count_of_unanalyzed_words * m_flag_to_mark_an_unanalyzed_word;
} else{
m_flag_to_mark_an_analyzed_word = 0.0;
m_flag_to_mark_an_analyzed_word = 0.0;
m_total_flagging_of_analyzed_words = 0.0;
m_total_flagging_of_unanalyzed_words = 0.0;
}
if (true){
out_distributions << Qt::left << Qt::endl << Qt::endl <<
qSetFieldWidth(30) << "Analyzed words " <<
qSetFieldWidth(15) << m_token_word_count_in_all_signatures <<
qSetFieldWidth(15) << ratio1 <<
qSetFieldWidth(15) << ratio1 <<
qSetFieldWidth(15) << m_flag_to_mark_an_analyzed_word <<
qSetFieldWidth(15) << m_total_flagging_of_analyzed_words <<
qSetFieldWidth (0) << Qt::endl;
out_distributions << Qt::left <<
qSetFieldWidth(30) << "Unanalyzed words" <<
qSetFieldWidth(15) << m_token_count_of_unanalyzed_words <<
qSetFieldWidth(15) << ratio2 <<
qSetFieldWidth(15) << ratio1 + ratio2<<
qSetFieldWidth(15) << m_flag_to_mark_an_unanalyzed_word <<
qSetFieldWidth(15) << m_total_flagging_of_unanalyzed_words <<
qSetFieldWidth (0) << Qt::endl;
}
out_summaries << "6. weigh analyzed and unanalyzed words"<<Qt::endl;
reports(out_summaries);
}
// #7
// todo : worry about whether type or token counts to be used here for pointer length --
void MDL::process_affixes(QTextStream & out_distributions, QTextStream& out_summaries){
bool verbose_flag = true;
if (verbose_flag){
out_distributions << Qt::endl << "-------------------------------------" << Qt::endl
<<"7. Process affixes (architectural) " << Qt::endl << Qt::endl;
}
affix_content_in_bits = 0.0;
int index = 0;
double running_total = 0.0;
out_distributions <<
qSetFieldWidth(15) << "affix" <<
qSetFieldWidth(10) << "count" <<
qSetFieldWidth(15) << "ratio" <<
qSetFieldWidth(15) << "running total" <<
qSetFieldWidth(20) << "affix pointer length" <<
qSetFieldWidth(0) << Qt::endl;
for (auto iter = m_affixes.constBegin(); iter != m_affixes.constEnd(); ++iter){
QString this_affix = iter.key();
m_affix2index[this_affix] = index++;
double this_affix_content_in_bits;
double this_affix_use_count = iter.value(); // to do -- check this is the choice for variable
//qDebug() << 438 << this_affix << this_affix_use_count;
int this_letter_count;
if (this_affix == "NULL") {
this_letter_count = 1;
} else{
this_letter_count = this_affix.length() + 1;
}
total_letter_count_in_affixes += this_letter_count;
double ratio =this_affix_use_count / type_word_count_in_all_signatures;
running_total += ratio;
m_affix_pointer_length[this_affix] = -1.0 * base2log( ratio );
//qDebug() << 449 << this_affix << m_affix_pointer_length[this_affix];
this_affix_content_in_bits = this_letter_count * bits_per_letter;
affix_content_in_bits += this_affix_content_in_bits ;
if (this_affix == "") {this_affix = "NULL";}
if (verbose_flag){
out_distributions <<
qSetFieldWidth(15) << this_affix <<
qSetFieldWidth(10) << this_affix_use_count <<
qSetFieldWidth(15) << ratio <<
qSetFieldWidth(15) << running_total <<
qSetFieldWidth(20) << m_affix_pointer_length[this_affix] <<
qSetFieldWidth(0) << Qt::endl;
}
}
stem_plus_affix_content_in_bits_Architecture_1 += affix_content_in_bits;
out_distributions<< Qt::endl << "-----------------------------------------------------------";
out_summaries << "7. Process affixes"<<Qt::endl;
reports(out_summaries);
}
// #8
void MDL::process_signatures_bis_Architecture(QTextStream & out_distributions, QTextStream& out_summaries){
out_distributions << Qt::endl << "8. Process signatures part 2 (architectural). No new distributions." << Qt::endl <<Qt::endl;
signature_to_affix_pointers_Architexture_8 = 0.0;
foreach (QString sigstring, signature2corpus_count.keys()){
out_distributions << "# "<< sigstring << Qt::endl;
double this_signature_pointer_use_in_bits(0);
QStringList affixes = sigstring.split("=");
foreach (QString affix, affixes){
this_signature_pointer_use_in_bits += m_affix_pointer_length[affix];
out_distributions <<
qSetFieldWidth(15) << affix <<
qSetFieldWidth(20) << m_affix_pointer_length[affix] <<
qSetFieldWidth(15) << ". Running total of affix pointer use: " <<
qSetFieldWidth(15) << this_signature_pointer_use_in_bits <<
qSetFieldWidth(0) << Qt::endl;
}
signature_to_affix_pointers_Architexture_8 += this_signature_pointer_use_in_bits;
out_distributions << qSetFieldWidth(15) << "running total" <<
qSetFieldWidth(20) << signature_to_affix_pointers_Architexture_8<<
qSetFieldWidth(0) <<Qt::endl << Qt::endl;
}
out_distributions << Qt::endl << " Running total of signature pointer use in bits " <<
signature_to_affix_pointers_Architexture_8 <<Qt::endl << Qt::endl;
out_summaries << "8. Process signatures, part 2"<<Qt::endl;
reports(out_summaries);
}
bool MDL::word_is_analyzed(QString word){
if (word2sig.contains(word)){return true;}
return (false);
}
void MDL::corpus_description(QTextStream& out_corpus, QTextStream& out_summaries ){
out_corpus.setFieldAlignment(QTextStream::AlignLeft);
out_corpus << Qt::endl<< "Analyzed words"<< Qt::endl;
out_corpus <<
qSetFieldWidth(25) << "word" <<
qSetFieldWidth(12) << " sig: "<<
qSetFieldWidth(10) << " stem: "<<
qSetFieldWidth(10) <<" affix: " <<
qSetFieldWidth(20) << " flag as analyzed: " <<
qSetFieldWidth(10) <<" count: " <<
qSetFieldWidth(10) <<" sum" <<
qSetFieldWidth(0) << Qt::endl;
foreach( QString word, word2sig.keys() ) {
compute_corpus_length(word, out_corpus);
}
out_corpus << "Total for analyzed words " << m_corpus_description_from_analyzed_words<< Qt::endl;
out_corpus << Qt::endl<< "Unanalyzed words"<< Qt::endl<< Qt::endl;
out_corpus <<
qSetFieldWidth(25) << "word" <<
qSetFieldWidth(12) << " pointer" <<
qSetFieldWidth(20) << " flag as unanalyzed" <<
qSetFieldWidth(10) << " count" <<
qSetFieldWidth(10) << " sum" <<
qSetFieldWidth(0) << Qt::endl;
foreach( QString word, unanalyzed_words.keys() ) {
compute_corpus_length(word, out_corpus);
}
out_summaries<<"9. Corpus description"<< Qt::endl;
out_corpus << "Total for unanalyzed words " << m_corpus_description_from_unanalyzed_words;
reports(out_summaries);
}
void MDL::compute_corpus_length(QString word, QTextStream & out_corpus){
bool verbose_flag (true);
double bits = 0.0;
QString sigstring;
double count = 1.0;
double signature_pointer;
double per_word_cost;
out_corpus.setFieldAlignment(QTextStream::AlignLeft);
out_corpus.setRealNumberPrecision(5);
if (word2sig.contains(word)){
count = m_word_token_counts[word];
sigstring = word2sig[word];
QString affix = word2affix[word];
signature_pointer = signature_plogs[sigstring];
double stem_pointer = stem2pointer_length_given_signature.value(word2stem[word]);
double affix_pointer = m_affix2pointer_length_affix_choice_within_signature[sigstring]->value( word2affix[word]);
per_word_cost = signature_pointer + stem_pointer + affix_pointer + m_flag_to_mark_an_analyzed_word;
bits = count * per_word_cost ;
m_corpus_description_from_analyzed_words += bits;
if (verbose_flag){
out_corpus <<
qSetFieldWidth(25) << word <<
qSetFieldWidth(12) << QString::number(signature_pointer) <<
qSetFieldWidth(10) << QString::number(stem_pointer) <<
qSetFieldWidth(10) << QString::number (affix_pointer) <<
qSetFieldWidth(20) <<m_flag_to_mark_an_analyzed_word <<
qSetFieldWidth(10) << count <<
qSetFieldWidth(10) <<per_word_cost <<
qSetFieldWidth(0) << Qt::endl;
}
}else{
double this_pointer;
double count = m_word_token_counts[word];
double ratio = count/m_token_count_of_unanalyzed_words;
this_pointer = m_unanalyzed_word2pointer_length[word] = -1.0 * base2log(ratio);
per_word_cost = this_pointer + m_flag_to_mark_an_unanalyzed_word;
bits = count * per_word_cost;
if (verbose_flag){
out_corpus <<
qSetFieldWidth(25) << word <<
qSetFieldWidth(12) << QString::number(this_pointer) <<
qSetFieldWidth(20) << m_flag_to_mark_an_unanalyzed_word <<
qSetFieldWidth(10) << count <<
qSetFieldWidth(10) << per_word_cost <<
qSetFieldWidth(0) << Qt::endl;
}
m_corpus_description_from_unanalyzed_words += bits;
}
}
void MDL::print_grammar(QTextStream& out){
QMap<QString, int> sigstring_to_row_number;
QMap<QString, int> stem_to_row_number;
int row = 0;
for(auto sigstring : signatures.keys())
{
sigstring_to_row_number[sigstring] = row++;
}
//<======= Start printing here: ==============================>
for(auto affix : m_affixes.keys())
{
out << QString::number(m_affix2index[affix])<< " " <<
affix<< " " <<
QString::number(m_affix_pointer_length.value(affix)) <<
Qt::endl;
}
out << "---"<< Qt::endl;
for(auto sigstring : signature2stems.keys())
{ out << QString::number(sigstring_to_row_number[sigstring])<< " ";
QStringList affixes = sigstring.split('=');
out << "[ ";
foreach (QString affix, affixes){
out << QString::number(m_affix2index[affix]) << " ";
}
out << "[ ";
bool first = true;
foreach (QString stem, *signature2stems[sigstring]){
if (!first){
out << ']';
} else{first = false;}
out << stem << " " <<
QString::number(stem2pointer_length_given_signature[stem]);
}
out << "[ ";
foreach (QString affix, affixes){
//out << affix << " ";
out << QString::number(m_affix2pointer_length_affix_choice_within_signature[sigstring]->value(affix)) << " ";
}
out << " [ " << signature_plogs[sigstring];
out << Qt::endl;
}
out << "--"<< Qt::endl;
for (auto word: unanalyzed_words.keys()){
if (word.length() == 0) continue;
out <<
word << " " <<
m_unanalyzed_word2pointer_length[word] + m_flag_to_mark_an_unanalyzed_word <<
Qt::endl;
}
out << "Plog analyzed word " << m_flag_to_mark_an_analyzed_word << Qt::endl;
if (m_suffix_flag){
out << "suffixes";
} else{
out << "prefixes";
}
}
QString Format(double n){
return QLocale(QLocale::English).toString(n, 'f', 2);
}
void MDL::reports(QTextStream & out){
//QLocale::setDefault(QLocale(QLocale::UnitedStates));
m_total_token_count_of_words = m_token_word_count_in_all_signatures + m_token_count_of_unanalyzed_words;
total_type_count_of_words = type_word_count_in_all_signatures + m_type_count_of_unanalyzed_words;
int field = 50;
int field2 = 15;
//bool verbose_flag(true);
out << "===========================================================" << Qt::endl;
if(m_suffix_flag){
out << "Suffixal analysis." <<qSetFieldWidth(0)<< Qt::endl;
}else{
out << "Prefixal analysis." << qSetFieldWidth(0)<< Qt::endl;
}
out << qSetFieldWidth(field) << Qt::left
<< "Type count of words: " << Qt::right << qSetFieldWidth(field2)
<< Format(total_type_count_of_words)
<< qSetFieldWidth(0)<< Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "Type count of unanalyzed words: " << Qt::right << qSetFieldWidth(field2)
<< Format(m_type_count_of_unanalyzed_words) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "Type count of analyzed words: " << Qt::right<< qSetFieldWidth(field2)
<< Format(type_word_count_in_all_signatures) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "Token count of all words:"<< Qt::right << qSetFieldWidth(field2)
<< Format(m_total_token_count_of_words) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "Token count of unanalyzed words:"<< Qt::right << qSetFieldWidth(field2)
<< Format(m_token_count_of_unanalyzed_words) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "Token count of analyzed words: "<< Qt::right << qSetFieldWidth(field2)
<< Format(m_token_word_count_in_all_signatures ) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "Total token count of words: "<< Qt::right << qSetFieldWidth(field2)
<< Format(m_token_word_count_in_all_signatures + m_token_count_of_unanalyzed_words ) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "Type count of words from Lxa: "<< Qt::right << qSetFieldWidth(field2)
<< Format(m_Lexicon->get_words()->get_count()) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "Token count of words from Lxa: "<< Qt::right << qSetFieldWidth(field2)
<< Format(m_Lexicon->get_words()->get_token_count()) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "Number of signatures: "<< Qt::right << qSetFieldWidth(field2)
<< Format(signatures.count() ) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "Number of stems: "<< Qt::right << qSetFieldWidth(field2)
<< Format(stems.count() ) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "Number of affixes: "<< Qt::right << qSetFieldWidth(field2)
<< Format(m_affixes.count() ) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "Total letter count in stems: " << Qt::right << qSetFieldWidth(field2)
<< Format(total_letter_count_in_stems) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "total letter count in affixes: " << Qt::right << qSetFieldWidth(field2)
<< Format(total_letter_count_in_affixes) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "total letter count: " << Qt::right << qSetFieldWidth(field2)
<< Format(get_total_letter_count()) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "Size of alphabet: " << Qt::right << qSetFieldWidth(field2)
<< alphabet.size() << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "Bits per letter: " << Qt::right << qSetFieldWidth(field2)
<< QLocale(QLocale::English).toString(bits_per_letter, 'f', 3) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "Running total of stem content (bits) : " << Qt::right << qSetFieldWidth(field2)
<< Format(stem_content_in_bits) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "Running total of affix content (bits): " << Qt::right << qSetFieldWidth(field2)
<< Format(affix_content_in_bits) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "Stem plus affix content: ** " << Qt::right << qSetFieldWidth(field2)
<< Format(stem_plus_affix_content_in_bits_Architecture_1) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "Cost of affix choices within signatures: **"<< Qt::right << qSetFieldWidth(field2)
<< Format(total_cost_of_affix_choice_within_all_signatures_Choice_2) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "Cost of a set of pointers to each signature: **"<< Qt::right << qSetFieldWidth(field2)
<< Format( pointers_to_signatures_Choice_3) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "Cost of stem-choice within signatures **"
<< Qt::right << qSetFieldWidth(field2)
<< Format(choice_of_stems_within_sigs_Choice_4) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "bit cost of flagging analyzed words: ** " << Qt::right << qSetFieldWidth(field2)
<< Format(m_total_flagging_of_analyzed_words) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "bit cost of flagging unanalyzed words: ** " << Qt::right << qSetFieldWidth(field2)
<< Format(m_total_flagging_of_unanalyzed_words) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left << "Grammar length in bits: "<< Qt::right << qSetFieldWidth(field2)
<< Format(get_grammar_length_in_bits()) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "corpus description from analyzed words: "<< Qt::right << qSetFieldWidth(field2)
<< Format(m_corpus_description_from_analyzed_words) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "corpus description from unanalyzed words: " << Qt::right << qSetFieldWidth(field2)
<< Format(m_corpus_description_from_unanalyzed_words) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "corpus description: " << Qt::right << qSetFieldWidth(field2)
<< Format(get_corpus_description()) << Qt::endl;
out << qSetFieldWidth(field) << Qt::left
<< "Total description length: " << Qt::right << qSetFieldWidth(field2)
<< Format(m_corpus_description_from_unanalyzed_words + m_corpus_description_from_analyzed_words + get_grammar_length_in_bits() ) << Qt::endl;
out << "===========================================================" << Qt::endl;
}