-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_ngram_index.rs
66 lines (51 loc) · 1.91 KB
/
create_ngram_index.rs
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
34
35
36
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
use chrono::Local;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fs, path::Path};
use crate::create_ngram::NgramHashMap;
#[derive(Serialize, Deserialize, Debug)]
pub struct HashMapStruct {
pub ngram_map: NgramHashMap,
}
pub fn create_file_path_index(file_path: String, hash_map: NgramHashMap) {
println!("serialize save start:{}", Local::now());
let hash_map = HashMapStruct {
ngram_map: hash_map,
};
let serialized = serde_json::to_string(&hash_map).unwrap();
fs::write(file_path, serialized).unwrap();
}
pub fn serialize_ngram_hashmap_to_json_files(file_path: String, hash_map: NgramHashMap) {
for (key, value) in &hash_map {
let hash_map_struct = create_hash_map_struct(key.clone(), value.clone());
let create_dir_path = format!("{}{}", file_path, first_two_chars(key));
create_dir_if_not_exists(create_dir_path.clone()).unwrap();
let create_file_path =
format!("{}/{}{}", create_dir_path.clone(), key, ".json".to_string());
let serialized = serde_json::to_string(&hash_map_struct).unwrap();
fs::write(create_file_path.clone(), serialized).unwrap();
}
}
fn create_hash_map_struct(key: String, v: Vec<(i32, String, usize)>) -> HashMapStruct {
let mut hash_map: NgramHashMap = HashMap::new();
hash_map.insert(key.to_string(), v.clone());
let map = HashMapStruct {
ngram_map: hash_map,
};
map
}
fn create_dir_if_not_exists<P: AsRef<Path>>(path: P) -> std::io::Result<()> {
let path = path.as_ref();
if !path.exists() {
fs::create_dir_all(path)?;
}
Ok(())
}
fn first_two_chars(s: &str) -> String {
s.chars().take(2).collect()
}
pub fn get_index(file_path: String) -> NgramHashMap {
let data = fs::read_to_string(file_path).unwrap();
let deserialized: HashMapStruct = serde_json::from_str(&data).unwrap();
let map = deserialized.ngram_map;
map
}