// File: /workspace/github_cpp_makefile_star2_O1/szmer[P]brill/Brill-train.cpp
// Function: main
int main(int argc, char * argv[]) {
	if(argc < 3 || (argc > 3 && (argc-3) % 2 != 0)) {
    print_usage();
		return 1;
	}

  string output_file_name = string("");
  double improv_margin = 0.0;
  int max_rules = 1000;
  ofstream out;

  // Parse the arguments.
  for (int i = 3; i+1 < argc; i += 2) {
    if(strcmp(argv[i], "-o") == 0)
      output_file_name = string(argv[i+1]);
    if(strcmp(argv[i], "-m") == 0)
      improv_margin = atof(argv[i+1]);
    if(strcmp(argv[i], "-r") == 0)
      max_rules = atoi(argv[i+1]);
  }

	try {
    // Make the base corpus (containing the truth) and the tagset vector.
		Corpus base_corp = Corpus::load_file(argv[1]);
		vector<string> tagset = Corpus::load_tagset(argv[2]);

		// Tag the corpus with the baseline unigram tagger:
		Corpus old_corp = base_corp.tag_all_unigram(output_file_name != "");
    // /\ request leaving unigram table(if we intend to make a tagger file)
		double old_score = Corpus::compare_corpora(& base_corp, & old_corp);
		cout << "Unigram accuracy: " << old_score << endl;

		// Prepare a file where we will output discovered rules (btw put unigrams there).
    prepare_output_file(&out, output_file_name, &base_corp);

		RulesScoreTab tab(tagset, & base_corp);
		Corpus new_corp;
    double new_score = 10.0; // to be compared to the old score (first defined on unigrams above)
    int counter = 0;
		while(counter != max_rules && improv_margin < new_score-old_score) {

      if (counter > 0) { // some cleanup necessary, but not in the first iteration
        tab.clear(); // remove the old rules scores
        old_score = new_score;
        old_corp = new_corp;
      }

      // Try to find some useful rules.
			try { tab.examine(& old_corp); }
			catch(const char * e) { // if the program couldn't find any new good rules, just terminate
						// the loop, and rethrow any other exception
				if(strcmp(e,"No good rules found") == 0)	break;
				else	throw e;
			}

			Rule * winner = tab.get_winner();
			new_corp = winner -> transform_corp(& old_corp);

      // print the rule to the output file, if present
			if(out.is_open())
				out << winner->id << "\n";

			new_score = Corpus::compare_corpora(& base_corp, & new_corp);
			cout << (counter+1) << ". " << winner->description() << "-> " << new_score << endl;

      counter ++;
		} // generate the rules

		cout << "Final score, with " << counter << " rules: " << new_score << endl;
  } // try

  // Catch and print any errors, and exit.
	catch(const char * e) {
		cout << e << endl;
		return 1;
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x458, %rsp            # imm = 0x458
movl %edi, %r14d
cmpl $0x3, %edi
setge %al
testb %r14b, %al
jne 0x9e27
callq 0x9a4b
movl $0x1, %ebx
jmp 0xa495
movq %rsi, %rbx
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x430d(%rip), %rdx      # 0xe146
leaq -0x68(%rbp), %rdi
movq %rdx, %rsi
callq 0xbdf6
leaq -0x480(%rbp), %rdi
callq 0x2370
cmpl $0x5, %r14d
jl 0x9f22
xorpd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movl $0x3e8, -0x2c(%rbp)     # imm = 0x3E8
movl $0x4, %r13d
leaq -0x280(%rbp), %r12
movq -0x8(%rbx,%r13,8), %rdi
leaq 0x46da(%rip), %rsi      # 0xe55e
callq 0x22d0
testl %eax, %eax
jne 0x9ece
movq (%rbx,%r13,8), %rsi
movq %r12, %rdi
leaq -0x1a0(%rbp), %rdx
callq 0xa682
leaq -0x68(%rbp), %rdi
movq %r12, %rsi
callq 0x22a0
movq -0x280(%rbp), %rdi
leaq -0x270(%rbp), %rax
cmpq %rax, %rdi
je 0x9ece
movq -0x270(%rbp), %rsi
incq %rsi
callq 0x2240
movq -0x8(%rbx,%r13,8), %r15
movq %r15, %rdi
leaq 0x4684(%rip), %rsi      # 0xe561
callq 0x22d0
testl %eax, %eax
jne 0x9ef4
movq (%rbx,%r13,8), %rdi
callq 0x22b0
movsd %xmm0, -0x40(%rbp)
movq %r15, %rdi
leaq 0x4666(%rip), %rsi      # 0xe564
callq 0x22d0
testl %eax, %eax
jne 0x9f13
movq (%rbx,%r13,8), %rdi
callq 0x2330
movl %eax, -0x2c(%rbp)
addq $0x2, %r13
cmpl %r14d, %r13d
jl 0x9e78
jmp 0x9f32
movl $0x3e8, -0x2c(%rbp)     # imm = 0x3E8
xorpd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movq 0x8(%rbx), %rsi
leaq -0x1a0(%rbp), %rdi
callq 0x271a
movq 0x10(%rbx), %rsi
leaq -0x158(%rbp), %rdi
callq 0x2bea
leaq 0x41ed(%rip), %rsi      # 0xe146
leaq -0x68(%rbp), %rdi
callq 0x20b0
xorl %edx, %edx
testl %eax, %eax
setne %dl
leaq -0x1e8(%rbp), %rdi
leaq -0x1a0(%rbp), %rsi
callq 0x2dba
leaq -0x1a0(%rbp), %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x258c
movsd %xmm0, -0x48(%rbp)
movq 0x903d(%rip), %rdi      # 0x12fd8
leaq 0x45c5(%rip), %rsi      # 0xe567
movl $0x12, %edx
callq 0x2280
movq 0x9025(%rip), %rdi      # 0x12fd8
movsd -0x48(%rbp), %xmm0
callq 0x2350
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2190
leaq -0x110(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq %rsi, %rdx
leaq -0x120(%rbp), %rdi
callq 0xbd5c
leaq -0x480(%rbp), %rdi
leaq -0x120(%rbp), %rsi
leaq -0x1a0(%rbp), %rdx
callq 0x9d08
movq -0x120(%rbp), %rdi
cmpq %r14, %rdi
je 0xa03e
movq -0x110(%rbp), %rsi
incq %rsi
callq 0x2240
leaq -0x280(%rbp), %rdi
leaq -0x158(%rbp), %rsi
leaq -0x1a0(%rbp), %rdx
callq 0x51f6
xorpd %xmm0, %xmm0
leaq -0xe0(%rbp), %rax
movapd %xmm0, -0x20(%rax)
xorl %r13d, %r13d
movq %r13, -0x10(%rax)
movl $0x0, (%rax)
movq %r13, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %r13, 0x20(%rax)
movsd 0x3f93(%rip), %xmm0     # 0xe020
cmpl $0x0, -0x2c(%rbp)
movsd %xmm0, -0x38(%rbp)
je 0xa357
subsd -0x48(%rbp), %xmm0
ucomisd -0x40(%rbp), %xmm0
jbe 0xa357
decl -0x2c(%rbp)
movsd 0x3f69(%rip), %xmm0     # 0xe020
movsd %xmm0, -0x38(%rbp)
leaq -0xb0(%rbp), %r15
xorl %r14d, %r14d
testl %r14d, %r14d
je 0xa113
leaq -0x248(%rbp), %rdi
callq 0xd870
leaq -0x278(%rbp), %rdi
callq 0xd838
leaq -0x1e8(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0xb218
leaq -0x1d0(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0xb994
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
leaq -0x280(%rbp), %rbx
movq %rbx, %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x5d52
movq %rbx, %rdi
callq 0x99a8
movq %rax, %rbx
movq %r15, %rdi
movq %rax, %rsi
leaq -0x1e8(%rbp), %rdx
callq 0x3e12
xorpd %xmm0, %xmm0
movapd %xmm0, -0x140(%rbp)
movapd -0x100(%rbp), %xmm0
movq -0xf0(%rbp), %rax
movups -0xb0(%rbp), %xmm1
movaps %xmm1, -0x100(%rbp)
movq -0xa0(%rbp), %rcx
movq %rcx, -0xf0(%rbp)
movaps -0x140(%rbp), %xmm1
movapd %xmm0, -0x140(%rbp)
movq %rax, -0x130(%rbp)
movups %xmm1, -0xb0(%rbp)
movq $0x0, -0xa0(%rbp)
leaq -0x140(%rbp), %rdi
callq 0xaeaa
leaq -0xe8(%rbp), %rdi
callq 0xbd24
movq -0x88(%rbp), %rax
testq %rax, %rax
je 0xa21b
movl -0x90(%rbp), %ecx
movl %ecx, -0xe0(%rbp)
movq %rax, -0xd8(%rbp)
movupd -0x80(%rbp), %xmm0
movapd %xmm0, -0xd0(%rbp)
leaq -0xe0(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x70(%rbp), %rax
movq %rax, -0xc0(%rbp)
xorl %ecx, %ecx
movq %rcx, -0x88(%rbp)
leaq -0x90(%rbp), %rax
movq %rax, -0x80(%rbp)
movq %rax, -0x78(%rbp)
movq %rcx, -0x70(%rbp)
leaq -0x98(%rbp), %rdi
callq 0xaefa
movq %r15, %rdi
callq 0xaeaa
leaq -0x410(%rbp), %rdi
callq 0x2080
testb %al, %al
je 0xa266
movq 0x80(%rbx), %rsi
leaq -0x480(%rbp), %rdi
callq 0x20f0
movl $0x1, %edx
movq %rax, %rdi
leaq 0x42fb(%rip), %rsi      # 0xe55c
callq 0x2280
leaq -0x1a0(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x258c
movsd %xmm0, -0x38(%rbp)
leal 0x1(%r14), %r13d
movq 0x8d4f(%rip), %rdi      # 0x12fd8
movl %r13d, %esi
callq 0x2390
movq %rax, %r12
movl $0x2, %edx
movq %rax, %rdi
leaq 0x42d7(%rip), %rsi      # 0xe57a
callq 0x2280
movq %r15, %rdi
movq %rbx, %rsi
callq 0x38d8
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
movq %r12, %rdi
callq 0x2280
movq %rax, %rbx
movl $0x3, %edx
movq %rax, %rdi
leaq 0x42a2(%rip), %rsi      # 0xe57d
callq 0x2280
movq %rbx, %rdi
movsd -0x38(%rbp), %xmm0
callq 0x2350
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2190
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0xa339
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x2240
cmpl %r14d, -0x2c(%rbp)
je 0xa357
movsd -0x38(%rbp), %xmm0
subsd -0x48(%rbp), %xmm0
ucomisd -0x40(%rbp), %xmm0
movl %r13d, %r14d
ja 0xa0c6
movq 0x8c7a(%rip), %rdi      # 0x12fd8
leaq 0x421c(%rip), %rsi      # 0xe581
movl $0x12, %edx
callq 0x2280
movq 0x8c62(%rip), %rdi      # 0x12fd8
movl %r13d, %esi
callq 0x2390
movq %rax, %rbx
leaq 0x420c(%rip), %rsi      # 0xe594
movl $0x8, %edx
movq %rax, %rdi
callq 0x2280
movq %rbx, %rdi
movsd -0x38(%rbp), %xmm0
callq 0x2350
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2190
leaq -0xe8(%rbp), %rdi
callq 0xaefa
leaq -0x100(%rbp), %rdi
callq 0xaeaa
leaq -0x218(%rbp), %rdi
callq 0xb04a
leaq -0x248(%rbp), %rdi
callq 0xb0da
leaq -0x278(%rbp), %rdi
callq 0xb1ba
leaq -0x1d0(%rbp), %rdi
callq 0xaefa
leaq -0x1e8(%rbp), %rdi
callq 0xaeaa
leaq -0x158(%rbp), %rdi
callq 0xa8b6
leaq -0x188(%rbp), %rdi
callq 0xaefa
leaq -0x1a0(%rbp), %rdi
callq 0xaeaa
xorl %ebx, %ebx
leaq -0x58(%rbp), %r14
movq 0x8b7f(%rip), %rax      # 0x12fd0
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x478(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x480(%rbp,%rcx)
callq 0x21f0
leaq -0x388(%rbp), %rdi
callq 0x20d0
movq -0x68(%rbp), %rdi
cmpq %r14, %rdi
je 0xa495
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2240
movl %ebx, %eax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xa535
movq %rdx, %r15
movq %rax, %rbx
movq -0x120(%rbp), %rdi
cmpq %r14, %rdi
je 0xa600
movq -0x110(%rbp), %rsi
incq %rsi
callq 0x2240
jmp 0xa600
movq %rdx, %r15
movq %rax, %rbx
jmp 0xa618
movq %rdx, %r15
movq %rax, %rbx
jmp 0xa624
movq %rdx, %r15
movq %rax, %rbx
jmp 0xa63c
movq %rax, %rbx
jmp 0xa512
movq %rax, %rbx
jmp 0xa52b
movq %rax, %rbx
leaq -0x480(%rbp), %rdi
callq 0x22f0
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0xa52b
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2240
movq %rbx, %rdi
callq 0x23b0
jmp 0xa5ac
movq %rdx, %r15
movq %rax, %rbx
jmp 0xa600
movq %rdx, %r15
movq %rax, %rbx
cmpl $0x1, %r15d
jne 0xa5dc
movq %rbx, %rdi
callq 0x2100
movq %rax, %rbx
leaq 0x3d82(%rip), %rsi      # 0xe2e4
movq %rax, %rdi
callq 0x22d0
testl %eax, %eax
jne 0xa57b
callq 0x2360
movl %r14d, %r13d
jmp 0xa357
movl $0x8, %edi
callq 0x2130
movq %rbx, (%rax)
movq 0x8a31(%rip), %rsi      # 0x12fc0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2380
movq %rdx, %r15
movq %rax, %rbx
callq 0x2360
jmp 0xa5dc
jmp 0xa5ac
jmp 0xa5ac
jmp 0xa5ac
movq %rdx, %r15
movq %rax, %rbx
jmp 0xa5dc
movq %rdx, %r15
movq %rax, %rbx
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0xa5dc
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x2240
leaq -0xe8(%rbp), %rdi
callq 0xaefa
leaq -0x100(%rbp), %rdi
callq 0xaeaa
leaq -0x280(%rbp), %rdi
callq 0xae6e
leaq -0x1d0(%rbp), %rdi
callq 0xaefa
leaq -0x1e8(%rbp), %rdi
callq 0xaeaa
leaq -0x158(%rbp), %rdi
callq 0xa8b6
leaq -0x188(%rbp), %rdi
callq 0xaefa
leaq -0x1a0(%rbp), %rdi
callq 0xaeaa
cmpl $0x1, %r15d
jne 0xa506
movq %rbx, %rdi
callq 0x2100
movq 0x8983(%rip), %rdi      # 0x12fd8
movq %rax, %rsi
callq 0x2220
movq %rax, %rdi
callq 0x2120
callq 0x2360
movl $0x1, %ebx
jmp 0xa446
movq %rax, %rbx
callq 0x2360
jmp 0xa506
nop