-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.rs
95 lines (80 loc) · 2.56 KB
/
bridge.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
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
// autocxx generates some stuff that makes clippy angry
#![allow(clippy::all)]
use ::std::cell::RefCell;
use ::std::mem::size_of;
use ::std::rc::Rc;
use autocxx::subclass::CppSubclassDefault;
use autocxx::{prelude::*, subclass::is_subclass};
include_cpp! {
#include "lm/virtual_interface.hh"
#include "lm/config.hh"
#include "lm/state.hh"
#include "util/mmap.hh"
#include "lm/enumerate_vocab.hh"
#include "util/string_piece.hh"
#include "lm/max_order.hh"
#include "lm/binary_format.hh"
#include "lm/facade.hh"
safety!(unsafe)
generate!("lm::ngram::SizeOfSanity")
generate!("lm::ngram::ModelMaxOrder")
generate_pod!("lm::ngram::FixedWidthParameters")
generate_pod!("lm::ngram::State")
generate!("util::LoadMethod")
generate!("lm::base::Model")
generate!("lm::base::Vocabulary")
generate!("lm::base::LoadVirtualPtr")
generate!("lm::ngram::Config")
generate!("lm::base::Config_Create")
generate!("lm::ngram::Config_set_load_method")
generate!("lm::ngram::Config_set_enumerate_callback")
generate!("lm::WordIndex")
generate!("StringPiece")
subclass!("lm::EnumerateVocab", VocabFetchCallback)
}
pub(crate) use ffi::*;
use lm::EnumerateVocab_methods;
use lm::WordIndex;
impl Clone for lm::ngram::State {
fn clone(&self) -> Self {
Self {
words: self.words,
backoff: self.backoff,
length: self.length,
}
}
}
#[is_subclass(superclass("EnumerateVocab"))]
#[derive(Default)]
pub struct VocabFetchCallback {
pub vocab: Vec<String>,
}
impl EnumerateVocab_methods for VocabFetchCallback {
fn Add(&mut self, index: WordIndex, string: &StringPiece) {
// make clippy happy
let _ = index;
let string = string
.as_string()
.as_ref()
// safety: this should ever only be none if the kenlm
// vocab contains a null ptr which means a bug
// over there. Since this is called from C++
// and kenlm dictates its signature no Result
// here either.
.unwrap()
.to_string();
self.vocab.push(string);
}
}
pub fn size_of_sanity_header() -> u64 {
u64::from(ffi::lm::ngram::SizeOfSanity())
}
pub fn get_max_order() -> u8 {
u8::from(ffi::lm::ngram::ModelMaxOrder())
}
pub fn get_size_of_fixed_width_params() -> usize {
size_of::<ffi::lm::ngram::FixedWidthParameters>()
}
pub fn get_vocab_call_back() -> Rc<RefCell<VocabFetchCallback>> {
VocabFetchCallback::default_rust_owned()
}