-
Notifications
You must be signed in to change notification settings - Fork 0
/
punkt_eval.py
89 lines (61 loc) · 2.22 KB
/
punkt_eval.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from nltk.tokenize import sent_tokenize
from . import token_extract as t_e
def eval_sent_tokenize(path, max_text_count=530):
"""
Evaluates the standard nltk sentence tokeniser on Russian National Corpus.
:return: accuracy, precision, recall, f1-measure of segmentation
"""
# extract raw and segmented versions of text
_, _, segmented_text, raw_text = t_e.extract(path, max_text_count)
# tokenise raw text using punkt tokeniser
res = sent_tokenize(raw_text, 'russian')
# remove redundant characters from results and segmented text
# so that we can compare them
table = str.maketrans('', '', "(){}<>:;.!,\"?`'")
new_c = [sent.translate(table) for sent in segmented_text]
new_res = [sent.translate(table) for sent in res]
new_text = raw_text.translate(table)
# evaluate the accuracy, precision, recall and f1-measure
print(evaluate(new_c, new_res))
def evaluate(gold, result):
gold_sents = remove_whitespaces(gold)
result_sents = remove_whitespaces(result)
gold_binary = get_binary(gold_sents)
result_binary = get_binary(result_sents)
predicted_pos = result_binary.count(1)
condition_pos = gold_binary.count(1)
true_pos = 0.0
length = len(gold_binary)
correct = 0.0
if length == len(result_binary):
for i in range(length):
if gold_binary[i] == result_binary[i]:
correct += 1.0
if gold_binary[i] == 1:
true_pos += 1.0
else:
return 0, 0, 0, 0
accuracy = correct / length
precision = true_pos / predicted_pos
recall = true_pos / condition_pos
f1 = 2 * precision * recall / (precision + recall)
return accuracy, precision, recall, f1
def get_binary(sents):
binary = []
for sent in sents:
if len(sent) >= 1:
binary.extend((len(sent) - 1) * [0])
binary.append(1)
return binary
def remove_whitespaces(sents):
temp = []
for s in sents:
ss = remove_whitespaces_raw(s)
temp.append(ss)
return temp
def remove_whitespaces_raw(raw_text):
ss = raw_text.replace('\n', '')
ss = ss.replace('\r', '')
ss = ss.replace('\t', '')
ss = ss.replace(' ', '')
return ss