-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathutils.py
61 lines (47 loc) · 1.32 KB
/
utils.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
import os
import pickle
import sys
import nltk
from mosestokenizer import *
from nltk import word_tokenize
from nltk.tokenize import sent_tokenize
import json
nltk.download('stopwords')
detokenizer = MosesDetokenizer('en')
def read_demos(json_path):
asp_demos = json.load(open(json_path))
asp_dfs, demos = asp_demos["asp_definition"], asp_demos["demo"]
return demos, asp_dfs
def lower_check(text):
# The BAGEL dataset uses X to replace named entities.
if text.startswith("X ") == False:
text1 = text[0].lower() + text[1:]
else:
text1 = text
return text1
def add_dot(text):
if text.strip()[-1] != '.':
text = text.strip() + ' .'
new_text = text
return new_text
def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def read_pickle(file):
with open(file, 'rb') as f:
data = pickle.load(f)
return data
def save_pickle(data, file):
with open(file, 'wb') as f:
pickle.dump(data, f)
print(f'Saved to {file}.')
def detokenize(text: str):
words = text.split(" ")
return detokenizer(words)
# Restore print
def enablePrint():
sys.stdout = sys.__stdout__