-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrouge.py
More file actions
76 lines (69 loc) · 2.56 KB
/
Copy pathrouge.py
File metadata and controls
76 lines (69 loc) · 2.56 KB
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
# -*- coding: utf-8 -*-
def _safe_divide(numerator, denominator):
if denominator > 0:
return numerator / denominator
else:
return 0
def _safe_f1(matches, recall_total, precision_total, alpha=0.5):
recall_score = _safe_divide(matches, recall_total)
precision_score = _safe_divide(matches, precision_total)
denom = (1.0 - alpha) * precision_score + alpha * recall_score
if denom > 0.0:
return (precision_score * recall_score) / denom
else:
return 0.0
def lcs(a, b):
"""
Compute the length of the longest common subsequence between two sequences.
Time complexity: O(len(a) * len(b))
Space complexity: O(min(len(a), len(b)))
"""
# This is an adaptation of the standard LCS dynamic programming algorithm
# tweaked for lower memory consumption.
# Sequence a is laid out along the rows, b along the columns.
# Minimize number of columns to minimize required memory
if len(a) < len(b):
a, b = b, a
# Sequence b now has the minimum length
# Quit early if one sequence is empty
if len(b) == 0:
return 0
# Use a single buffer to store the counts for the current row, and
# overwrite it on each pass
row = [0] * len(b)
for ai in a:
left = 0
diag = 0
for j, bj in enumerate(b):
up = row[j]
if ai == bj:
value = diag + 1
else:
value = max(left, up)
row[j] = value
left = value
diag = up
# Return the last cell of the last row
return left
def rouge_l(ground_truth, gen_str):
"""
Compute the ROUGE-L score of a peer with respect to one or more models.
"""
matches = lcs(gen_str, ground_truth)
recall_total = len(gen_str)
precision_total = len(ground_truth)
return _safe_f1(matches, recall_total, precision_total)
import pandas as pd
pre = pd.read_csv(r'/media/ubuntu/data/OCRTijian/submit.csv')
answer = pd.read_csv(r'/media/ubuntu/data/OCRTijian/foreword/gpu/model/ocr_train_answer.csv')
df_all = pd.merge(answer, pre, on='id', how='left')
df = df_all.dropna(axis=0)
print(df)
score = 0
for i in range(len(df)):
# print(df['id'][i],rouge_l(df['content_x'].values[i], df['content_y'].values[i]))
print(i+1,df['content_x'].values[i])
print(df['content_y'].values[i])
print(rouge_l(df['content_x'].values[i], df['content_y'].values[i]))
score += rouge_l(df['content_x'].values[i], df['content_y'].values[i])
print(score/len(df_all))