// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %edx, -0x4c(%rbp)
movq %rsi, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
leaq 0x20(%rdi), %rax
andl $0x0, 0x20(%rdi)
andq $0x0, 0x28(%rdi)
movq %rax, 0x30(%rdi)
movq %rax, 0x38(%rdi)
andq $0x0, 0x40(%rdi)
movq 0x8(%rsi), %rax
subq (%rsi), %rax
pushq $0x18
popq %rcx
cqto
idivq %rcx
movq %rdi, -0x38(%rbp)
movq %rax, %rsi
callq 0x4f6a
leaq -0x78(%rbp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq %rbx, -0x48(%rbp)
movq (%rbx), %r13
leaq -0xc0(%rbp), %r14
leaq -0x100(%rbp), %r12
leaq -0xe0(%rbp), %rbx
movq -0x48(%rbp), %rax
cmpq 0x8(%rax), %r13
je 0x2c6a
xorps %xmm0, %xmm0
movaps %xmm0, -0xc0(%rbp)
andq $0x0, -0xb0(%rbp)
movq -0x38(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x5d3c
movq %r14, %rdi
callq 0x4dc8
movq 0x8(%r15), %rdi
addq $-0x18, %rdi
movq 0x8(%r13), %rsi
subq (%r13), %rsi
sarq $0x6, %rsi
callq 0x501a
movq %r13, -0x40(%rbp)
movq (%r13), %r13
movq -0x40(%rbp), %rax
cmpq 0x8(%rax), %r13
je 0x2c5d
leaq -0x80(%rbp), %rdi
movq %r13, %rsi
callq 0x50ae
leaq 0x20(%r13), %r15
movq %rax, %rdi
movq %r15, %rsi
callq 0x5118
testq %rax, %rax
je 0x2bdc
leaq -0x80(%rbp), %rdi
movq %r13, %rsi
callq 0x50ae
movq %rax, %rdi
movq %r15, %rsi
callq 0x513c
incl (%rax)
jmp 0x2bf9
leaq -0x80(%rbp), %rdi
movq %r13, %rsi
callq 0x50ae
movq %rax, %rdi
movq %r15, %rsi
callq 0x513c
movl $0x1, (%rax)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %r15
movq %r12, %rdi
movq %r13, %rsi
callq 0x2210
movq %rbx, %rdi
leaq 0x5558(%rip), %rsi      # 0x816e
leaq -0x29(%rbp), %rdx
callq 0x4d8c
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x4e92
addq $-0x18, %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x5f32
movq %r14, %rdi
callq 0x4efe
movq %rbx, %rdi
callq 0x2368
movq %r12, %rdi
callq 0x2368
addq $0x40, %r13
jmp 0x2b93
movq -0x40(%rbp), %r13
addq $0x18, %r13
jmp 0x2b3b
movq -0x38(%rbp), %rax
movq (%rax), %r14
leaq -0x80(%rbp), %r13
movq -0x38(%rbp), %rax
cmpq 0x8(%rax), %r14
je 0x2d99
xorl %r15d, %r15d
movq %r14, -0x40(%rbp)
movl %r15d, %r15d
movq 0x8(%r14), %rax
subq (%r14), %rax
sarq $0x6, %rax
cmpq %r15, %rax
jbe 0x2d90
movq %r14, %rdi
movq %r15, %rsi
callq 0x51a6
movq %rax, %rbx
movq %r13, %rdi
movq %rax, %rsi
callq 0x50ae
cmpq $0x2, 0x28(%rax)
jb 0x2d69
leaq -0xc0(%rbp), %rdi
leaq 0x549b(%rip), %rsi      # 0x816e
leaq -0x29(%rbp), %rdx
callq 0x4d8c
movq %r13, %rdi
movq %rbx, %rsi
callq 0x50ae
movq 0x18(%rax), %r12
xorl %r14d, %r14d
movq %r13, %rdi
movq %rbx, %rsi
callq 0x50ae
addq $0x8, %rax
cmpq %rax, %r12
je 0x2d2c
cmpl %r14d, 0x40(%r12)
jle 0x2d1f
leaq 0x20(%r12), %rsi
leaq -0xc0(%rbp), %rdi
callq 0x2110
movl 0x40(%r12), %r14d
movq %r12, %rdi
callq 0x2120
movq %rax, %r12
jmp 0x2cee
movq %r13, %rdi
movq %rbx, %rsi
callq 0x50ae
movq %rax, %rdi
callq 0x6c72
movq %r13, %rdi
movq %rbx, %rsi
callq 0x50ae
movq %rax, %rdi
leaq -0xc0(%rbp), %rsi
callq 0x513c
leaq -0xc0(%rbp), %rdi
callq 0x2368
movq -0x40(%rbp), %r14
movq %r13, %rdi
movq %rbx, %rsi
callq 0x50ae
movq 0x18(%rax), %rsi
addq $0x20, %rsi
addq $0x20, %rbx
movq %rbx, %rdi
callq 0x2110
incl %r15d
jmp 0x2c8a
addq $0x18, %r14
jmp 0x2c75
cmpb $0x0, -0x4c(%rbp)
je 0x2dde
movq -0x68(%rbp), %rbx
addq $0x18, -0x48(%rbp)
leaq -0x78(%rbp), %rax
cmpq %rax, %rbx
je 0x2dde
leaq 0x20(%rbx), %rsi
movq 0x58(%rbx), %r14
movq -0x48(%rbp), %rdi
callq 0x51c8
addq $0x20, %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0x2110
movq %rbx, %rdi
callq 0x2120
movq %rax, %rbx
jmp 0x2da8
leaq -0x80(%rbp), %rdi
callq 0x551c
movq -0x38(%rbp), %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x2e70
jmp 0x2e51
movq %rax, %rbx
leaq -0xc0(%rbp), %rdi
callq 0x4dc8
jmp 0x2e67
jmp 0x2e51
jmp 0x2e51
jmp 0x2e58
jmp 0x2e51
movq %rax, %rbx
jmp 0x2e42
movq %rax, %rbx
jmp 0x2e36
movq %rax, %rbx
leaq -0xc0(%rbp), %rdi
callq 0x4efe
leaq -0xe0(%rbp), %rdi
callq 0x2368
leaq -0x100(%rbp), %rdi
jmp 0x2e62
jmp 0x2e58
jmp 0x2e51
jmp 0x2e51
movq %rax, %rbx
jmp 0x2e67
jmp 0x2e58
movq %rax, %rbx
leaq -0xc0(%rbp), %rdi
callq 0x2368
leaq -0x80(%rbp), %rdi
callq 0x551c
movq -0x38(%rbp), %rdi
callq 0x4f1e
movq %rbx, %rdi
callq 0x22e0
nop