Skip to content

is_final revamp #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions rustfst-cli/src/cmds/compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ use std::sync::Arc;

use anyhow::Result;

use rustfst::algorithms::compose::{
compose, ComposeFst, ComposeFstOpOptions, LabelReachableData, MatcherFst,
};
use rustfst::fst_impls::VectorFst;
use rustfst::semirings::TropicalWeight;

use crate::binary_fst_algorithm::BinaryFstAlgorithm;
use rustfst::algorithms::compose::compose_filters::{
AltSequenceComposeFilterBuilder, ComposeFilterBuilder,
};
Expand All @@ -20,9 +13,16 @@ use rustfst::algorithms::compose::lookahead_matchers::{
LabelLookAheadMatcher, LookaheadMatcher, MatcherFlagsTrait,
};
use rustfst::algorithms::compose::matchers::{MatchType, Matcher, MatcherFlags, SortedMatcher};
use rustfst::algorithms::lazy::SimpleHashMapCache;
use rustfst::algorithms::compose::{
compose, ComposeFst, ComposeFstOpOptions, LabelReachableData, MatcherFst,
};
use rustfst::algorithms::lazy::SimpleVecCache;
use rustfst::algorithms::tr_compares::ILabelCompare;
use rustfst::algorithms::tr_sort;
use rustfst::fst_impls::VectorFst;
use rustfst::semirings::TropicalWeight;

use crate::binary_fst_algorithm::BinaryFstAlgorithm;

#[derive(Debug, Clone, Copy)]
pub enum ComposeType {
Expand Down Expand Up @@ -136,13 +136,13 @@ impl BinaryFstAlgorithm for ComposeAlgorithm {
None,
);

let dyn_fst = ComposeFst::<_, _, SimpleHashMapCache<_>>::new_with_options(
let dyn_fst = ComposeFst::<_, _, SimpleVecCache<_>>::new_with_options(
graph1look,
fst_2,
compose_options,
)?;

dyn_fst.compute()
Ok(dyn_fst.compute_2())
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions rustfst/src/algorithms/closure/closure_fst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ where
self.0.num_trs_unchecked(s)
}

fn is_final(&self, state_id: usize) -> Result<bool> {
self.0.is_final(state_id)
}

unsafe fn is_final_unchecked(&self, state_id: usize) -> bool {
self.0.is_final_unchecked(state_id)
}

fn get_trs(&self, state_id: usize) -> Result<Self::TRS> {
self.0.get_trs(state_id)
}
Expand All @@ -111,9 +119,17 @@ where
self.0.num_input_epsilons(state)
}

unsafe fn num_input_epsilons_unchecked(&self, state: usize) -> usize {
self.0.num_input_epsilons_unchecked(state)
}

fn num_output_epsilons(&self, state: usize) -> Result<usize> {
self.0.num_output_epsilons(state)
}

unsafe fn num_output_epsilons_unchecked(&self, state: usize) -> usize {
self.0.num_output_epsilons_unchecked(state)
}
}

impl<'a, W, F> StateIterator<'a> for ClosureFst<W, F>
Expand Down
16 changes: 16 additions & 0 deletions rustfst/src/algorithms/compose/add_on.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ impl<W: Semiring, F: CoreFst<W>, T> CoreFst<W> for FstAddOn<F, T> {
self.fst.num_trs_unchecked(s)
}

fn is_final(&self, state_id: usize) -> Result<bool> {
self.fst.is_final(state_id)
}

unsafe fn is_final_unchecked(&self, state_id: usize) -> bool {
self.fst.is_final_unchecked(state_id)
}

fn get_trs(&self, state_id: usize) -> Result<Self::TRS> {
self.fst.get_trs(state_id)
}
Expand All @@ -73,9 +81,17 @@ impl<W: Semiring, F: CoreFst<W>, T> CoreFst<W> for FstAddOn<F, T> {
self.fst.num_input_epsilons(state)
}

unsafe fn num_input_epsilons_unchecked(&self, state: usize) -> usize {
self.fst.num_input_epsilons_unchecked(state)
}

fn num_output_epsilons(&self, state: usize) -> Result<usize> {
self.fst.num_output_epsilons(state)
}

unsafe fn num_output_epsilons_unchecked(&self, state: usize) -> usize {
self.fst.num_output_epsilons_unchecked(state)
}
}

