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

Implement WeaklyDivisibleSemiring for BooleanWeight #284

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.
- Implement `WeaklyDivisibleSemiring` for `BooleanWeight`

## [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.

11 changes: 11 additions & 0 deletions rustfst/src/semirings/boolean_weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use anyhow::Result;

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

use super::{DivideType, WeaklyDivisibleSemiring};
/// Boolean semiring: (&, |, false, true).
#[derive(Clone, Debug, PartialEq, PartialOrd, Default, Eq, Copy, Hash)]
pub struct BooleanWeight {
Expand Down Expand Up @@ -77,6 +79,15 @@ impl StarSemiring for BooleanWeight {
}
}

impl WeaklyDivisibleSemiring for BooleanWeight {
fn divide_assign(&mut self, rhs: &Self, _divide_type: DivideType) -> Result<()> {
if !rhs.value {
bail!("Division by 0")
}
Ok(()) // x / true == true
}
}

impl From<bool> for BooleanWeight {
fn from(b: bool) -> Self {
Self::new(b)
Expand Down