// Copyright (C) 2022 tslil clingman
//
// This file is part of srchr.
//
// srchr is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// srchr is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// srchr. If not, see .
use std::fs;
use crate::config::*;
extern crate json;
const fn build_lookup_table() -> [usize; 128] {
let mut result = [0; 128];
let mut i = 0;
while i < NUM_KEYS {
result[KEY_CHARS[i] as usize] = i;
i += 1;
}
return result;
}
const CHAR_TO_INDEX: [usize; 128] = build_lookup_table();
const NUM_BIGRAMS: usize = NUM_KEYS * NUM_KEYS;
pub struct Corpus {
bigram_percs: [f32; NUM_BIGRAMS],
skipgram_percs: [f32; NUM_BIGRAMS],
character_percs: [f32; 128],
}
fn pair_to_index(x: u8, y: u8) -> usize {
let xi = CHAR_TO_INDEX[x as usize];
let yi = CHAR_TO_INDEX[y as usize];
xi + NUM_KEYS * yi
}
impl Corpus {
pub fn get_character_perc(&self, c: u8) -> f32 {
self.character_percs[c as usize]
}
pub fn get_bigram_perc(&self, x: u8, y: u8) -> f32 {
self.bigram_percs[pair_to_index(x, y)]
}
pub fn get_skipgram_perc(&self, x: u8, y: u8) -> f32 {
self.skipgram_percs[pair_to_index(x, y)]
}
pub fn load_from_json_file(path: &str) -> Result {
let contents = fs::read_to_string(path)?;
let json = json::parse(&contents).unwrap();
let mut character_percs = [0.0; 128];
let mut bigram_percs = [0.0; NUM_BIGRAMS];
let mut skipgram_percs = [0.0; NUM_BIGRAMS];
for (key, value) in json.entries() {
if key == "characters" {
for (c, p) in value.entries() {
if let Some(c) = canonicalise(c.as_bytes()[0] as char) {
character_percs[c as usize] = p.as_f32().unwrap();
}
}
} else if key == "bigrams" {
for (b, p) in value.entries() {
let canon: Vec