-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
44 lines (37 loc) · 1.14 KB
/
util.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
'''
Utility functions
'''
import sys
import json
import cPickle as pkl
#json loads strings as unicode; we currently still work with Python 2 strings, and need conversion
def unicode_to_utf8(d):
return dict((key.encode("UTF-8"), value) for (key,value) in d.items())
def load_dict(filename):
try:
with open(filename, 'rb') as f:
return unicode_to_utf8(json.load(f))
except:
with open(filename, 'rb') as f:
return pkl.load(f)
def load_config(basename):
try:
with open('%s.json' % basename, 'rb') as f:
return json.load(f)
except:
try:
with open('%s.pkl' % basename, 'rb') as f:
return pkl.load(f)
except:
sys.stderr.write('Error: config file {0}.json is missing\n'.format(basename))
sys.exit(1)
def seqs2words(seq, inverse_target_dictionary, join=True):
words = []
for w in seq:
if w == 0:
break
if w in inverse_target_dictionary:
words.append(inverse_target_dictionary[w])
else:
words.append('UNK')
return ' '.join(words) if join else words