-
Notifications
You must be signed in to change notification settings - Fork 2
/
BLEU.py
33 lines (31 loc) · 1.18 KB
/
BLEU.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
from nltk.translate.bleu_score import sentence_bleu
import string
from nltk.translate.bleu_score import SmoothingFunction
import argparse
def avg_bleu(mono_path, pseudo_path):
with open(mono_path,'r') as f:
lines1 = f.readlines()
with open(pseudo_path,'r') as f:
lines2 = f.readlines()
assert(len(lines1)==len(lines2))
sum=0
valid_lines=0
for x in range(len(lines1)):
s1=''.join(i for i in lines1[x] if not(i in string.punctuation+'\n'))
s2=''.join(i for i in lines2[x] if not(i in string.punctuation+'\n'))
if not (not s1 or not s2):
valid_lines=valid_lines+1
if(s1==s2):
bleu=1
else:
bleu=sentence_bleu([s1.split()], s2.split(),smoothing_function=SmoothingFunction().method4)
sum=sum+bleu
avg_bleu=sum/valid_lines
print(avg_bleu)
return avg_bleu
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--mono',type=str,help='Input path to monoingual data txt file')
parser.add_argument('--pseudo',type=str,help='Input Path to pseudo txt file')
args = parser.parse_args()
avg_bleu(args.mono, args.pseudo)