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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use libxml::readonly::RoNode;
use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;
#[derive(Debug)]
pub struct RuntimeParseData {
pub had_whitespace: bool,
pub chars: Vec<char>,
}
impl Default for RuntimeParseData {
fn default() -> RuntimeParseData {
RuntimeParseData {
had_whitespace: true, chars: Vec::new(),
}
}
}
#[derive(Clone)]
pub enum SpecialTagsOption {
Enter,
Normalize(String),
FunctionNormalize(Arc<fn(RoNode) -> String>),
Skip,
}
impl fmt::Debug for SpecialTagsOption {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use SpecialTagsOption::*;
write!(f, "SpecialTagsOption {{")?;
match self {
Enter => write!(f, "Enter")?,
Skip => write!(f, "Skip")?,
Normalize(v) => write!(f, "Normalize({v})")?,
FunctionNormalize(_) => write!(f, "FunctionNormalize")?,
};
write!(f, "}}")
}
}
#[derive(Debug, Clone)]
pub struct DNMParameters {
pub special_tag_name_options: HashMap<String, SpecialTagsOption>,
pub special_tag_class_options: HashMap<String, SpecialTagsOption>,
pub normalize_white_spaces: bool,
pub wrap_tokens: bool,
pub normalize_unicode: bool,
pub stem_words_once: bool,
pub stem_words_full: bool,
pub convert_to_lowercase: bool,
pub support_back_mapping: bool,
}
impl Default for DNMParameters {
fn default() -> DNMParameters {
DNMParameters {
special_tag_name_options: HashMap::new(),
special_tag_class_options: HashMap::new(),
normalize_white_spaces: true,
wrap_tokens: false,
normalize_unicode: false,
stem_words_once: false,
stem_words_full: false,
convert_to_lowercase: false,
support_back_mapping: true,
}
}
}
impl DNMParameters {
pub fn llamapun_normalization() -> DNMParameters {
let mut name_options = HashMap::new();
name_options.insert(
"math".to_string(),
SpecialTagsOption::Normalize("mathformula".to_string()),
);
name_options.insert(
"cite".to_string(),
SpecialTagsOption::Normalize("CitationElement".to_string()),
);
name_options.insert("img".to_string(), SpecialTagsOption::Skip);
name_options.insert("table".to_string(), SpecialTagsOption::Skip);
name_options.insert("head".to_string(), SpecialTagsOption::Skip);
name_options.insert("footer".to_string(), SpecialTagsOption::Skip);
let mut class_options = HashMap::new();
class_options.insert(
"ltx_equation".to_string(),
SpecialTagsOption::Normalize("\nmathformula\n".to_string()),
);
class_options.insert(
"ltx_equationgroup".to_string(),
SpecialTagsOption::Normalize("\nmathformula\n".to_string()),
);
class_options.insert(
"ltx_ref".to_string(),
SpecialTagsOption::Normalize("REF".to_string()),
);
class_options.insert("ltx_authors".to_string(), SpecialTagsOption::Skip);
class_options.insert("ltx_TOC".to_string(), SpecialTagsOption::Skip);
class_options.insert("ltx_note_mark".to_string(), SpecialTagsOption::Skip);
class_options.insert("ltx_note_outer".to_string(), SpecialTagsOption::Skip);
class_options.insert("ltx_bibliography".to_string(), SpecialTagsOption::Skip);
class_options.insert("ltx_tag_figure".to_string(), SpecialTagsOption::Skip);
class_options.insert("ltx_tag_table".to_string(), SpecialTagsOption::Skip);
DNMParameters {
special_tag_name_options: name_options,
special_tag_class_options: class_options,
normalize_white_spaces: false, wrap_tokens: true,
normalize_unicode: true,
..Default::default()
}
}
pub fn check(&self) {
if self.stem_words_once && self.stem_words_full {
dbg!(
"llamapun::dnm: Parameter options stem_words_once\
and stem_words_full are both set"
);
}
if (self.stem_words_once || self.stem_words_full) && self.convert_to_lowercase {
dbg!(
"llamapun::dnm: Parameter option convert_to_lowercase\
is redundant, because stemming converts to lowercase already"
);
}
if (self.stem_words_once || self.stem_words_full) && self.support_back_mapping {
dbg!(
"llamapun::dnm: Parameter option support_back_mapping\
does not work in combination with word stemming yet"
);
}
}
}