Struct galm::Database

source ·
pub struct Database {
    pub characters: Characters,
    pub max_distance_size: usize,
}
Expand description

Database

Fields§

§characters: Characters§max_distance_size: usize

Implementations§

source§

impl Database

Database

source

pub fn get_distance(&self, from: &str, to: &str) -> u8

Get the matching rate of character. Range is 0..galm::Database.max_distance_size. The more similar the two arguments, the smaller the return value.

// Initialize GalM Database instance.
let galm: galm::Database = galm::Database::default();

// Get the matching rate of character.
let distance: u8 = galm.get_distance("王", "玉");

assert_eq!(distance, 22);
source

pub fn get_word_distance(&self, str1: &str, str2: &str) -> usize

Get the matching rate of word. Range is 0..std::usize::MAX. The more similar the two arguments, the smaller the return value.

// Initialize GalM Database instance.
let galm: galm::Database = galm::Database::default();

let sort_key = "王様";
let mut vec = ["皇様", "玉様", "大様"];

// Sort Example
vec.sort_by_key( |candidate| galm.get_word_distance(sort_key, candidate) );

assert_eq!(vec, ["玉様", "大様", "皇様"]);
Examples found in repository?
examples/galm.rs (line 82)
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
fn main() {

    // 翻訳対象を読み込み
    let args: Vec<String> = std::env::args().collect();

    // --version もしくは --help が指定されていた場合は、他のオプションに関係なく出力
    // また、両方指定されている場合は、先に指定されていた方を優先して出力
    for arg in args.iter() {
        match &*arg.to_string() {

            // print version
            "-v" | "--version" => {
                println!("galm version {}", VERSIONSTR);
                return;
            },

            // print help
            "-h" | "--help" => {
                println!("{}", HELPSTR);
                return;
            },
            _ => {}
        }
    }

    match args.len() {
        1 => {
            // 引数が指定されていない場合は --help を出力
            println!("{}", HELPSTR);
            return;
        },
        2 => {

            use std::io::{self, Read};
            let stdin = io::stdin();
            let mut handle = stdin.lock();
            let mut buffer = String::new();
            let _ = handle.read_to_string(&mut buffer);

            // initialize galm
            let galm = galm::new();

            // sort
            let sort_key = &*args[1];
            let mut vec = buffer.lines().collect::<Vec<&str>>();
            vec.sort_by_cached_key( |candidate| galm.get_word_distance(sort_key, candidate) );

            // print
            print_vec(&vec);
            return;
        },
        _ => {}
    }

    // 各処理
    match &*args[2] {
        "-f" | "--file" => {
            if args.len() > 3 {

                use std::fs::File;
                use std::io::prelude::*;
                let mut file = File::open(&*args[3]).unwrap();
                let mut buffer = String::new();
                let _ = file.read_to_string(&mut buffer);

                // initialize galm
                let galm = galm::new();

                // sort
                let sort_key = &*args[1];
                let mut vec = buffer.lines().collect::<Vec<&str>>();
                vec.sort_by_cached_key( |candidate| galm.get_word_distance(sort_key, candidate) );

                // print
                print_vec(&vec);
            } else {
                println!(r"Error:
    filepath is not specified.");
            }
        },
        arg => {
            println!(r"Error:
    not exists option: `{}`
    most similar param name: `{}`
    try 'galm --help' for more information",
                arg,
                get_similar_param(arg));
        }
    }
}

Trait Implementations§

source§

impl Debug for Database

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Database

source§

fn default() -> Self

Initialize GalM Database instance.

let galm: galm::Database = galm::Database::default();

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.