#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int none=0, insertion=0, deletion=0, hit=0;

void go_to_next_line(FILE *fin1) {
  char c;
  if (!feof(fin1)) {
    do {
      fscanf(fin1, "%c", &c);
    } while (c != '\n' && !feof(fin1));
  }
}

int check_boundary(char *s1, char *s2, char *t1, char *t2) {
  int succeed = 1, lens1 = strlen(s1)-1, lent1 = strlen(t1)-1;
  int i;
  for (i=0; i<3; i++) {
    if (s2[i] != t2[i]) {
      succeed = 0;
      break;
    }
    if (s1[lens1-i] != t1[lent1-i]) {
      succeed = 0;
      break;
    }
  }
  return succeed;
}

void check(FILE *l, FILE *f) {
  char sl[10][200], sf[10][200], space;
  int lens=0, lenf=0, i, j;

  int ins=insertion, del = deletion, h = hit;

  printf("Correct Segmentation: ");
  while (!feof(l)) {
    fscanf(l, "%s", sl[lens++]);
    printf("%s ", sl[lens-1]);
    fscanf(l, "%c", &space);
    if(space == '\n') {
      break;
    }
  }
  printf("\n");

  printf("Undivide++ Segmentation: ");
  while (!feof(f)) {
    fscanf(f, "%s", sf[lenf++]);
    printf("%s ", sf[lenf-1]);
    fscanf(f, "%c", &space);
    if(space == '\n') {
      break;
    }
  }
  printf("\n");

  if (lens == lenf && lens == 1) {
    none++;
    return;
  }
  if (lens == lenf) {
    int totalfound = 0;
    for (i=0; i<lens-1; i++) {
      int found = 0;
      for (j=0; j<lens-1; j++) {
	int ch = check_boundary(sf[i], sf[i+1], sl[j], sl[j+1]);
	if (ch == 1) {
	  found = 1;
	  break;
	}
      }
      if (found) {
	totalfound++;
	hit++;
      }
      else {
	deletion++;
      }
    } 
    if (totalfound < lenf) {
      insertion += lenf - totalfound - 1;
    }
  }
  else if (lens < lenf) {
    int totalfound = 0;
    for (i=0; i<lenf-1; i++) {
      int found = 0;
      for (j=0; j<lens-1; j++) {
	int ch = check_boundary(sf[i], sf[i+1], sl[j], sl[j+1]);
	if (ch == 1) {
	  found = 1;
	  totalfound++;
	  break;
	}
      }
      if (found) {
	hit++;
      }
      else {
	insertion++;
      }
    }
    if (totalfound < lens) {
      deletion += lens - totalfound - 1;
    }
  }
  else {
    int totalfound = 0;
    for (i=0; i<lens-1; i++) {
      int found = 0;
      for (j=0; j<lenf-1; j++) {
	int ch = check_boundary(sl[i], sl[i+1], sf[j], sf[j+1]);
	if (ch == 1) {
	  found = 1;
	  break;
	}
      }
      if (found) {
	totalfound++;
	hit++;
      }
      else {
	deletion++;
      }
    }
    if (totalfound < lenf) {
      insertion += lenf - totalfound - 1;
    }
  }
  printf("insertions=%d deletions=%d hits=%d\n", insertion-ins, deletion-del, hit-h);
}

int main() {
  FILE *list = fopen("colplus_annotate.txt", "r");
  char s1[200], s2[200], space;

  while (!feof(list)) {
    FILE *fin1 = fopen("colplus.txt", "r");

    fscanf(list, "%s", s1);
    fscanf(list, "%c", &space);

    while (!feof(fin1)) {
      fscanf(fin1, "%s", s2);
      fscanf(fin1, "%c", &space);
      if (!strcmp(s1, s2)) {
	check(list, fin1);
	printf("tot_insertions=%d tot_deletion=%d tot_hit=%d\n\n", insertion, deletion, hit);
	break;
      }
      else {
	go_to_next_line(fin1);
      }
    }
    fclose(fin1);
  }

  float precision = (1.0*hit)/(hit+insertion);
  float recall = (1.0*hit)/(hit+deletion);
  float fscore = 2*precision*recall/(precision+recall);
  FILE *score = fopen("scores.txt", "w");
  printf("\nprecision = %f\nrecall = %f\n\nf-score = %f\n", precision, recall, fscore);
  fprintf(score, "precision = %f\nrecall = %f\n\nf-score = %f\n", precision, recall, fscore);
  fclose(list);
}
