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
use super::semiring::{Weight};
use super::{ExpandedFst, MutableFst, StateId, Arc};
use std::vec::Vec;
pub fn extendfinal<W: Weight, F: ExpandedFst<W> + MutableFst<W>> (mut fst: F) -> F {
let mut finalstates: Vec<StateId> = Vec::new();
for i in 0..fst.get_numstates() {
if fst.is_final(i) {
finalstates.push(i)
}
}
let newfinal = fst.add_state(W::one());
for state in finalstates {
let finalweight = fst.get_finalweight(state);
fst.add_arc(state, newfinal, 0, 0, finalweight);
fst.set_finalweight(state, W::zero());
}
fst
}
pub fn unextendfinal<W: Weight, F: ExpandedFst<W> + MutableFst<W>> (mut fst: F) -> F {
let mut finalstate = 0;
for i in 0..fst.get_numstates() {
if fst.is_final(i) {
finalstate = i;
break
}
}
for i in 0..fst.get_numstates() {
for arc in fst.arc_iter(i) {
if arc.ilabel() == 0 && arc.olabel() == 0 && arc.nextstate() == finalstate {
fst.set_finalweight(i, arc.weight());
}
}
}
fst.del_state(finalstate);
fst
}
pub fn reverse<W: Weight, F: ExpandedFst<W> + MutableFst<W>, O: MutableFst<W>> (mut ifst: F) -> O {
ifst = extendfinal(ifst);
let mut ofst = O::new();
if let Some(osyms) = ifst.get_osyms() {
ofst.set_isyms(osyms);
}
if let Some(isyms) = ifst.get_isyms() {
ofst.set_osyms(isyms);
}
for i in 0..ifst.get_numstates() {
ofst.add_state(W::zero());
if ifst.is_final(i) {
ofst.set_start(i);
}
}
ofst.set_finalweight(ifst.get_start().unwrap(), W::one());
for i in 0..ifst.get_numstates() {
for arc in ifst.arc_iter(i) {
ofst.add_arc(arc.nextstate(), i, arc.ilabel(), arc.olabel(), arc.weight().reverse())
}
}
ofst
}
pub mod shortestpath;
pub mod connect;