lindera_dictionary/builder/
connection_cost_matrix.rs1use std::borrow::Cow;
2use std::fs::File;
3use std::io::{self, Write};
4use std::path::Path;
5use std::str::FromStr;
6
7use byteorder::{LittleEndian, WriteBytesExt};
8use derive_builder::Builder;
9use log::debug;
10
11use crate::LinderaResult;
12use crate::decompress::Algorithm;
13use crate::error::LinderaErrorKind;
14use crate::util::{compress_write, read_file_with_encoding};
15
16#[derive(Builder, Debug)]
17#[builder(name = ConnectionCostMatrixBuilderOptions)]
18#[builder(build_fn(name = "builder"))]
19pub struct ConnectionCostMatrixBuilder {
20 #[builder(default = "\"UTF-8\".into()", setter(into))]
21 encoding: Cow<'static, str>,
22 #[builder(default = "Algorithm::Deflate")]
23 compress_algorithm: Algorithm,
24}
25
26impl ConnectionCostMatrixBuilder {
27 pub fn build(&self, input_dir: &Path, output_dir: &Path) -> LinderaResult<()> {
28 let matrix_data_path = input_dir.join("matrix.def");
29 debug!("reading {matrix_data_path:?}");
30 let matrix_data = read_file_with_encoding(&matrix_data_path, &self.encoding)?;
31
32 let mut lines = Vec::new();
33 for line in matrix_data.lines() {
34 let fields: Vec<i32> = line
35 .split_whitespace()
36 .map(i32::from_str)
37 .collect::<Result<_, _>>()
38 .map_err(|err| LinderaErrorKind::Parse.with_error(anyhow::anyhow!(err)))?;
39 lines.push(fields);
40 }
41 let mut lines_it = lines.into_iter();
42 let header = lines_it.next().ok_or_else(|| {
43 LinderaErrorKind::Content.with_error(anyhow::anyhow!("unknown error"))
44 })?;
45 let forward_size = header[0] as u32;
46 let backward_size = header[1] as u32;
47 let len = 3 + (forward_size * backward_size) as usize;
48 let mut costs = vec![i16::MAX; len];
49 costs[0] = -1; costs[1] = forward_size as i16;
51 costs[2] = backward_size as i16;
52 for fields in lines_it {
53 let forward_id = fields[0] as u32;
54 let backward_id = fields[1] as u32;
55 let cost = fields[2] as u16;
56 costs[3 + (forward_id + backward_id * forward_size) as usize] = cost as i16;
57 }
58
59 let wtr_matrix_mtx_path = output_dir.join(Path::new("matrix.mtx"));
60 let mut wtr_matrix_mtx = io::BufWriter::new(
61 File::create(wtr_matrix_mtx_path)
62 .map_err(|err| LinderaErrorKind::Io.with_error(anyhow::anyhow!(err)))?,
63 );
64 let mut matrix_mtx_buffer = Vec::new();
65 for cost in costs {
66 matrix_mtx_buffer
67 .write_i16::<LittleEndian>(cost)
68 .map_err(|err| LinderaErrorKind::Io.with_error(anyhow::anyhow!(err)))?;
69 }
70
71 compress_write(
72 &matrix_mtx_buffer,
73 self.compress_algorithm,
74 &mut wtr_matrix_mtx,
75 )?;
76
77 wtr_matrix_mtx
78 .flush()
79 .map_err(|err| LinderaErrorKind::Io.with_error(anyhow::anyhow!(err)))?;
80
81 Ok(())
82 }
83}