impl<'a, F: StateIterator<'a>, T> StateIterator<'a> for FstAddOn<F, T> {
Expand Down
14 changes: 7 additions & 7 deletions rustfst/src/algorithms/compose/compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,37 @@ pub fn compose_with_config<
config: ComposeConfig,
) -> Result<F3> {
let mut ofst: F3 = match config.compose_filter {
ComposeFilterEnum::AutoFilter => ComposeFst::new_auto(fst1, fst2)?.compute()?,
ComposeFilterEnum::AutoFilter => ComposeFst::new_auto(fst1, fst2)?.compute_2(),
ComposeFilterEnum::NullFilter => ComposeFst::<
_,
NullComposeFilterBuilder<_, SortedMatcher<_, _>, SortedMatcher<_, _>>,
>::new(fst1, fst2)?
.compute()?,
.compute_2(),
ComposeFilterEnum::SequenceFilter => ComposeFst::<
_,
SequenceComposeFilterBuilder<_, SortedMatcher<_, _>, SortedMatcher<_, _>>,
>::new(fst1, fst2)?
.compute()?,
.compute_2(),
ComposeFilterEnum::AltSequenceFilter => ComposeFst::<
_,
AltSequenceComposeFilterBuilder<_, SortedMatcher<_, _>, SortedMatcher<_, _>>,
>::new(fst1, fst2)?
.compute()?,
.compute_2(),
ComposeFilterEnum::MatchFilter => ComposeFst::<
_,
MatchComposeFilterBuilder<_, SortedMatcher<_, _>, SortedMatcher<_, _>>,
>::new(fst1, fst2)?
.compute()?,
.compute_2(),
ComposeFilterEnum::NoMatchFilter => ComposeFst::<
_,
NoMatchComposeFilterBuilder<_, SortedMatcher<_, _>, SortedMatcher<_, _>>,
>::new(fst1, fst2)?
.compute()?,
.compute_2(),
ComposeFilterEnum::TrivialFilter => ComposeFst::<
_,
TrivialComposeFilterBuilder<_, SortedMatcher<_, _>, SortedMatcher<_, _>>,
>::new(fst1, fst2)?
.compute()?,
.compute_2(),
};

if config.connect {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ impl<W: Semiring, M1: Matcher<W>, M2: Matcher<W>> ComposeFilter<W>
self.s1 = s1;
self.s2 = s2;
self.fs = filter_state.clone();
// TODO: Could probably use unchecked here as the state should exist.
let fst2 = self.fst2();
let na2 = fst2.num_trs(self.s2)?;
let ne2 = fst2.num_input_epsilons(self.s2)?;
let fin2 = fst2.is_final(self.s2)?;
self.alleps2 = na2 == ne2 && !fin2;
self.noeps2 = ne2 == 0;
unsafe {
let fst2 = self.fst2();
let na2 = fst2.num_trs_unchecked(self.s2);
let ne2 = fst2.num_input_epsilons_unchecked(self.s2);
let fin2 = fst2.is_final_unchecked(self.s2);
self.alleps2 = na2 == ne2 && !fin2;
self.noeps2 = ne2 == 0;
}
}
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,25 @@ impl<W: Semiring, M1: Matcher<W>, M2: Matcher<W>> ComposeFilter<W>
self.s2 = s2;
self.fs = filter_state.clone();

let fst1 = self.fst1();
let fst2 = self.fst2();
unsafe {
let fst1 = self.fst1();
let fst2 = self.fst2();

let na1 = fst1.num_trs(s1)?;
let na2 = fst2.num_trs(s2)?;
let na1 = fst1.num_trs_unchecked(s1);
let na2 = fst2.num_trs_unchecked(s2);

let ne1 = fst1.num_output_epsilons(s1)?;
let ne2 = fst2.num_input_epsilons(s2)?;
let ne1 = fst1.num_output_epsilons_unchecked(s1);
let ne2 = fst2.num_input_epsilons_unchecked(s2);

let f1 = fst1.is_final(s1)?;
let f2 = fst2.is_final(s2)?;
let f1 = fst1.is_final_unchecked(s1);
let f2 = fst2.is_final_unchecked(s2);

self.alleps1 = na1 == ne1 && !f1;
self.alleps2 = na2 == ne2 && !f2;
self.alleps1 = na1 == ne1 && !f1;
self.alleps2 = na2 == ne2 && !f2;

self.noeps1 = ne1 == 0;
self.noeps2 = ne2 == 0;
self.noeps1 = ne1 == 0;
self.noeps2 = ne2 == 0;
}
}
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,14 @@ impl<W: Semiring, M1: Matcher<W>, M2: Matcher<W>> ComposeFilter<W>
self.s1 = s1;
self.s2 = s2;
self.fs = filter_state.clone();
// TODO: Could probably use unchecked here as the state should exist.
let fst1 = self.fst1();
let na1 = fst1.num_trs(self.s1)?;
let ne1 = fst1.num_output_epsilons(self.s1)?;
let fin1 = fst1.is_final(self.s1)?;
self.alleps1 = na1 == ne1 && !fin1;
self.noeps1 = ne1 == 0;
unsafe {
let fst1 = self.fst1();
let na1 = fst1.num_trs_unchecked(self.s1);
let ne1 = fst1.num_output_epsilons_unchecked(self.s1);
let fin1 = fst1.is_final_unchecked(self.s1);
self.alleps1 = na1 == ne1 && !fin1;
self.noeps1 = ne1 == 0;
}
}
Ok(())
}
Expand Down
24 changes: 24 additions & 0 deletions rustfst/src/algorithms/compose/compose_fst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::algorithms::compose::compose_filters::{
};
use crate::algorithms::compose::matchers::{GenericMatcher, Matcher};
use crate::algorithms::compose::{ComposeFstOp, ComposeFstOpOptions, ComposeStateTuple};
use crate::algorithms::lazy::cache::fst_cache::FillableFstCache;
use crate::algorithms::lazy::{FstCache, LazyFst, SimpleVecCache, StateTable};
use crate::fst_properties::FstProperties;
use crate::fst_traits::{
Expand Down Expand Up @@ -86,6 +87,13 @@ impl<W: Semiring, CFB: ComposeFilterBuilder<W>, Cache: FstCache<W>> ComposeFst<W
pub fn compute<F2: MutableFst<W> + AllocableFst<W>>(&self) -> Result<F2> {
self.0.compute()
}

pub fn compute_2<F2: MutableFst<W> + AllocableFst<W>>(self) -> F2
where
Cache: FillableFstCache<W>,
{
self.0.into_static_fst()
}
}

