Skip to content
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

Add TrivialWeight #285

Open
wants to merge 1 commit 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
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 trivial = TrivialWeight;

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

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