// File: szmer[P]brill/./src/corpus.cpp
// Function: Corpus::tag_all_unigram(bool)
Corpus Corpus::tag_all_unigram(bool leave_table) {

	Corpus child;
	child.sentences.reserve(sentences.size());

	// Compute frequency distribution for each tag for each word token (in this Corpus instance).
	map< string, map<string,int> > tag_freqs; // word => <tag => frequency>

	for(vector< vector<TaggedWord> >::iterator stc = sentences.begin(); stc != sentences.end(); stc++) { 

		child.sentences.push_back( vector<TaggedWord>() );
		child.sentences.rbegin()->reserve( stc->size() );	// reserve space in the last sentence
									// vector of the child

		for(vector<TaggedWord>::iterator tword = stc->begin(); tword != stc->end(); tword ++) {

			if(tag_freqs[tword->word].count(tword->tag) == 0) // (entry for this tag doesn't exist yet)
				tag_freqs[tword->word][tword->tag] = 1;
			else
				tag_freqs[tword->word][tword->tag] ++;
	
			// Copy the word to the last sentence of the child Corpus (with an empty tag).
			child.sentences.rbegin()->push_back( TaggedWord(tword->word, string("")) );

		}
	}

	// Go through the words in the child Corpus and tag each with the most frequent tag.
	for(vector< vector<TaggedWord> >::iterator stc = child.sentences.begin(); stc != child.sentences.end()
						; stc++) 
		for(unsigned int wi = 0; wi < stc->size(); wi ++) {

			TaggedWord * tword = & stc->at(wi);
	
			// Now, each word will receive its only known interpretation. IF there is more than one 
			// possible tag for given word, wipe all entries in tag_freqs except for the tag with
			// the highest frequency.
			if(tag_freqs[tword->word].size() > 1) {

				string max_label = "";
				int max_val = 0;

				for(map<string,int>::iterator tag_entry = tag_freqs[tword->word].begin()
						; tag_entry != tag_freqs[tword->word].end(); tag_entry ++)
					if(tag_entry->second > max_val) {
						max_label = tag_entry->first;
						max_val = tag_entry->second;
					}

				tag_freqs[tword->word].clear();
				tag_freqs[tword->word][max_label]; // create entry, don't care about value
			}
	
			// (there is only one intepretation)
			// Extract tag label of this lone entry.
			tword->tag = tag_freqs[tword->word].begin()->first;
	
		}

	// Fill unigram_table if it is requested.
	if(leave_table)
		for(map<string, map<string,int> >::iterator i = tag_freqs.begin(); i != tag_freqs.end(); i ++)
			unigram_table[ i->first ] = i->second.begin()->first;

	return child;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2b0, %rsp            # imm = 0x2B0
movq %rdi, -0x1d0(%rbp)
movb %dl, %al
movq %rdi, %rcx
movq %rcx, -0x1e0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x1d8(%rbp)
movb $0x0, -0x12(%rbp)
callq 0x7e90
movq -0x1d8(%rbp), %rdi
callq 0x7dd0
movq -0x1d0(%rbp), %rdi
movq %rax, %rsi
callq 0x83e0
jmp 0x40ac
leaq -0x58(%rbp), %rdi
callq 0x84e0
movq -0x1d8(%rbp), %rdi
callq 0x8500
movq %rax, -0x60(%rbp)
movq -0x1d8(%rbp), %rdi
callq 0x8570
movq %rax, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x8530
testb $0x1, %al
jne 0x40eb
jmp 0x4479
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
movq $0x0, -0x70(%rbp)
leaq -0x80(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
callq 0x7fa0
movq -0x1d0(%rbp), %rdi
movq -0x1e8(%rbp), %rsi
callq 0x7f70
jmp 0x411f
leaq -0x80(%rbp), %rdi
callq 0x7fc0
movq -0x1d0(%rbp), %rsi
leaq -0x88(%rbp), %rdi
movq %rdi, -0x1f8(%rbp)
callq 0x8180
movq -0x1f8(%rbp), %rdi
callq 0x81d0
movq %rax, -0x1f0(%rbp)
jmp 0x4157
leaq -0x60(%rbp), %rdi
callq 0x86a0
movq %rax, %rdi
callq 0x7e20
movq -0x1f0(%rbp), %rdi
movq %rax, %rsi
callq 0x85a0
jmp 0x4179
leaq -0x60(%rbp), %rdi
callq 0x86a0
movq %rax, %rdi
callq 0x86c0
movq %rax, -0x90(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x86a0
movq %rax, %rdi
callq 0x8730
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x86f0
testb $0x1, %al
jne 0x41c5
jmp 0x4460
leaq -0x90(%rbp), %rdi
callq 0x8840
movq %rax, %rsi
leaq -0x58(%rbp), %rdi
callq 0x8760
movq %rax, -0x200(%rbp)
jmp 0x41e6
leaq -0x90(%rbp), %rdi
callq 0x8840
movq -0x200(%rbp), %rdi
movq %rax, %rsi
addq $0x20, %rsi
callq 0x8860
movq %rax, -0x208(%rbp)
jmp 0x420e
movq -0x208(%rbp), %rax
cmpq $0x0, %rax
jne 0x42b6
leaq -0x90(%rbp), %rdi
callq 0x8840
movq %rax, %rsi
leaq -0x58(%rbp), %rdi
callq 0x8760
movq %rax, -0x210(%rbp)
jmp 0x4240
leaq -0x90(%rbp), %rdi
callq 0x8840
movq -0x210(%rbp), %rdi
movq %rax, %rsi
addq $0x20, %rsi
callq 0x88c0
movq %rax, -0x218(%rbp)
jmp 0x4268
movq -0x218(%rbp), %rax
movl $0x1, (%rax)
jmp 0x430d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x48ba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x7fc0
jmp 0x4893
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x4893
leaq -0x90(%rbp), %rdi
callq 0x8840
movq %rax, %rsi
leaq -0x58(%rbp), %rdi
callq 0x8760
movq %rax, -0x220(%rbp)
jmp 0x42d7
leaq -0x90(%rbp), %rdi
callq 0x8840
movq -0x220(%rbp), %rdi
movq %rax, %rsi
addq $0x20, %rsi
callq 0x88c0
movq %rax, -0x228(%rbp)
jmp 0x42ff
movq -0x228(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq -0x1d0(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
movq %rdi, -0x238(%rbp)
callq 0x8180
movq -0x238(%rbp), %rdi
callq 0x81d0
movq %rax, -0x230(%rbp)
jmp 0x433c
leaq -0x90(%rbp), %rdi
callq 0x8840
movq %rax, %rsi
leaq -0x100(%rbp), %rdi
callq 0x30f0
jmp 0x4359
leaq -0x121(%rbp), %rdi
movq %rdi, -0x240(%rbp)
callq 0x3410
movq -0x240(%rbp), %rdx
leaq 0xedd4(%rip), %rsi      # 0x1314e
leaq -0x120(%rbp), %rdi
callq 0x7ec0
jmp 0x4388
leaq -0xe0(%rbp), %rdi
leaq -0x100(%rbp), %rsi
leaq -0x120(%rbp), %rdx
callq 0x8240
jmp 0x43a4
movq -0x230(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x8210
jmp 0x43b9
leaq -0xe0(%rbp), %rdi
callq 0x82d0
leaq -0x120(%rbp), %rdi
callq 0x34b8
leaq -0x121(%rbp), %rdi
callq 0x3280
leaq -0x100(%rbp), %rdi
callq 0x34b8
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
callq 0x89a0
movq %rax, -0x130(%rbp)
jmp 0x4191
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x4443
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x4437
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x82d0
leaq -0x120(%rbp), %rdi
callq 0x34b8
leaq -0x121(%rbp), %rdi
callq 0x3280
leaq -0x100(%rbp), %rdi
callq 0x34b8
jmp 0x4893
jmp 0x4462
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
callq 0x89e0
movq %rax, -0x138(%rbp)
jmp 0x40c5
movq -0x1d0(%rbp), %rdi
callq 0x8500
movq %rax, -0x140(%rbp)
movq -0x1d0(%rbp), %rdi
callq 0x8570
movq %rax, -0x148(%rbp)
leaq -0x140(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0x8530
testb $0x1, %al
jne 0x44bb
jmp 0x4793
movl $0x0, -0x14c(%rbp)
movl -0x14c(%rbp), %eax
movq %rax, -0x248(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x86a0
movq %rax, %rdi
callq 0x7e20
movq %rax, %rcx
movq -0x248(%rbp), %rax
cmpq %rcx, %rax
jae 0x4777
leaq -0x140(%rbp), %rdi
callq 0x86a0
movq %rax, %rdi
movl -0x14c(%rbp), %eax
movl %eax, %esi
callq 0x8a20
movq %rax, -0x250(%rbp)
jmp 0x451e
movq -0x250(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x158(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x8760
movq %rax, -0x258(%rbp)
jmp 0x4545
movq -0x258(%rbp), %rdi
callq 0x8a60
cmpq $0x1, %rax
jbe 0x4714
leaq -0x179(%rbp), %rdi
movq %rdi, -0x260(%rbp)
callq 0x3410
movq -0x260(%rbp), %rdx
leaq 0xebd2(%rip), %rsi      # 0x1314e
leaq -0x178(%rbp), %rdi
callq 0x7ec0
jmp 0x458a
leaq -0x179(%rbp), %rdi
callq 0x3280
movl $0x0, -0x180(%rbp)
movq -0x158(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x8760
movq %rax, -0x268(%rbp)
jmp 0x45b9
movq -0x268(%rbp), %rdi
callq 0x8a80
movq %rax, -0x188(%rbp)
movq -0x158(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x8760
movq %rax, -0x270(%rbp)
jmp 0x45e5
movq -0x270(%rbp), %rdi
callq 0x8ae0
movq %rax, -0x190(%rbp)
leaq -0x188(%rbp), %rdi
leaq -0x190(%rbp), %rsi
callq 0x8ab0
testb $0x1, %al
jne 0x4614
jmp 0x46b5
leaq -0x188(%rbp), %rdi
callq 0x8b10
movl 0x20(%rax), %eax
cmpl -0x180(%rbp), %eax
jle 0x4699
leaq -0x188(%rbp), %rdi
callq 0x8b10
movq %rax, %rsi
leaq -0x178(%rbp), %rdi
callq 0x3040
jmp 0x4648
leaq -0x188(%rbp), %rdi
callq 0x8b10
movl 0x20(%rax), %eax
movl %eax, -0x180(%rbp)
jmp 0x4699
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x179(%rbp), %rdi
callq 0x3280
jmp 0x4893
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x178(%rbp), %rdi
callq 0x34b8
jmp 0x4893
jmp 0x469b
leaq -0x188(%rbp), %rdi
xorl %esi, %esi
callq 0x8b30
movq %rax, -0x198(%rbp)
jmp 0x45cc
movq -0x158(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x8760
movq %rax, -0x278(%rbp)
jmp 0x46ce
movq -0x278(%rbp), %rdi
callq 0x8b70
movq -0x158(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x8760
movq %rax, -0x280(%rbp)
jmp 0x46f3
movq -0x280(%rbp), %rdi
leaq -0x178(%rbp), %rsi
callq 0x88c0
jmp 0x4708
leaq -0x178(%rbp), %rdi
callq 0x34b8
movq -0x158(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x8760
movq %rax, -0x288(%rbp)
jmp 0x472d
movq -0x288(%rbp), %rdi
callq 0x8a80
movq %rax, -0x1a0(%rbp)
leaq -0x1a0(%rbp), %rdi
callq 0x8b10
movq %rax, %rsi
movq -0x158(%rbp), %rdi
addq $0x20, %rdi
callq 0x3040
jmp 0x4761
jmp 0x4763
movl -0x14c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14c(%rbp)
jmp 0x44c5
jmp 0x4779
leaq -0x140(%rbp), %rdi
xorl %esi, %esi
callq 0x89e0
movq %rax, -0x1a8(%rbp)
jmp 0x448c
testb $0x1, -0x11(%rbp)
je 0x487e
leaq -0x58(%rbp), %rdi
callq 0x8b90
movq %rax, -0x1b0(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x8bf0
movq %rax, -0x1b8(%rbp)
leaq -0x1b0(%rbp), %rdi
leaq -0x1b8(%rbp), %rsi
callq 0x8bc0
testb $0x1, %al
jne 0x47d9
jmp 0x487c
leaq -0x1b0(%rbp), %rdi
movq %rdi, -0x2a8(%rbp)
callq 0x8c20
movq %rax, %rdi
addq $0x20, %rdi
callq 0x8a80
movq %rax, -0x1c0(%rbp)
leaq -0x1c0(%rbp), %rdi
callq 0x8b10
movq -0x2a8(%rbp), %rdi
movq %rax, %rcx
movq -0x1d8(%rbp), %rax
movq %rcx, -0x2a0(%rbp)
addq $0x18, %rax
movq %rax, -0x298(%rbp)
callq 0x8c20
movq -0x298(%rbp), %rdi
movq %rax, %rsi
callq 0x8c40
movq %rax, -0x290(%rbp)
jmp 0x484b
movq -0x2a0(%rbp), %rsi
movq -0x290(%rbp), %rdi
callq 0x3040
jmp 0x4860
jmp 0x4862
leaq -0x1b0(%rbp), %rdi
xorl %esi, %esi
callq 0x8d20
movq %rax, -0x1c8(%rbp)
jmp 0x47ad
jmp 0x487e
movb $0x1, -0x12(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x8d60
testb $0x1, -0x12(%rbp)
jne 0x48aa
jmp 0x489e
leaq -0x58(%rbp), %rdi
callq 0x8d60
jmp 0x48ba
movq -0x1d0(%rbp), %rdi
callq 0x8300
movq -0x1e0(%rbp), %rax
addq $0x2b0, %rsp            # imm = 0x2B0
popq %rbp
retq
movq -0x1d0(%rbp), %rdi
callq 0x8300
movq -0x20(%rbp), %rdi
callq 0x3400
nop