Skip to content

Commit

Permalink
Add Optimize to CLI (#219)
Browse files Browse the repository at this point in the history
* Add optimize to CLI

* Fmt
  • Loading branch information
Garvys authored Sep 15, 2022
1 parent 331cea3 commit 3e97b42
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
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

0 comments on commit 3e97b42

Please sign in to comment.