rustfst/
string_path.rs

1use crate::{FstPath, Label, Semiring, SymbolTable};
2use anyhow::{format_err, Result};
3use std::sync::Arc;
4
5/// Wrapper around `FstPath` to nicely handle `SymbolTable`s.
6#[derive(Debug, Clone)]
7pub struct StringPath<W: Semiring> {
8    fst_path: FstPath<W>,
9    isymt: Arc<SymbolTable>,
10    osymt: Arc<SymbolTable>,
11}
12
13impl<W: Semiring> StringPath<W> {
14    pub fn new(fst_path: FstPath<W>, isymt: Arc<SymbolTable>, osymt: Arc<SymbolTable>) -> Self {
15        Self {
16            fst_path,
17            isymt,
18            osymt,
19        }
20    }
21
22    pub fn weight(&self) -> &W {
23        &self.fst_path.weight
24    }
25
26    pub fn ilabels(&self) -> &[Label] {
27        self.fst_path.ilabels.as_slice()
28    }
29
30    pub fn olabels(&self) -> &[Label] {
31        self.fst_path.olabels.as_slice()
32    }
33
34    pub fn istring(&self) -> Result<String> {
35        let res: Result<Vec<_>> = self
36            .fst_path
37            .ilabels
38            .iter()
39            .map(|e| {
40                self.isymt
41                    .get_symbol(*e)
42                    .ok_or_else(|| format_err!("Missing {} in symbol table", e))
43                // .map_err()
44            })
45            .collect();
46        let res = res?.join(" ");
47        Ok(res)
48    }
49
50    pub fn ostring(&self) -> Result<String> {
51        let res: Result<Vec<_>> = self
52            .fst_path
53            .olabels
54            .iter()
55            .map(|e| {
56                self.osymt
57                    .get_symbol(*e)
58                    .ok_or_else(|| format_err!("Missing {} in symbol table", e))
59                // .map_err()
60            })
61            .collect();
62        let res = res?.join(" ");
63        Ok(res)
64    }
65}