Skip to content

Commit

Permalink
Add TrivialWeight
Browse files Browse the repository at this point in the history
  • Loading branch information
bluebear94 committed Sep 30, 2024
1 parent 3799b81 commit 550375e
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Added
- Binary serialization & deserialization support for FST caches.
- Binary serialization & deserialization support for Compose FST op state table.
- Add `TrivialWeight`

## [0.8.0] - 2020-16-10

Expand Down
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rustfst/src/semirings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod probability_weight;
mod product_weight;
mod string_variant;
mod string_weight;
mod trivial_weight;
mod tropical_weight;
mod union_weight;
pub(crate) mod utils_float;
Expand All @@ -33,5 +34,6 @@ pub(crate) use self::string_variant::StringWeightVariant;
pub use self::string_weight::{
StringType, StringWeightLeft, StringWeightRestrict, StringWeightRight,
};
pub use self::trivial_weight::TrivialWeight;
pub use self::tropical_weight::TropicalWeight;
pub use self::union_weight::{UnionWeight, UnionWeightOption};
105 changes: 105 additions & 0 deletions rustfst/src/semirings/trivial_weight.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use anyhow::Result;

use crate::semirings::{CompleteSemiring, ReverseBack, Semiring, SemiringProperties, StarSemiring};
use std::borrow::Borrow;

use super::WeaklyDivisibleSemiring;

/// Trivial semiring: (..., ..., (), ()).
///
/// This is useful for defining unweighted transducers.
#[derive(Clone, Debug, PartialEq, PartialOrd, Default, Eq, Copy, Hash)]
pub struct TrivialWeight;

impl Semiring for TrivialWeight {
type Type = ();

type ReverseWeight = TrivialWeight;

fn zero() -> Self {
Self
}

fn one() -> Self {
Self
}

fn new(_value: Self::Type) -> Self {
Self
}

fn plus_assign<P: Borrow<Self>>(&mut self, _rhs: P) -> anyhow::Result<()> {
Ok(())
}

fn times_assign<P: Borrow<Self>>(&mut self, _rhs: P) -> anyhow::Result<()> {
Ok(())
}

fn approx_equal<P: Borrow<Self>>(&self, _rhs: P, _delta: f32) -> bool {
true
}

fn value(&self) -> &Self::Type {
&()
}

fn take_value(self) -> Self::Type {}

fn set_value(&mut self, _value: Self::Type) {}

fn reverse(&self) -> anyhow::Result<Self::ReverseWeight> {
Ok(*self)
}

fn properties() -> SemiringProperties {
SemiringProperties::LEFT_SEMIRING
| SemiringProperties::RIGHT_SEMIRING
| SemiringProperties::COMMUTATIVE
| SemiringProperties::IDEMPOTENT
| SemiringProperties::PATH
}
}

impl ReverseBack<TrivialWeight> for TrivialWeight {
fn reverse_back(&self) -> Result<TrivialWeight> {
Ok(*self)
}
}

impl CompleteSemiring for TrivialWeight {}

impl StarSemiring for TrivialWeight {
fn closure(&self) -> Self {
Self
}
}

impl WeaklyDivisibleSemiring for TrivialWeight {
fn divide_assign(&mut self, _rhs: &Self, _divide_type: super::DivideType) -> Result<()> {
bail!("Division by 0")
}
}

impl From<()> for TrivialWeight {
fn from(_value: ()) -> Self {
Self
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_trivial_weight() -> Result<()> {
let b_trivial = TrivialWeight;

// Test plus
assert_eq!(b_trivial.plus(b_trivial)?, b_trivial);

// Test times
assert_eq!(b_trivial.times(b_trivial)?, b_trivial);
Ok(())
}
}

0 comments on commit 550375e

Please sign in to comment.