1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use std::io::{self, BufReader};
use std::fs::File;
use std::path::Path;
use util::*;


/// 形態素の連接コスト表を扱う
pub struct Matrix {
    left_size: i32,
    #[allow(dead_code)]
    right_size: i32,
    matrix: Box<[i16]>
}

impl Matrix {
    pub fn new(data_dir: &Path) -> io::Result<Matrix> {
        let path = data_dir.join("matrix.bin");
        let mut reader = BufReader::new(File::open(&path)?);
        let left_size = reader.get_int()?;
        let right_size = reader.get_int()?;

        Ok(Matrix {
            left_size: left_size,
            right_size: right_size,
            matrix: reader.get_short_array((left_size * right_size) as usize)?
        })
    }

    /// 形態素同士の連接コストを求める
    pub fn link_cost(&self, left_id: i16, right_id: i16) -> i32 {
        self.matrix[(right_id as usize) * (self.left_size as usize) + (left_id as usize)] as i32
    }
}