impl<W: Semiring, F1: ExpandedFst<W>, F2: ExpandedFst<W>>
Expand Down Expand Up @@ -129,6 +137,14 @@ where
self.0.num_trs_unchecked(s)
}

fn is_final(&self, state_id: usize) -> Result<bool> {
self.0.is_final(state_id)
}

unsafe fn is_final_unchecked(&self, state_id: usize) -> bool {
self.0.is_final_unchecked(state_id)
}

fn get_trs(&self, state_id: usize) -> Result<Self::TRS> {
self.0.get_trs(state_id)
}
Expand All @@ -145,9 +161,17 @@ where
self.0.num_input_epsilons(state)
}

unsafe fn num_input_epsilons_unchecked(&self, state: usize) -> usize {
self.0.num_input_epsilons_unchecked(state)
}

fn num_output_epsilons(&self, state: usize) -> Result<usize> {
self.0.num_output_epsilons(state)
}

unsafe fn num_output_epsilons_unchecked(&self, state: usize) -> usize {
self.0.num_output_epsilons_unchecked(state)
}
}

impl<'a, W, CFB, Cache> StateIterator<'a> for ComposeFst<W, CFB, Cache>
Expand Down
14 changes: 8 additions & 6 deletions rustfst/src/algorithms/compose/compose_fst_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ impl<W: Semiring, CFB: ComposeFilterBuilder<W>> ComposeFstOp<W, CFB> {
} else {
Tr::new(NO_LABEL, EPS_LABEL, W::one(), sb)
};
let mut trs = vec![];
let mut trs_vec = TrsVec(Arc::new(vec![]));
// Safe as there is no other Arc to the same allocation.
let trs = Arc::get_mut(&mut trs_vec.0).unwrap();

match selector {
Selector::Fst1Matcher2 => {
Expand All @@ -147,10 +149,10 @@ impl<W: Semiring, CFB: ComposeFilterBuilder<W>> ComposeFstOp<W, CFB> {
match_input,
&mut compose_filter,
selector,
&mut trs,
trs,
)?;
for tr in self.fst1.get_trs(sb)?.trs() {
self.match_tr(sa, tr, match_input, &mut compose_filter, selector, &mut trs)?;
self.match_tr(sa, tr, match_input, &mut compose_filter, selector, trs)?;
}
}
Selector::Fst2Matcher1 => {
Expand All @@ -160,14 +162,14 @@ impl<W: Semiring, CFB: ComposeFilterBuilder<W>> ComposeFstOp<W, CFB> {
match_input,
&mut compose_filter,
selector,
&mut trs,
trs,
)?;
for tr in self.fst2.get_trs(sb)?.trs() {
self.match_tr(sa, tr, match_input, &mut compose_filter, selector, &mut trs)?;
self.match_tr(sa, tr, match_input, &mut compose_filter, selector, trs)?;
}
}
}
Ok(TrsVec(Arc::new(trs)))
Ok(trs_vec)
}

fn add_tr(
Expand Down
Loading