blob: e89e277b475ccda6c16726217f0b349bb9a2a326 [file] [log] [blame]
daza5cb357d2020-10-06 12:03:12 +02001from CoNLL_Annotation import *
2from collections import Counter
3import pandas as pd
4import numpy as np
5from sklearn.metrics import precision_recall_fscore_support as eval_f1
6from tabulate import tabulate
7
8
9def eval_lemma(sys, gld):
10 match, err, symbol = 0, 0, []
11 mistakes = []
12 for i, gld_tok in enumerate(gld.tokens):
13 if gld_tok.lemma == sys.tokens[i].lemma:
14 match += 1
15 elif not sys.tokens[i].lemma.isalnum(): # This was added because Turku does not lemmatize symbols (it only copies them) => ERR ((',', '--', ','), 43642)
16 symbol.append(sys.tokens[i].lemma)
17 if sys.tokens[i].word == sys.tokens[i].lemma:
18 match += 1
19 else:
20 err += 1
21 else:
22 err += 1
23 mistakes.append((gld_tok.word, gld_tok.lemma, sys.tokens[i].lemma))
24 return match, err, symbol, mistakes
25
26
27def eval_pos(sys, gld):
28 match, mistakes = 0, []
29 y_gld, y_pred = [], []
30 for i, gld_tok in enumerate(gld.tokens):
31 y_gld.append(gld_tok.pos_tag)
32 y_pred.append(sys.tokens[i].pos_tag)
33 all_pos_labels.add(gld_tok.pos_tag)
34 all_pos_labels.add(sys.tokens[i].pos_tag)
35 if gld_tok.pos_tag == sys.tokens[i].pos_tag:
36 match += 1
37 else:
38 mistakes.append((gld_tok.word, gld_tok.pos_tag, sys.tokens[i].pos_tag))
39 return y_gld, y_pred, match, mistakes
40
41
42
43if __name__ == "__main__":
44
45 # Read the Original TiGeR Annotations
46 gld_filename = "/home/daza/datasets/TIGER_conll/tiger_release_aug07.corrected.16012013.conll09"
47 gld_generator = read_conll_generator(gld_filename, token_class=CoNLL09_Token)
48
49 # Read the Annotations Generated by the Automatic Parser [Turku]
50 sys_filename = "/home/daza/datasets/TIGER_conll/tiger_turku_parsed.conllu"
51 sys_generator = read_conll_generator(sys_filename, token_class=CoNLLUP_Token)
52
53 lemma_all_match, lemma_all_err, lemma_all_mistakes = 0, 0, []
54 lemma_all_symbols = []
55 pos_all_match, pos_all_err, pos_all_mistakes = 0, 0, []
56 pos_all_pred, pos_all_gld = [], []
57 all_pos_labels = set()
58
59 for i, (s,g) in enumerate(zip(sys_generator, gld_generator)):
60 assert len(s.tokens) == len(g.tokens), "Token Mismatch!"
61 # Lemmas ...
62 lemma_match, lemma_err, lemma_sym, mistakes = eval_lemma(s,g)
63 lemma_all_match += lemma_match
64 lemma_all_err += lemma_err
65 lemma_all_mistakes += mistakes
66 lemma_all_symbols += lemma_sym
67 # POS Tags ...
68 pos_gld, pos_pred, pos_match, pos_mistakes = eval_pos(s, g)
69 pos_all_pred += pos_pred
70 pos_all_gld += pos_gld
71 pos_all_match += pos_match
72 pos_all_err += len(pos_mistakes)
73 pos_all_mistakes += pos_mistakes
74
75 # Lemmas ...
76 print(f"Lemma Matches = {lemma_all_match} || Errors = {lemma_all_err} || Symbol Chars = {len(lemma_all_symbols)}")
77 print(f"Lemma Accuracy = {(lemma_all_match*100/(lemma_all_match + lemma_all_err)):.2f}%\n")
78 lemma_miss_df = pd.DataFrame(lemma_all_mistakes, columns =['Gold_Word', 'Gold_Lemma', 'Sys_Lemma']).value_counts()
79 lemma_miss_df.to_csv(path_or_buf="LemmaErrors.tsv", sep="\t")
80
81 # POS Tags ...
82 print(f"POS Matches = {pos_all_match} || Errors = {pos_all_err}")
83 print(f"POS Tagging Accuracy = {(pos_all_match*100/(pos_all_match + pos_all_err)):.2f}%\n")
84 pos_miss_df = pd.DataFrame(pos_all_mistakes, columns =['Gold_Word', 'Gold_POS', 'Sys_POS']).value_counts()
85 pos_miss_df.to_csv(path_or_buf="POS-Errors.tsv", sep="\t")
86
87 ordered_labels = sorted(all_pos_labels)
88 p_labels, r_labels, f_labels, support = eval_f1(y_true=pos_all_gld, y_pred=pos_all_pred, labels=ordered_labels , average=None)
89 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])
90 print("\n\n")
91 print(tabulate(scores_per_label, headers=["POS Tag","Precision", "Recall", "F1"], floatfmt=".2f"))
92 print("\n Total Prec, Rec, and F1 Score: ")
93 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)
94 print(p_labels*100, r_labels*100, f_labels*100)
95
96