-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate_oov.py
33 lines (30 loc) · 1.01 KB
/
calculate_oov.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
import os
import argparse
from collections import Counter
def calculate_oov_rate(in_file):
assert os.path.exists(in_file + "/train.txt")
with open(in_file + "/train.txt", "r") as train_set:
train_dic = Counter()
for line in train_set:
words = line.split() + ['<eos>']
for word in words:
train_dic[word] += 1
oov_list = []
tokens = 0
assert os.path.exists(in_file + "/test.txt")
with open(in_file + "/test.txt", "r") as test_set:
for line in test_set:
words = line.split() + ['<eos>']
for word in words:
if word not in train_dic:
oov_list.append(word)
tokens += 1
print("Number of OoV: {}".format(len(oov_list)))
print("Number of tokens in the test set: {}".format(tokens))
print("Oov Ratio: {}%".format(len(oov_list)/tokens*100))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--path', '-p', type=str, required=True,
help= 'path to the directory with the train and test datasets e.g. ptb')
args = parser.parse_args()
calculate_oov_rate(args.path)