-
Notifications
You must be signed in to change notification settings - Fork 2
/
pst_convert_to_pfa.m
237 lines (157 loc) · 4.22 KB
/
pst_convert_to_pfa.m
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
function PFA=pst_convert_to_pfa(TREE,ALPHABET)
% Converts a PST (probabilistic suffix tree) to a probabilistic finite automaton
%
% convert a PST to a PFA using the following algorithm:
%
% 1) include all leaves and internal nodes
% 2) starting with the root, flow downward (in terms of order)
% 3) arcs represent generating a given symbol, connecting each state
% to the state representing the longest suffix of the next generated symbol
% 4) after visiting all nodes, we're finished
% from the tree, start from the bottom and work our way up
counter=1;
for i=1:length(TREE)
for j=1:length(TREE(i).label)
%PFA(counter).states=TREE(i).string{j};
if TREE(i).internal(j)
continue;
end
PFA(counter).label=TREE(i).label{j};
PFA(counter).trans=TREE(i).g_sigma_s(:,j)';
PFA(counter).order=i;
PFA(counter).recurrent=1;
counter=counter+1;
end
end
% first we need to ensure that every leaf and its prefix are included...
% add the necessary arcs
% node order
% add the unused one symbol NODES FIRST!!!
for i=1:length(PFA)
ORDER(i)=length(PFA(i).label);
end
%
exitflag=0;
for i=1:length(PFA)
conns=find(PFA(i).trans>0);
% for each connection, get the sequence and PFA
currlabel=PFA(i).label;
if strcmp(currlabel,'epsilon')
currlabel='';
end
for j=1:length(conns)
node=get_suffix([ currlabel ALPHABET(conns(j)) ],PFA);
node
% add any new nodes if necessary, should be one symbol since no other
% suffix exists
% get the edge of the first order
if isnan(node)
order_edge=max(find(ORDER==1));
insertion_point=order_edge+1;
NEWPFA=PFA;
%NEWPFA(insertion_point).states=conns(j);
NEWPFA(insertion_point).label=ALPHABET(conns(j));
NEWPFA(insertion_point).trans=PFA(1).trans';
NEWPFA(insertion_point+1:end+1)=PFA(insertion_point:end);
PFA=NEWPFA;
NEWORDER=ORDER;
NEWORDER(insertion_point)=1;
NEWORDER(insertion_point+1:end+1)=ORDER(insertion_point:end);
ORDER=NEWORDER;
clear NEWORDER;
clear NEWPFA;
node=insertion_point;
end
PFA(i).arcs(j)=node;
PFA(i).arcs_p(j)=PFA(i).trans(conns(j));
PFA(i).arcs_states(j)=ALPHABET(conns(j));
end
end
% need to add all prefixes here or we'll be caught with orphaned nodes
exitflag=0;
% add all prefixes that don't already exist as states, use the longest suffix for transition probabilities
while ~exitflag
exitflag=1;
for i=1:length(PFA)
if PFA(i).order>2
% get list of prefixes
brkflag=0;
match=[];
for j=length(PFA(i).label)-1:-1:1
currprefix=PFA(i).label(1:j)
match=[];
for k=1:length(PFA)
if strcmp(currprefix,PFA(k).label)
match=k
break;
end
end
% if match is empty append a new prefix
if isempty(match)
newnode=length(PFA)+1;
node=get_suffix(currprefix,PFA);
PFA(newnode).label=currprefix
if isnan(node)
node=1;
end
node
PFA(newnode).trans=PFA(node).trans;
PFA(newnode).order=length(currprefix);
conns=find(PFA(newnode).trans>0);
for k=1:length(conns)
node=get_suffix([ currlabel ALPHABET(conns(k)) ],PFA);
PFA(newnode).arcs(k)=node;
PFA(newnode).arcs_p(k)=PFA(newnode).trans(conns(k));
PFA(newnode).arcs_states(k)=ALPHABET(conns(k));
end
exitflag=0;
end
end
end
end
end
for i=1:length(PFA)
conns=find(PFA(i).trans>0);
% for each connection, get the sequence and PFA
currlabel=PFA(i).label;
if strcmp(currlabel,'epsilon')
currlabel='';
end
for j=1:length(conns)
node=get_suffix([ currlabel ALPHABET(conns(j)) ],PFA);
node
% add any new nodes if necessary, should be one symbol since no other
% suffix exists
% get the edge of the first order
if isnan(node)
error('Broken connection');
end
PFA(i).arcs(j)=node;
PFA(i).arcs_p(j)=PFA(i).trans(conns(j));
PFA(i).arcs_states(j)=ALPHABET(conns(j));
end
end
end
function node=get_suffix(SEQUENCE,PFA)
%
% for a given tree, find the longest suffix of a given sequence
%
% remove the prefix until we find our match (i.e. longest suffix match)
exitflag=0;
curr=SEQUENCE;
while ~exitflag
if isempty(curr)
node=NaN;
exitflag=1;
break;
end
for j=length(PFA):-1:1
match=strcmp(PFA(j).label,curr);
if match
node=j;
exitflag=1;
end
end
curr=curr(2:end);
end
end