-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
executable file
·67 lines (49 loc) · 1.7 KB
/
tools.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
62
63
64
65
66
67
import sys, os, codecs
def dynamicPrint(str):
sys.stdout.write('\r{}'.format(str))
sys.stdout.flush()
def checkDirectory(path):
if not os.path.exists(path):
os.makedirs(path)
def getListFromFile(path, encoding="utf-8"):
with codecs.open(path, encoding=encoding) as input_file:
return input_file.read().splitlines()
def getSetFromFile(path, encoding="utf-8"):
return set(getListFromFile(path, encoding=encoding))
def WWconcantenateTrain(path_ww, path_train, path_output):
with codecs.open(path_ww, encoding="utf-8") as wwFile:
data_ww = wwFile.read()
with codecs.open(path_train, encoding="utf-8") as trainFile:
data_train = trainFile.read()
with codecs.open(path_output, mode="w", encoding="utf-8") as output:
print >> output, data_ww + data_train[:-1]
def getIntersectionDict(dict1, dict2):
keys_intersections = set(dict1.keys()) & set(dict2.keys())
dict_intersection = dict()
for key in keys_intersections:
if key in dict1:
value1 = dict1[key]
else:
continue
if key in dict2:
value2 = dict2[key]
else:
continue
dict_intersection[key] = min(value1, value2)
return dict_intersection
def getUnionDict(dict1, dict2):
keys_union = set(dict1.keys()) | set(dict2.keys())
dict_union = dict()
for key in keys_union:
if key in dict1:
value1 = dict1[key]
else:
dict_union[key] = dict2[key]
continue
if key in dict2:
value2 = dict2[key]
else:
dict_union[key] = value1
continue
dict_union[key] = max(value1, value2)
return dict_union