| from 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 |
| |
| |
| 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) |
| all_pos_labels.add(sys.tokens[i].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__": |
| |
| # Read the Original TiGeR Annotations |
| gld_filename = "/home/daza/datasets/TIGER_conll/tiger_release_aug07.corrected.16012013.conll09" |
| gld_generator = read_conll_generator(gld_filename, token_class=CoNLL09_Token) |
| |
| # Read the Annotations Generated by the Automatic Parser [Turku] |
| sys_filename = "/home/daza/datasets/TIGER_conll/tiger_turku_parsed.conllu" |
| sys_generator = read_conll_generator(sys_filename, 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)): |
| assert len(s.tokens) == len(g.tokens), "Token Mismatch!" |
| # 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 ... |
| print(f"Lemma Matches = {lemma_all_match} || Errors = {lemma_all_err} || Symbol Chars = {len(lemma_all_symbols)}") |
| print(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="LemmaErrors.tsv", sep="\t") |
| |
| # POS Tags ... |
| print(f"POS Matches = {pos_all_match} || Errors = {pos_all_err}") |
| print(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="POS-Errors.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]) |
| print("\n\n") |
| print(tabulate(scores_per_label, headers=["POS Tag","Precision", "Recall", "F1"], floatfmt=".2f")) |
| print("\n Total Prec, Rec, and F1 Score: ") |
| 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) |
| print(p_labels*100, r_labels*100, f_labels*100) |
| |
| |