-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_bin_dim.py
132 lines (119 loc) · 3.48 KB
/
get_bin_dim.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import numpy as np
import sys
import os
import binary_io
io_funcs = binary_io.BinaryIOCollection()
#need to make this better.. read in a file with phonelist etc. This is only for testing.
def get_items(infile):
with open(infile, 'r') as f:
itemlist=f.read().strip().split('___')
itemlist.pop(-1)
itemlist.insert(0,"0")
return itemlist
def one_hot(number, max_size):
""" Returns the one hot vector of a number, given the max"""
b = np.zeros(max_size,dtype=float)
b[number]=1.0
return b
def get_binary_dim(infile, phonefile, statefile):
"""Makes binary vectors"""
STATELIST=get_items(statefile)
PHONELIST=get_items(phonefile)
POSLIST=[0, 'aux','cc','content','det','in', 'md','pps','to','wp', 'punc']
POS2LIST=['b','m','e']
PRESENTLIST=[0, '+','-']
PLACELIST=[0, 'a','b','d','g','l','p','v', '-']
PLACE2LIST=[0,'a','d','l','s','-']
PLACE3LIST=[0,'a','f','l','n','r','s','-']
POSITIONLIST=[0, 'initial', 'single','final','mid']
STRENGTHLIST=[0, 1, 2, 3, '-']
STRENGTH2LIST=[0, '1', '3', '4']
CODALIST =[0,'coda', 'onset']
features=[STATELIST,
STATELIST,
STATELIST,
PHONELIST,
PRESENTLIST,
PLACE3LIST,
STRENGTHLIST,
PLACE2LIST,
STRENGTHLIST,
PRESENTLIST,
PLACELIST,
PRESENTLIST,
PHONELIST,
PRESENTLIST,
PLACE3LIST,
STRENGTHLIST,
PLACE2LIST,
STRENGTHLIST,
PRESENTLIST,
PLACELIST,
PRESENTLIST,
'FLOAT',
'FLOAT',
'FLOAT',
'FLOAT',
POS2LIST,
'FLOAT',
'FLOAT',
'FLOAT',
'FLOAT',
'FLOAT',
'FLOAT',
'FLOAT',
'FLOAT',
'FLOAT',
'FLOAT',
'FLOAT',
'FLOAT',
'FLOAT',
CODALIST,
CODALIST,
CODALIST,
'FLOAT',
'FLOAT',
'FLOAT',
'FLOAT',
STRENGTH2LIST,
STRENGTH2LIST,
POSITIONLIST,
PHONELIST,
PRESENTLIST,
PLACE3LIST,
STRENGTHLIST,
PLACE2LIST,
STRENGTHLIST,
PRESENTLIST,
PLACELIST,
PRESENTLIST,
'FLOAT',
'FLOAT',
POSLIST,
POSLIST,
POSLIST]
final_list=[]
with open(infile, 'r') as f:
feat_list=f.readline().strip().split()
for feat, list_name in enumerate(features):
#print "DEBUG: ", feat+2, list_name, feat_list[feat+2]
final_list.extend(get_element(feat_list[feat + 2], list_name))
#print len(final_list)
return len(final_list)
def get_element(item, find_list):
"""Returns the item index in a list"""
if (find_list == 'FLOAT'):
return [float(item)]
else:
return list(one_hot(np.where(np.asarray(find_list)==item)[0][0], len(find_list)))
def read_binary_file(filename, feature_dimension):
load_mat=io_funcs.load_binary_file(filename, feature_dimension)
return load_mat
if __name__=="__main__":
infile=sys.argv[1]
phonelist=sys.argv[2]
statelist=sys.argv[3]
print get_binary_dim(infile, phonelist, statelist)
#print bin_dim
# binary_mat = read_binary_file(outfile,711)
# print binary_mat