Database

Struct 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 75)
33fn main() {
34    // 翻訳対象を読み込み
35    let args: Vec<String> = std::env::args().collect();
36
37    // --version もしくは --help が指定されていた場合は、他のオプションに関係なく出力
38    // また、両方指定されている場合は、先に指定されていた方を優先して出力
39    for arg in args.iter() {
40        match &*arg.to_string() {
41            // print version
42            "-v" | "--version" => {
43                println!("galm version {}", VERSIONSTR);
44                return;
45            }
46
47            // print help
48            "-h" | "--help" => {
49                println!("{}", HELPSTR);
50                return;
51            }
52            _ => {}
53        }
54    }
55
56    match args.len() {
57        1 => {
58            // 引数が指定されていない場合は --help を出力
59            println!("{}", HELPSTR);
60            return;
61        }
62        2 => {
63            use std::io::{self, Read};
64            let stdin = io::stdin();
65            let mut handle = stdin.lock();
66            let mut buffer = String::new();
67            let _ = handle.read_to_string(&mut buffer);
68
69            // initialize galm
70            let galm = galm::new();
71
72            // sort
73            let sort_key = &*args[1];
74            let mut vec = buffer.lines().collect::<Vec<&str>>();
75            vec.sort_by_cached_key(|candidate| galm.get_word_distance(sort_key, candidate));
76
77            // print
78            print_vec(&vec);
79            return;
80        }
81        _ => {}
82    }
83
84    // 各処理
85    match &*args[2] {
86        "-f" | "--file" => {
87            if args.len() > 3 {
88                use std::fs::File;
89                use std::io::prelude::*;
90                let mut file = File::open(&*args[3]).unwrap();
91                let mut buffer = String::new();
92                let _ = file.read_to_string(&mut buffer);
93
94                // initialize galm
95                let galm = galm::new();
96
97                // sort
98                let sort_key = &*args[1];
99                let mut vec = buffer.lines().collect::<Vec<&str>>();
100                vec.sort_by_cached_key(|candidate| galm.get_word_distance(sort_key, candidate));
101
102                // print
103                print_vec(&vec);
104            } else {
105                println!(
106                    r"Error:
107    filepath is not specified."
108                );
109            }
110        }
111        arg => {
112            println!(
113                r"Error:
114    not exists option: `{}`
115    most similar param name: `{}`
116    try 'galm --help' for more information",
117                arg,
118                get_similar_param(arg)
119            );
120        }
121    }
122}

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 T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.