lindera_dictionary/loader/
prefix_dictionary.rs

1use std::path::Path;
2
3use crate::LinderaResult;
4#[cfg(feature = "compress")]
5use crate::decompress::decompress;
6use crate::dictionary::prefix_dictionary::PrefixDictionary;
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 PrefixDictionaryLoader {}
14
15impl PrefixDictionaryLoader {
16    #[allow(unused_mut)]
17    pub fn load(input_dir: &Path) -> LinderaResult<PrefixDictionary> {
18        let mut da_data = read_file(input_dir.join("dict.da").as_path())?;
19        let mut vals_data = read_file(input_dir.join("dict.vals").as_path())?;
20        let mut words_idx_data = read_file(input_dir.join("dict.wordsidx").as_path())?;
21        let mut words_data = read_file(input_dir.join("dict.words").as_path())?;
22
23        #[cfg(feature = "compress")]
24        {
25            let (compressed_data, _) =
26                bincode::serde::decode_from_slice(da_data.as_slice(), bincode::config::legacy())
27                    .map_err(|err| {
28                        LinderaErrorKind::Deserialize
29                            .with_error(anyhow::anyhow!(err))
30                            .add_context("Failed to deserialize dict.da data")
31                    })?;
32            da_data = decompress(compressed_data).map_err(|err| {
33                LinderaErrorKind::Compression
34                    .with_error(err)
35                    .add_context("Failed to decompress dict.da DoubleArray data")
36            })?;
37        }
38        #[cfg(feature = "compress")]
39        {
40            let (compressed_data, _) =
41                bincode::serde::decode_from_slice(vals_data.as_slice(), bincode::config::legacy())
42                    .map_err(|err| {
43                        LinderaErrorKind::Deserialize
44                            .with_error(anyhow::anyhow!(err))
45                            .add_context("Failed to deserialize dict.vals data")
46                    })?;
47            vals_data = decompress(compressed_data).map_err(|err| {
48                LinderaErrorKind::Compression
49                    .with_error(err)
50                    .add_context("Failed to decompress dict.vals word values data")
51            })?;
52        }
53        #[cfg(feature = "compress")]
54        {
55            let (compressed_data, _) = bincode::serde::decode_from_slice(
56                words_idx_data.as_slice(),
57                bincode::config::legacy(),
58            )
59            .map_err(|err| {
60                LinderaErrorKind::Deserialize
61                    .with_error(anyhow::anyhow!(err))
62                    .add_context("Failed to deserialize dict.wordsidx data")
63            })?;
64            words_idx_data = decompress(compressed_data).map_err(|err| {
65                LinderaErrorKind::Compression
66                    .with_error(err)
67                    .add_context("Failed to decompress dict.wordsidx word index data")
68            })?;
69        }
70        #[cfg(feature = "compress")]
71        {
72            let (compressed_data, _) =
73                bincode::serde::decode_from_slice(words_data.as_slice(), bincode::config::legacy())
74                    .map_err(|err| {
75                        LinderaErrorKind::Deserialize
76                            .with_error(anyhow::anyhow!(err))
77                            .add_context("Failed to deserialize dict.words data")
78                    })?;
79            words_data = decompress(compressed_data).map_err(|err| {
80                LinderaErrorKind::Compression
81                    .with_error(err)
82                    .add_context("Failed to decompress dict.words word details data")
83            })?;
84        }
85
86        Ok(PrefixDictionary::load(
87            da_data,
88            vals_data,
89            words_idx_data,
90            words_data,
91            true,
92        ))
93    }
94
95    #[cfg(feature = "mmap")]
96    pub fn load_mmap(input_dir: &Path) -> LinderaResult<PrefixDictionary> {
97        let da_data = mmap_file(input_dir.join("dict.da").as_path())?;
98        let vals_data = mmap_file(input_dir.join("dict.vals").as_path())?;
99        let words_idx_data = mmap_file(input_dir.join("dict.wordsidx").as_path())?;
100        let words_data = mmap_file(input_dir.join("dict.words").as_path())?;
101
102        Ok(PrefixDictionary::load(
103            da_data,
104            vals_data,
105            words_idx_data,
106            words_data,
107            true,
108        ))
109    }
110}