lindera_dictionary/dictionary_loader/
connection_cost_matrix.rs

1use std::path::Path;
2
3use crate::LinderaResult;
4#[cfg(feature = "compress")]
5use crate::decompress::decompress;
6use crate::dictionary::connection_cost_matrix::ConnectionCostMatrix;
7#[cfg(feature = "compress")]
8use crate::error::LinderaErrorKind;
9#[cfg(feature = "mmap")]
10use crate::util::mmap_file;
11use crate::util::read_file;
12
13pub struct ConnectionCostMatrixLoader {}
14
15impl ConnectionCostMatrixLoader {
16    #[allow(unused_mut)]
17    pub fn load(input_dir: &Path) -> LinderaResult<ConnectionCostMatrix> {
18        let mut data = read_file(input_dir.join("matrix.mtx").as_path())?;
19
20        #[cfg(feature = "compress")]
21        {
22            let (compressed_data, _) =
23                bincode::serde::decode_from_slice(data.as_slice(), bincode::config::legacy())
24                    .map_err(|err| {
25                        LinderaErrorKind::Deserialize
26                            .with_error(anyhow::anyhow!(err))
27                            .add_context("Failed to deserialize matrix.mtx data")
28                    })?;
29            data = decompress(compressed_data).map_err(|err| {
30                LinderaErrorKind::Compression
31                    .with_error(err)
32                    .add_context("Failed to decompress connection cost matrix data")
33            })?;
34        }
35
36        Ok(ConnectionCostMatrix::load(data))
37    }
38
39    #[cfg(feature = "mmap")]
40    pub fn load_mmap(input_dir: &Path) -> LinderaResult<ConnectionCostMatrix> {
41        let data = mmap_file(input_dir.join("matrix.mtx").as_path())?;
42
43        Ok(ConnectionCostMatrix::load(data))
44    }
45}