Successfully evaluated several taggers
diff --git a/TIGER/evaluate.py b/TIGER/evaluate.py
new file mode 100644
index 0000000..0dffc20
--- /dev/null
+++ b/TIGER/evaluate.py
@@ -0,0 +1,137 @@
+from lib.CoNLL_Annotation import *
+from collections import Counter
+import pandas as pd
+import numpy as np
+from sklearn.metrics import precision_recall_fscore_support as eval_f1
+from tabulate import tabulate
+import logging, argparse, sys
+from datetime import datetime
+
+
+def eval_lemma(sys, gld):
+ match, err, symbol = 0, 0, []
+ mistakes = []
+ for i, gld_tok in enumerate(gld.tokens):
+ if gld_tok.lemma == sys.tokens[i].lemma:
+ match += 1
+ elif not sys.tokens[i].lemma.isalnum(): # This was added because Turku does not lemmatize symbols (it only copies them) => ERR ((',', '--', ','), 43642)
+ symbol.append(sys.tokens[i].lemma)
+ if sys.tokens[i].word == sys.tokens[i].lemma:
+ match += 1
+ else:
+ err += 1
+ else:
+ err += 1
+ mistakes.append((gld_tok.word, gld_tok.lemma, sys.tokens[i].lemma))
+ return match, err, symbol, mistakes
+
+
+def eval_pos(sys, gld):
+ match, mistakes = 0, []
+ y_gld, y_pred = [], []
+ for i, gld_tok in enumerate(gld.tokens):
+ y_gld.append(gld_tok.pos_tag)
+ y_pred.append(sys.tokens[i].pos_tag)
+ all_pos_labels.add(gld_tok.pos_tag)
+ if gld_tok.pos_tag == sys.tokens[i].pos_tag:
+ match += 1
+ else:
+ mistakes.append((gld_tok.word, gld_tok.pos_tag, sys.tokens[i].pos_tag))
+ return y_gld, y_pred, match, mistakes
+
+
+
+if __name__ == "__main__":
+ """
+ EVALUATIONS:
+ python TIGER/evaluate.py -t Turku\
+ --sys_file /home/daza/datasets/TIGER_conll/tiger_turku_parsed.conllu \
+ --gld_file /home/daza/datasets/TIGER_conll/tiger_release_aug07.corrected.16012013.conll09
+
+ python TIGER/evaluate.py -t SpaCy\
+ --sys_file /home/daza/datasets/TIGER_conll/tiger_spacy_parsed.conllu \
+ --gld_file /home/daza/datasets/TIGER_conll/tiger_release_aug07.corrected.16012013.conll09
+
+ python TIGER/evaluate.py -t RNNTagger\
+ --sys_file /home/daza/datasets/TIGER_conll/tiger_all.parsed.RNNTagger.conll \
+ --gld_file /home/daza/datasets/TIGER_conll/tiger_release_aug07.corrected.16012013.conll09
+
+ python TIGER/evaluate.py -t TreeTagger\
+ --sys_file /home/daza/datasets/TIGER_conll/tiger_all.parsed.TreeTagger.conll \
+ --gld_file /home/daza/datasets/TIGER_conll/tiger_release_aug07.corrected.16012013.conll09
+ """
+
+ # =====================================================================================
+ # INPUT PARAMS
+ # =====================================================================================
+ parser = argparse.ArgumentParser()
+ parser.add_argument("-s", "--sys_file", help="System output in CoNLL-U Format", required=True)
+ parser.add_argument("-g", "--gld_file", help="Gold Labels to evaluate in CoNLL-U Format", required=True)
+ parser.add_argument("-t", "--type_sys", help="Which system produced the outputs", default="system")
+ args = parser.parse_args()
+
+ # =====================================================================================
+ # LOGGING INFO ...
+ # =====================================================================================
+ logger = logging.getLogger(__name__)
+ console_hdlr = logging.StreamHandler(sys.stdout)
+ file_hdlr = logging.FileHandler(filename=f"logs/Eval_Tiger.{args.type_sys}.log")
+ logging.basicConfig(level=logging.INFO, handlers=[console_hdlr, file_hdlr])
+ now_is = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
+ logger.info(f"\n\nEvaluating TIGER Corpus {now_is}")
+
+ # Read the Original TiGeR Annotations
+ gld_generator = read_conll_generator(args.gld_file, token_class=CoNLL09_Token)
+ # Read the Annotations Generated by the Automatic Parser [Turku, SpaCy, RNNTagger]
+ if args.type_sys == "RNNTagger":
+ sys_generator = read_conll_generator(args.sys_file, token_class=RNNTagger_Token)
+ elif args.type_sys == "TreeTagger":
+ sys_generator = read_conll_generator(args.sys_file, token_class=RNNTagger_Token, sent_sep="</S>")
+ else:
+ sys_generator = read_conll_generator(args.sys_file, token_class=CoNLLUP_Token)
+
+ lemma_all_match, lemma_all_err, lemma_all_mistakes = 0, 0, []
+ lemma_all_symbols = []
+ pos_all_match, pos_all_err, pos_all_mistakes = 0, 0, []
+ pos_all_pred, pos_all_gld = [], []
+ all_pos_labels = set()
+
+ for i, (s,g) in enumerate(zip(sys_generator, gld_generator)):
+ # print([x.word for x in s.tokens])
+ # print([x.word for x in g.tokens])
+ assert len(s.tokens) == len(g.tokens), f"Token Mismatch! S={len(s.tokens)} G={len(g.tokens)} IX={i+1}"
+ # Lemmas ...
+ lemma_match, lemma_err, lemma_sym, mistakes = eval_lemma(s,g)
+ lemma_all_match += lemma_match
+ lemma_all_err += lemma_err
+ lemma_all_mistakes += mistakes
+ lemma_all_symbols += lemma_sym
+ # POS Tags ...
+ pos_gld, pos_pred, pos_match, pos_mistakes = eval_pos(s, g)
+ pos_all_pred += pos_pred
+ pos_all_gld += pos_gld
+ pos_all_match += pos_match
+ pos_all_err += len(pos_mistakes)
+ pos_all_mistakes += pos_mistakes
+
+ # Lemmas ...
+ logger.info(f"Lemma Matches = {lemma_all_match} || Errors = {lemma_all_err} || Symbol Chars = {len(lemma_all_symbols)}")
+ logger.info(f"Lemma Accuracy = {(lemma_all_match*100/(lemma_all_match + lemma_all_err)):.2f}%\n")
+ lemma_miss_df = pd.DataFrame(lemma_all_mistakes, columns =['Gold_Word', 'Gold_Lemma', 'Sys_Lemma']).value_counts()
+ lemma_miss_df.to_csv(path_or_buf=f"outputs/LemmaErrors.{args.type_sys}.tsv", sep="\t")
+
+ # POS Tags ...
+ logger.info(f"POS Matches = {pos_all_match} || Errors = {pos_all_err}")
+ logger.info(f"POS Tagging Accuracy = {(pos_all_match*100/(pos_all_match + pos_all_err)):.2f}%\n")
+ pos_miss_df = pd.DataFrame(pos_all_mistakes, columns =['Gold_Word', 'Gold_POS', 'Sys_POS']).value_counts()
+ pos_miss_df.to_csv(path_or_buf=f"outputs/POS-Errors.{args.type_sys}.tsv", sep="\t")
+
+ ordered_labels = sorted(all_pos_labels)
+ p_labels, r_labels, f_labels, support = eval_f1(y_true=pos_all_gld, y_pred=pos_all_pred, labels=ordered_labels , average=None)
+ scores_per_label = zip(ordered_labels, [x*100 for x in p_labels], [x*100 for x in r_labels], [x*100 for x in f_labels])
+ logger.info("\n\n")
+ logger.info(tabulate(scores_per_label, headers=["POS Tag","Precision", "Recall", "F1"], floatfmt=".2f"))
+ p_labels, r_labels, f_labels, support = eval_f1(y_true=np.array(pos_all_gld), y_pred=np.array(pos_all_pred), average='micro', zero_division=0)
+ logger.info(f"Total Prec = {p_labels*100}\tRec = {r_labels*100}\tF1 = {f_labels*100}")
+
+
\ No newline at end of file