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
use anyhow::Result;
use std::ops::Deref;
use crate::fst_properties::FstProperties;
use crate::fst_traits::MutableFst;
use crate::semirings::Semiring;
use crate::Tr;
use crate::{Label, StateId, EPS_LABEL};
#[derive(Clone, Debug)]
pub struct FinalTr<W: Semiring> {
pub ilabel: Label,
pub olabel: Label,
pub weight: W,
}
#[derive(PartialEq)]
pub enum MapFinalAction {
MapNoSuperfinal,
MapAllowSuperfinal,
MapRequireSuperfinal,
}
pub trait TrMapper<S: Semiring> {
fn tr_map(&self, tr: &mut Tr<S>) -> Result<()>;
fn final_tr_map(&self, final_tr: &mut FinalTr<S>) -> Result<()>;
fn final_action(&self) -> MapFinalAction;
fn properties(&self, inprops: FstProperties) -> FstProperties;
}
impl<S: Semiring, T: TrMapper<S>, TP: Deref<Target = T>> TrMapper<S> for TP {
fn tr_map(&self, tr: &mut Tr<S>) -> Result<()> {
self.deref().tr_map(tr)
}
fn final_tr_map(&self, final_tr: &mut FinalTr<S>) -> Result<()> {
self.deref().final_tr_map(final_tr)
}
fn final_action(&self) -> MapFinalAction {
self.deref().final_action()
}
fn properties(&self, inprops: FstProperties) -> FstProperties {
self.deref().properties(inprops)
}
}
pub fn tr_map<W, F, M>(ifst: &mut F, mapper: &M) -> Result<()>
where
W: Semiring,
F: MutableFst<W>,
M: TrMapper<W>,
{
if ifst.start().is_none() {
return Ok(());
}
let inprops = ifst.properties();
let final_action = mapper.final_action();
let mut superfinal: Option<StateId> = None;
if final_action == MapFinalAction::MapRequireSuperfinal {
let superfinal_id = ifst.add_state();
superfinal = Some(superfinal_id);
ifst.set_final(superfinal_id, W::one()).unwrap();
}
for state in 0..(ifst.num_states() as StateId) {
unsafe {
let mut it_tr = ifst.tr_iter_unchecked_mut(state);
for idx_tr in 0..it_tr.len() {
let mut tr = it_tr.get_unchecked(idx_tr).clone();
mapper.tr_map(&mut tr)?;
it_tr.set_tr_unchecked(idx_tr, tr);
}
}
if let Some(w) = unsafe { ifst.final_weight_unchecked(state) } {
let mut final_tr = FinalTr {
ilabel: EPS_LABEL,
olabel: EPS_LABEL,
weight: w,
};
mapper.final_tr_map(&mut final_tr)?;
match final_action {
MapFinalAction::MapNoSuperfinal => {
if final_tr.ilabel != EPS_LABEL || final_tr.olabel != EPS_LABEL {
bail!("TrMap: Non-zero tr labels for superfinal tr")
}
unsafe {
ifst.set_final_unchecked(state, final_tr.weight);
}
}
MapFinalAction::MapAllowSuperfinal => {
if Some(state) != superfinal {
if final_tr.ilabel != EPS_LABEL || final_tr.olabel != EPS_LABEL {
if superfinal.is_none() {
let superfinal_id = ifst.add_state();
superfinal = Some(superfinal_id);
unsafe {
ifst.set_final_unchecked(superfinal_id, W::one());
}
}
unsafe {
ifst.add_tr_unchecked(
state,
Tr::new(
final_tr.ilabel,
final_tr.olabel,
final_tr.weight,
superfinal.unwrap(),
),
);
ifst.delete_final_weight_unchecked(state);
}
} else {
unsafe {
ifst.set_final_unchecked(state, final_tr.weight);
}
}
}
}
MapFinalAction::MapRequireSuperfinal => {
if Some(state) != superfinal {
if final_tr.ilabel != EPS_LABEL
|| final_tr.olabel != EPS_LABEL
|| !final_tr.weight.is_zero()
{
unsafe {
ifst.add_tr_unchecked(
state,
Tr::new(
final_tr.ilabel,
final_tr.olabel,
final_tr.weight,
superfinal.unwrap(),
),
);
ifst.delete_final_weight_unchecked(state);
}
}
}
}
};
}
}
ifst.set_properties_with_mask(mapper.properties(inprops), FstProperties::all_properties());
Ok(())
}