diff --git a/Cargo.lock b/Cargo.lock index 1427e2fdd..6d49a23e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -696,7 +696,7 @@ dependencies = [ [[package]] name = "rustfst" -version = "0.13.0" +version = "0.13.1" dependencies = [ "anyhow", "bimap", @@ -726,7 +726,7 @@ dependencies = [ [[package]] name = "rustfst-cli" -version = "0.13.0" +version = "0.13.1" dependencies = [ "anyhow", "clap", @@ -740,7 +740,7 @@ dependencies = [ [[package]] name = "rustfst-ffi" -version = "0.13.0" +version = "0.13.1" dependencies = [ "anyhow", "downcast-rs", diff --git a/rustfst-ffi/src/algorithms/compose.rs b/rustfst-ffi/src/algorithms/compose.rs index 56782d39c..f0cd7717b 100644 --- a/rustfst-ffi/src/algorithms/compose.rs +++ b/rustfst-ffi/src/algorithms/compose.rs @@ -269,7 +269,7 @@ pub extern "C" fn fst_matcher_config_destroy(ptr: *mut CMatcherConfig) -> RUSTFS } unsafe { - Box::from_raw(ptr); + drop(Box::from_raw(ptr)); } Ok(()) }) @@ -283,7 +283,7 @@ pub extern "C" fn fst_compose_config_destroy(ptr: *mut CComposeConfig) -> RUSTFS } unsafe { - Box::from_raw(ptr); + drop(Box::from_raw(ptr)); } Ok(()) }) diff --git a/rustfst-ffi/src/fst/mod.rs b/rustfst-ffi/src/fst/mod.rs index 4eef0e4fe..548b61215 100644 --- a/rustfst-ffi/src/fst/mod.rs +++ b/rustfst-ffi/src/fst/mod.rs @@ -327,7 +327,7 @@ pub fn fst_destroy(fst_ptr: *mut CFst) -> RUSTFST_FFI_RESULT { } unsafe { - Box::from_raw(fst_ptr); + drop(Box::from_raw(fst_ptr)); } Ok(()) }) diff --git a/rustfst-ffi/src/iterators.rs b/rustfst-ffi/src/iterators.rs index 4d2756fb3..045e667fa 100644 --- a/rustfst-ffi/src/iterators.rs +++ b/rustfst-ffi/src/iterators.rs @@ -113,7 +113,7 @@ pub extern "C" fn trs_iterator_destroy(iter_ptr: *mut CTrsIterator) -> RUSTFST_F } unsafe { - Box::from_raw(iter_ptr); + drop(Box::from_raw(iter_ptr)); } Ok(()) }) @@ -274,7 +274,7 @@ pub extern "C" fn mut_trs_iterator_destroy(iter_ptr: *mut CMutTrsIterator) -> RU } unsafe { - Box::from_raw(iter_ptr); + drop(Box::from_raw(iter_ptr)); } Ok(()) }) @@ -333,7 +333,7 @@ pub extern "C" fn state_iterator_destroy(iter_ptr: *mut CStateIterator) -> RUSTF } unsafe { - Box::from_raw(iter_ptr); + drop(Box::from_raw(iter_ptr)); } Ok(()) }) diff --git a/rustfst-ffi/src/string_path.rs b/rustfst-ffi/src/string_path.rs index 0fcf6339b..fec6a5236 100644 --- a/rustfst-ffi/src/string_path.rs +++ b/rustfst-ffi/src/string_path.rs @@ -17,7 +17,7 @@ pub extern "C" fn string_path_destroy(iter_ptr: *mut CStringPath) -> RUSTFST_FFI } unsafe { - Box::from_raw(iter_ptr); + drop(Box::from_raw(iter_ptr)); } Ok(()) }) diff --git a/rustfst-ffi/src/string_paths_iterator.rs b/rustfst-ffi/src/string_paths_iterator.rs index 4027ccfd7..1d6897298 100644 --- a/rustfst-ffi/src/string_paths_iterator.rs +++ b/rustfst-ffi/src/string_paths_iterator.rs @@ -105,7 +105,7 @@ pub extern "C" fn string_paths_iterator_destroy( } unsafe { - Box::from_raw(iter_ptr); + drop(Box::from_raw(iter_ptr)); } Ok(()) }) diff --git a/rustfst-ffi/src/symbol_table.rs b/rustfst-ffi/src/symbol_table.rs index aaedec548..5dfb240e5 100644 --- a/rustfst-ffi/src/symbol_table.rs +++ b/rustfst-ffi/src/symbol_table.rs @@ -206,7 +206,7 @@ pub extern "C" fn symt_destroy(symt_ptr: *mut CSymbolTable) -> RUSTFST_FFI_RESUL } unsafe { - Box::from_raw(symt_ptr); + drop(Box::from_raw(symt_ptr)); } Ok(()) }) diff --git a/rustfst-ffi/src/tr.rs b/rustfst-ffi/src/tr.rs index 315d955a8..5d0aa1048 100644 --- a/rustfst-ffi/src/tr.rs +++ b/rustfst-ffi/src/tr.rs @@ -150,7 +150,7 @@ pub extern "C" fn tr_delete(tr_ptr: *mut CTr) -> RUSTFST_FFI_RESULT { } unsafe { - Box::from_raw(tr_ptr); + drop(Box::from_raw(tr_ptr)); } Ok(()) }) diff --git a/rustfst-ffi/src/trs.rs b/rustfst-ffi/src/trs.rs index bd7f647a2..a3c1c2feb 100644 --- a/rustfst-ffi/src/trs.rs +++ b/rustfst-ffi/src/trs.rs @@ -92,7 +92,7 @@ pub extern "C" fn trs_vec_delete(trs_ptr: *mut CTrs) -> RUSTFST_FFI_RESULT { } unsafe { - Box::from_raw(trs_ptr); + drop(Box::from_raw(trs_ptr)); } Ok(()) }) diff --git a/rustfst/src/algorithms/compose/compose_fst.rs b/rustfst/src/algorithms/compose/compose_fst.rs index 493955a3b..62b56aad7 100644 --- a/rustfst/src/algorithms/compose/compose_fst.rs +++ b/rustfst/src/algorithms/compose/compose_fst.rs @@ -1,7 +1,9 @@ -use anyhow::Result; use std::borrow::Borrow; use std::fmt::Debug; use std::path::Path; +use std::sync::Arc; + +use anyhow::Result; use crate::algorithms::compose::compose_filters::{ ComposeFilter, ComposeFilterBuilder, SequenceComposeFilterBuilder, @@ -18,7 +20,6 @@ use crate::fst_traits::{AllocableFst, CoreFst, Fst, FstIterator, MutableFst, Sta use crate::parsers::SerializeBinary; use crate::semirings::{Semiring, SerializableSemiring}; use crate::{StateId, SymbolTable, TrsVec}; -use std::sync::Arc; type InnerLazyFst = LazyFst, Cache>; @@ -160,6 +161,9 @@ where /// Turns the Lazy FST into a static one. pub fn compute + AllocableFst>(&self) -> Result { + // Small trick to make sure that both FSTs are fully expanded. + // iterate_lazy(self.0.op.fst1.borrow())?; + // iterate_lazy(self.0.op.fst2.borrow())?; self.0.compute() } } @@ -356,11 +360,12 @@ where #[cfg(test)] mod test { - use super::*; use crate::algorithms::compose::matchers::SortedMatcher; use crate::fst_impls::VectorFst; use crate::semirings::TropicalWeight; + use super::*; + #[test] fn test_compose_fst_sync() { fn is_sync() {} diff --git a/rustfst/src/algorithms/compose/compose_fst_op.rs b/rustfst/src/algorithms/compose/compose_fst_op.rs index 28f1dfb8d..ca5e80954 100644 --- a/rustfst/src/algorithms/compose/compose_fst_op.rs +++ b/rustfst/src/algorithms/compose/compose_fst_op.rs @@ -82,8 +82,8 @@ where >, match_type: MatchType, properties: FstProperties, - fst1: B1, - fst2: B2, + pub(crate) fst1: B1, + pub(crate) fst2: B2, } impl Clone for ComposeFstOp @@ -389,11 +389,12 @@ where fn compute_start(&self) -> Result> { let compose_filter = self.compose_filter_builder.build()?; let s1 = self.fst1.borrow().start(); + // Let's put it here to force the fst2 to have its start state computed in the case of a Lazy Fst. + let s2 = self.fst2.borrow().start(); if s1.is_none() { return Ok(None); } let s1 = s1.unwrap(); - let s2 = self.fst2.borrow().start(); if s2.is_none() { return Ok(None); } diff --git a/rustfst/src/algorithms/compose/interval_reach_visitor.rs b/rustfst/src/algorithms/compose/interval_reach_visitor.rs index 0a24d42bf..f56f21001 100644 --- a/rustfst/src/algorithms/compose/interval_reach_visitor.rs +++ b/rustfst/src/algorithms/compose/interval_reach_visitor.rs @@ -1,9 +1,12 @@ +use std::cmp::Ordering; + +use anyhow::Result; + use crate::algorithms::compose::{IntInterval, IntervalSet}; use crate::algorithms::dfs_visit::Visitor; use crate::fst_traits::Fst; use crate::semirings::Semiring; use crate::{StateId, Tr}; -use std::cmp::Ordering; static UNASSIGNED: usize = std::usize::MAX; @@ -30,18 +33,18 @@ impl<'a, W: Semiring, F: Fst> Visitor<'a, W, F> for IntervalReachVisitor<'a, fn init_visit(&mut self, _fst: &'a F) {} /// Invoked when state discovered (2nd arg is DFS tree root). - fn init_state(&mut self, s: StateId, _root: StateId) -> bool { + fn init_state(&mut self, s: StateId, _root: StateId) -> Result { while self.isets.len() <= (s as usize) { self.isets.push(IntervalSet::default()); } while self.state2index.len() <= (s as usize) { self.state2index.push(UNASSIGNED); } - if let Some(final_weight) = self.fst.final_weight(s).unwrap() { + if let Some(final_weight) = self.fst.final_weight(s)? { if !final_weight.is_zero() { let interval_set = &mut self.isets[s as usize]; if self.index == UNASSIGNED { - if self.fst.num_trs(s).unwrap() > 0 { + if self.fst.num_trs(s)? > 0 { panic!("IntervalReachVisitor: state2index map must be empty for this FST") } let index = self.state2index[s as usize]; @@ -56,7 +59,7 @@ impl<'a, W: Semiring, F: Fst> Visitor<'a, W, F> for IntervalReachVisitor<'a, } } } - true + Ok(true) } /// Invoked when tree transition to white/undiscovered state examined. diff --git a/rustfst/src/algorithms/compose/label_reachable.rs b/rustfst/src/algorithms/compose/label_reachable.rs index 12698419a..827b908b9 100644 --- a/rustfst/src/algorithms/compose/label_reachable.rs +++ b/rustfst/src/algorithms/compose/label_reachable.rs @@ -10,6 +10,7 @@ use crate::algorithms::{fst_convert_from_ref, tr_sort}; use crate::fst_impls::VectorFst; use crate::fst_properties::FstProperties; use crate::fst_traits::{CoreFst, ExpandedFst, Fst, MutableFst}; +use crate::prelude::compose::LookaheadRelabelFst; use crate::semirings::Semiring; use crate::{Label, StateId, Tr, Trs, EPS_LABEL, NO_LABEL, UNASSIGNED}; @@ -60,6 +61,19 @@ impl LabelReachableData { .or_insert_with(|| n as Label + 1) } + pub fn relabel_unmut(&self, label: Label) -> Result