// File: /workspace/github_cpp_makefile_star2_O2/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 $0x438, %rsp            # imm = 0x438
movl %edi, %r14d
cmpl $0x3, %edi
setge %al
testb %r14b, %al
jne 0x69e6
callq 0x67e1
pushq $0x1
popq %rbx
jmp 0x6e6f
movq %rsi, %rbx
leaq 0x4756(%rip), %rsi      # 0xb146
leaq -0x68(%rbp), %rdi
leaq -0x458(%rbp), %rdx
callq 0x6f56
leaq -0x458(%rbp), %rdi
callq 0x2340
movl %r14d, %r13d
movl $0x3e8, -0x34(%rbp)     # imm = 0x3E8
xorpd %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
pushq $0x4
popq %r15
leaq -0x210(%rbp), %r14
cmpq %r13, %r15
jae 0x6ac5
movq -0x8(%rbx,%r15,8), %r12
cmpb $0x2d, (%r12)
jne 0x6abc
cmpb $0x6f, 0x1(%r12)
jne 0x6a82
cmpb $0x0, 0x2(%r12)
jne 0x6a82
movq (%rbx,%r15,8), %rsi
movq %r14, %rdi
leaq -0xe8(%rbp), %rdx
callq 0x6f56
leaq -0x68(%rbp), %rdi
movq %r14, %rsi
callq 0x2260
movq %r14, %rdi
callq 0x23e8
movq -0x8(%rbx,%r15,8), %r12
cmpb $0x2d, (%r12)
jne 0x6abc
cmpb $0x6d, 0x1(%r12)
jne 0x6aa0
cmpb $0x0, 0x2(%r12)
jne 0x6aa0
movq (%rbx,%r15,8), %rdi
callq 0x2270
movsd %xmm0, -0x48(%rbp)
cmpb $0x72, 0x1(%r12)
jne 0x6abc
cmpb $0x0, 0x2(%r12)
jne 0x6abc
movq (%rbx,%r15,8), %rdi
callq 0x22f0
movl %eax, -0x34(%rbp)
addq $0x2, %r15
jmp 0x6a2a
movq 0x8(%rbx), %rsi
leaq -0xe8(%rbp), %rdi
callq 0x2676
movq 0x10(%rbx), %rsi
leaq -0x80(%rbp), %rdi
callq 0x29e8
leaq 0x465d(%rip), %rsi      # 0xb146
leaq -0x68(%rbp), %rdi
callq 0x766c
movzbl %al, %edx
leaq -0x178(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x2b2c
leaq -0xe8(%rbp), %rdi
leaq -0x178(%rbp), %rsi
callq 0x251c
movsd %xmm0, -0x40(%rbp)
movq 0x94b1(%rip), %rdi      # 0xffd8
leaq 0x4a30(%rip), %rsi      # 0xb55e
callq 0x21f0
movq %rax, %rdi
movsd -0x40(%rbp), %xmm0
callq 0x2310
movq %rax, %rdi
callq 0x2110
leaq -0xa0(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x2290
leaq -0x458(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
leaq -0xe8(%rbp), %rdx
callq 0x68ea
leaq -0xa0(%rbp), %rdi
callq 0x23e8
leaq -0x210(%rbp), %rdi
leaq -0x80(%rbp), %rsi
leaq -0xe8(%rbp), %rdx
callq 0x4470
xorpd %xmm0, %xmm0
leaq -0x110(%rbp), %rax
andq $0x0, -0x10(%rax)
movapd %xmm0, -0x20(%rax)
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
xorl %r14d, %r14d
movsd 0x4456(%rip), %xmm0     # 0xb020
movsd %xmm0, -0x30(%rbp)
leaq -0x210(%rbp), %r12
leaq -0x178(%rbp), %rsi
leaq -0x258(%rbp), %r15
movq %rsi, %rbx
cmpl %r14d, -0x34(%rbp)
je 0x6ddd
movsd -0x30(%rbp), %xmm0
subsd -0x40(%rbp), %xmm0
ucomisd -0x48(%rbp), %xmm0
jbe 0x6ddd
testl %r14d, %r14d
je 0x6c3a
leaq -0x210(%rbp), %rdi
callq 0x671c
movq %rbx, %rdi
leaq -0x130(%rbp), %rsi
callq 0x767a
movq %rbx, %rsi
leaq -0x210(%rbp), %r12
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movq %r12, %rdi
callq 0x4c18
movq %r12, %rdi
callq 0x6740
movq %rax, %r12
movq %r15, %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x3616
leaq -0x130(%rbp), %rdi
movq %r15, %rsi
callq 0x76a4
movq %r15, %rdi
callq 0x70e8
leaq -0x3e8(%rbp), %rdi
callq 0x2070
testb %al, %al
je 0x6ca5
movq 0x80(%r12), %rsi
leaq -0x458(%rbp), %rdi
callq 0x20d0
movq %rax, %rdi
leaq 0x48bc(%rip), %rsi      # 0xb55c
callq 0x21f0
leaq -0xe8(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x251c
movsd %xmm0, -0x30(%rbp)
incl %r14d
movq 0x9311(%rip), %rdi      # 0xffd8
movl %r14d, %esi
callq 0x2360
movq %rax, %rdi
leaq 0x4898(%rip), %rsi      # 0xb571
callq 0x21f0
movq %rax, %r13
movq %r15, %rdi
movq %r12, %rsi
callq 0x32ca
movq %r13, %rdi
movq %r15, %rsi
callq 0x21e0
movq %rax, %rdi
leaq 0x4873(%rip), %rsi      # 0xb574
callq 0x21f0
movq %rax, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x2310
movq %rax, %rdi
callq 0x2110
movq %r15, %rdi
callq 0x23e8
leaq -0x210(%rbp), %r12
movq %rbx, %rsi
jmp 0x6be7
jmp 0x6d96
movq %rdx, %r15
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x23e8
jmp 0x6ee4
movq %rdx, %r15
movq %rax, %rbx
jmp 0x6ef0
movq %rdx, %r15
movq %rax, %rbx
jmp 0x6ef9
movq %rdx, %r15
movq %rax, %rbx
jmp 0x6f05
movq %rax, %rbx
jmp 0x6d85
movq %rax, %rbx
jmp 0x6d8e
movq %rax, %rbx
leaq -0x458(%rbp), %rdi
callq 0x22c0
leaq -0x68(%rbp), %rdi
callq 0x23e8
movq %rbx, %rdi
callq 0x2370
movq %rdx, %r15
movq %rax, %rbx
jmp 0x6ee4
jmp 0x6ec6
movq %rdx, %r15
movq %rax, %rbx
cmpl $0x1, %r15d
jne 0x6ecc
movq %rbx, %rdi
callq 0x20e0
movq %rax, %rbx
leaq 0x451c(%rip), %rsi      # 0xb2e4
movq %rax, %rdi
callq 0x22a0
testl %eax, %eax
jne 0x6e83
callq 0x2320
movq 0x91f4(%rip), %rdi      # 0xffd8
leaq 0x478d(%rip), %rsi      # 0xb578
callq 0x21f0
movq %rax, %rdi
movl %r14d, %esi
callq 0x2360
leaq 0x4789(%rip), %rsi      # 0xb58b
movq %rax, %rdi
callq 0x21f0
movq %rax, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x2310
movq %rax, %rdi
callq 0x2110
leaq -0x130(%rbp), %rdi
callq 0x70e8
leaq -0x210(%rbp), %rdi
callq 0x76ce
leaq -0x178(%rbp), %rdi
callq 0x70e8
leaq -0x80(%rbp), %rdi
callq 0x7108
leaq -0xe8(%rbp), %rdi
callq 0x70e8
xorl %ebx, %ebx
leaq -0x458(%rbp), %rdi
callq 0x22c0
leaq -0x68(%rbp), %rdi
callq 0x23e8
movl %ebx, %eax
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x8
popq %rdi
callq 0x2120
movq %rbx, (%rax)
movq 0x9133(%rip), %rsi      # 0xffc8
movq %rax, %rdi
xorl %edx, %edx
callq 0x2350
movq %rdx, %r15
movq %rax, %rbx
callq 0x2320
jmp 0x6ecc
jmp 0x6ec6
jmp 0x6ec6
jmp 0x6ec6
movq %rdx, %r15
movq %rax, %rbx
leaq -0x258(%rbp), %rdi
callq 0x23e8
jmp 0x6ecc
movq %rdx, %r15
movq %rax, %rbx
leaq -0x130(%rbp), %rdi
callq 0x70e8
leaq -0x210(%rbp), %rdi
callq 0x76ce
leaq -0x178(%rbp), %rdi
callq 0x70e8
leaq -0x80(%rbp), %rdi
callq 0x7108
leaq -0xe8(%rbp), %rdi
callq 0x70e8
cmpl $0x1, %r15d
jne 0x6d79
movq %rbx, %rdi
callq 0x20e0
movq 0x90ba(%rip), %rdi      # 0xffd8
movq %rax, %rsi
callq 0x21f0
movq %rax, %rdi
callq 0x2110
callq 0x2320
pushq $0x1
popq %rbx
jmp 0x6e5a
movq %rax, %rbx
callq 0x2320
jmp 0x6d79