-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwCFG_tree_sampling.py
77 lines (63 loc) · 2.13 KB
/
wCFG_tree_sampling.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
import numpy as np
import os
prob = 0.52
N_trees = 200
N_term_MAX = 4000
N_grammars = 120
N_prom = 20
p = prob
for n_grammar in range(N_grammars):
directory = 'samples/trees/tree_samples_p.'+str(p)+'_grammar.'+str(n_grammar)
if not os.path.exists('samples'):
os.makedirs('samples')
if not os.path.exists('samples/trees'):
os.makedirs('samples/trees')
if not os.path.exists(directory):
os.makedirs(directory)
for N_t in range(N_trees):
p = prob
tree = np.array([1])
tree_index = np.array([[0,0,0]])
weight_index = []
shell = 1
tree_index = np.append(tree_index,[[shell,1,0]],axis=0)
N_no_term=1
N_term=0
N_no_term_temp=0
N_term_temp=0
while N_no_term != 0:
N_no_term_temp=0
shell+=1
#print tree
tree_temp = np.array([])
weight_temp=[]
for i in tree:
if i == 1:
if np.random.rand() < (1.-p):
N_no_term_temp+=2
tree_temp = np.append(tree_temp,[1,1])
weight_temp.append(('M'))
else:
N_term_temp+=1
tree_temp = np.append(tree_temp,[0])
weight_temp.append(('O'))
else:
tree_temp = np.append(tree_temp,[0])
weight_temp.append(('X'))
weight_index.append(weight_temp)
N_no_term=N_no_term_temp
N_term=N_term_temp
tree_index=np.append(tree_index,[[shell,N_no_term,N_term]],axis=0)
tree=tree_temp
#print weight_temp
if N_no_term == 0:
True
#print tree
if N_term == N_prom:
p = 0.98
if N_term >= N_term_MAX:
break
np.save(directory+'/tree_No.'+str(N_t),np.array(weight_index,dtype=object))
#print weight_index
#print '__________________________________________-'
print n_grammar