pub struct Database {
pub characters: Characters,
pub max_distance_size: usize,
}Expand description
Database
Fields§
§characters: Characters§max_distance_size: usizeImplementations§
Source§impl Database
Database
impl Database
Database
Sourcepub fn get_distance(&self, from: &str, to: &str) -> u8
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);Sourcepub fn get_word_distance(&self, str1: &str, str2: &str) -> usize
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§
Auto Trait Implementations§
impl Freeze for Database
impl RefUnwindSafe for Database
impl Send for Database
impl Sync for Database
impl Unpin for Database
impl UnwindSafe for Database
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more