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
use anyhow::Result;
use itertools::Itertools;
use serde_derive::{Deserialize, Serialize};
use crate::algorithms::tr_compares::{ilabel_compare, olabel_compare};
use crate::algorithms::tr_sort;
use crate::algorithms::compose::matchers::{MatchType, Matcher, SortedMatcher};
use crate::fst_traits::{AllocableFst, MutableFst, SerializableFst};
use crate::semirings::{SerializableSemiring, WeaklyDivisibleSemiring, WeightQuantize};
use crate::tests_openfst::FstTestData;
use crate::{Tr, Label, StateId, NO_LABEL, NO_STATE_ID};
#[derive(Serialize, Deserialize, Debug)]
struct SerializedTr {
ilabel: i32,
olabel: i32,
weight: String,
nextstate: i32,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct MatcherOperationResult {
state: usize,
label: usize,
match_type: usize,
trs: Vec<SerializedTr>,
}
pub struct MatcherTestData<F>
where
F: SerializableFst,
F::W: SerializableSemiring,
{
label: Label,
state: StateId,
match_type: MatchType,
trs: Vec<Tr<F::W>>,
}
impl MatcherOperationResult {
pub fn parse<F>(&self) -> MatcherTestData<F>
where
F: SerializableFst,
F::W: SerializableSemiring,
{
MatcherTestData {
label: self.label,
state: self.state,
match_type: match self.match_type {
1 => MatchType::MatchInput,
2 => MatchType::MatchOutput,
_ => panic!("Unsupported match_type : {:?}", self.match_type),
},
trs: self
.trs
.iter()
.map(|s| {
let ilabel = if s.ilabel == -1 {
NO_LABEL
} else {
s.ilabel as usize
};
let olabel = if s.olabel == -1 {
NO_LABEL
} else {
s.olabel as usize
};
let nextstate = if s.nextstate == -1 {
NO_STATE_ID
} else {
s.nextstate as usize
};
Tr::new(
ilabel,
olabel,
F::W::parse_text(s.weight.as_str()).unwrap().1,
nextstate,
)
})
.collect(),
}
}
}
pub fn test_sorted_matcher<F>(test_data: &FstTestData<F>) -> Result<()>
where
F: SerializableFst + MutableFst + AllocableFst,
F::W: SerializableSemiring + WeaklyDivisibleSemiring + WeightQuantize + 'static,
{
unimplemented!()
// let mut fst_isorted = test_data.raw.clone();
// tr_sort(&mut fst_isorted, ilabel_compare);
//
// let mut fst_osorted = test_data.raw.clone();
// tr_sort(&mut fst_osorted, olabel_compare);
//
// for matcher_data in &test_data.matcher {
// let fst = match matcher_data.match_type {
// MatchType::MatchInput => &fst_isorted,
// MatchType::MatchOutput => &fst_osorted,
// _ => bail!("Unsupported match_type : {:?}", matcher_data.match_type),
// };
//
// let matcher = SortedMatcher::new(fst, matcher_data.match_type)?;
// let trs: Vec<Tr<_>> = matcher
// .iter(matcher_data.state, matcher_data.label)?
// .map(|f| {
// f.into_tr(matcher_data.state, matcher_data.match_type)
// .unwrap()
// })
// .collect();
//
// assert_eq!(
// trs,
// matcher_data.trs.iter().cloned().collect_vec(),
// "Test matcher failed {:?} {:?} {:?}",
// matcher_data.state,
// matcher_data.label,
// matcher_data.match_type
// );
// }
// Ok(())
}