Skip to content
New issue

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

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

Already on GitHub? # to your account

Add Optimize to CLI #219

Merged
merged 2 commits into from
Sep 15, 2022
Merged
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 rustfst-cli/src/cmds/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod determinize;
pub mod invert;
pub mod map;
pub mod minimize;
pub mod optimize;
pub mod project;
pub mod push;
pub mod reverse;
Expand Down
41 changes: 41 additions & 0 deletions rustfst-cli/src/cmds/optimize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use anyhow::Result;

use rustfst::prelude::*;

use crate::unary_fst_algorithm::UnaryFstAlgorithm;

pub struct OptimizeAlgorithm {
path_in: String,
path_out: String,
}

impl UnaryFstAlgorithm for OptimizeAlgorithm {
fn get_path_in(&self) -> &str {
self.path_in.as_str()
}

fn get_path_out(&self) -> &str {
self.path_out.as_str()
}

fn get_algorithm_name(&self) -> String {
"optimize".to_string()
}

fn run_algorithm(
&self,
mut fst: VectorFst<TropicalWeight>,
) -> Result<VectorFst<TropicalWeight>> {
optimize(&mut fst)?;
Ok(fst)
}
}

impl OptimizeAlgorithm {
pub fn new(path_in: &str, path_out: &str) -> Self {
Self {
path_in: path_in.to_string(),
path_out: path_out.to_string(),
}
}
}
10 changes: 10 additions & 0 deletions rustfst-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::cmds::determinize::DeterminizeAlgorithm;
use crate::cmds::invert::InvertAlgorithm;
use crate::cmds::map::MapAlgorithm;
use crate::cmds::minimize::MinimizeAlgorithm;
use crate::cmds::optimize::OptimizeAlgorithm;
use crate::cmds::project::ProjectFstAlgorithm;
use crate::cmds::push::PushAlgorithm;
use crate::cmds::reverse::ReverseAlgorithm;
Expand Down Expand Up @@ -88,6 +89,10 @@ fn main() {
let topsort_cmd = SubCommand::with_name("topsort").about("Topsort algorithm.");
app = app.subcommand(one_in_one_out_options(topsort_cmd));

// Optimize
let optimize_cmd = SubCommand::with_name("optimize").about("Optimize algorithm.");
app = app.subcommand(one_in_one_out_options(optimize_cmd));

// Reverse
let reverse_cmd = SubCommand::with_name("reverse").about("Reverse algorithm.");
app = app.subcommand(one_in_one_out_options(reverse_cmd));
Expand Down Expand Up @@ -208,6 +213,11 @@ fn handle(matches: clap::ArgMatches) -> Result<()> {
m.value_of("out.fst").unwrap(),
)
.run_cli_or_bench(m),
("optimize", Some(m)) => OptimizeAlgorithm::new(
m.value_of("in.fst").unwrap(),
m.value_of("out.fst").unwrap(),
)
.run_cli_or_bench(m),
("project", Some(m)) => ProjectFstAlgorithm::new(
m.value_of("in.fst").unwrap(),
m.is_present("project_output"),
Expand Down