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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use libxml::parser::XmlParseError;
use libxml::readonly::RoNode;
use libxml::tree::Document as XmlDoc;
use libxml::xpath::Context;
use super::corpus::Corpus;
use super::{DNMRangeIterator, RoNodeIterator};
use crate::dnm::DNM;
pub struct Document<'d> {
pub dom: XmlDoc,
pub path: String,
pub corpus: &'d Corpus,
pub dnm: Option<DNM>,
}
impl<'d> Document<'d> {
pub fn new(filepath: String, corpus: &'d Corpus) -> Result<Self, XmlParseError> {
let dom = if filepath.ends_with(".xhtml") {
corpus.xml_parser.parse_file(&filepath)?
} else {
corpus.html_parser.parse_file(&filepath)?
};
Ok(Document {
path: filepath,
dom,
corpus,
dnm: None,
})
}
pub fn get_heading_nodes(&self) -> Vec<RoNode> { Document::heading_nodes(&self.dom) }
fn heading_nodes(doc: &XmlDoc) -> Vec<RoNode> {
Document::xpath_nodes(doc,
"//*[contains(@class,'ltx_title') and not(contains(@class,'ltx_title_document')) and (local-name()='h1' or local-name()='h2' or local-name()='h3' or local-name()='h4' or local-name()='h5' or local-name()='h6') and not(descendant::*[contains(@class,'ltx_ERROR')])]",
)
}
pub fn heading_iter(&self) -> RoNodeIterator {
RoNodeIterator {
walker: Document::heading_nodes(&self.dom).into_iter(),
document: self,
}
}
pub fn get_paragraph_nodes(&self) -> Vec<RoNode> { Document::paragraph_nodes(&self.dom) }
fn paragraph_nodes(doc: &XmlDoc) -> Vec<RoNode> {
Document::xpath_nodes(doc,
"//*[local-name()='div' and contains(@class,'ltx_para') and not(descendant::*[contains(@class,'ltx_ERROR')]) and not(preceding-sibling::*[contains(@class,'ltx_ERROR')])]",
)
}
pub fn paragraph_iter(&self) -> RoNodeIterator {
RoNodeIterator {
walker: Document::paragraph_nodes(&self.dom).into_iter(),
document: self,
}
}
fn abstract_p_node(doc: &XmlDoc) -> Option<RoNode> {
Document::xpath_node(doc,
"//*[local-name()='div' and contains(@class,'ltx_abstract') and not(descendant::*[contains(@class,'ltx_ERROR')])]//*[local-name()='p' and contains(@class,'ltx_p')][1]")
}
fn keywords_node(doc: &XmlDoc) -> Option<RoNode> {
Document::xpath_node(doc,
"//*[local-name()='div' and contains(@class,'ltx_keywords') and not(descendant::*[contains(@class,'ltx_ERROR')])]")
}
fn acknowledgement_node(doc: &XmlDoc) -> Option<RoNode> {
Document::xpath_node(doc,
"//*[local-name()='div' and contains(@class,'ltx_acknowledgement') and not(descendant::*[contains(@class,'ltx_ERROR')])]")
}
fn caption_nodes(doc: &XmlDoc) -> Vec<RoNode> {
Document::xpath_nodes(doc,
"//*[local-name()='figcaption' and contains(@class,'ltx_caption') and not(descendant::*[contains(@class,'ltx_ERROR')])]")
}
pub fn extended_paragraph_iter(&self) -> RoNodeIterator {
let mut paras = Vec::new();
if let Some(anode) = Document::abstract_p_node(&self.dom) {
paras.push(anode);
}
if let Some(keywords) = Document::keywords_node(&self.dom) {
paras.push(keywords);
}
paras.extend(Document::paragraph_nodes(&self.dom));
if let Some(anode) = Document::acknowledgement_node(&self.dom) {
paras.push(anode);
}
paras.extend(Document::caption_nodes(&self.dom));
RoNodeIterator {
walker: paras.into_iter(),
document: self,
}
}
pub fn get_math_nodes(&self) -> Vec<RoNode> { Document::math_nodes(&self.dom) }
pub(crate) fn math_nodes(doc: &XmlDoc) -> Vec<RoNode> {
Document::xpath_nodes(doc, "//*[local-name()='math']")
}
pub fn get_ref_nodes(&self) -> Vec<RoNode> { Document::ref_nodes(&self.dom) }
pub(crate) fn ref_nodes(doc: &XmlDoc) -> Vec<RoNode> {
Document::xpath_nodes(doc,
"//*[(local-name()='span' or local-name()='a') and (contains(@class,'ltx_ref ') or @class='ltx_ref')]")
}
pub fn sentence_iter(&mut self) -> DNMRangeIterator {
if self.dnm.is_none() {
if let Some(root) = self.dom.get_root_readonly() {
self.dnm = Some(DNM::new(root, self.corpus.dnm_parameters.clone()));
}
}
let tokenizer = &self.corpus.tokenizer;
let sentences = tokenizer.sentences(self.dnm.as_ref().unwrap());
DNMRangeIterator {
walker: sentences.into_iter(),
document: self,
}
}
pub fn get_xpath_nodes(&self, xpath_str: &str) -> Vec<RoNode> {
Document::xpath_nodes(&self.dom, xpath_str)
}
pub(crate) fn xpath_nodes(doc: &XmlDoc, xpath_str: &str) -> Vec<RoNode> {
let xpath_context = Context::new(doc).unwrap();
match xpath_context.evaluate(xpath_str) {
Ok(found_payload) => found_payload.get_readonly_nodes_as_vec(),
_ => Vec::new(),
}
}
pub fn get_xpath_node(&self, xpath_str: &str) -> Option<RoNode> {
Document::xpath_node(&self.dom, xpath_str)
}
pub(crate) fn xpath_node(doc: &XmlDoc, xpath_str: &str) -> Option<RoNode> {
let xpath_context = Context::new(doc).unwrap();
match xpath_context.evaluate(xpath_str) {
Ok(found_payload) => {
let mut vec_nodes = found_payload.get_readonly_nodes_as_vec();
if !vec_nodes.is_empty() {
Some(vec_nodes.remove(0))
} else {
None
}
},
_ => None,
}
}
pub fn xpath_selector_iter(&self, xpath_str: &str) -> RoNodeIterator {
RoNodeIterator {
walker: Document::xpath_nodes(&self.dom, xpath_str).into_iter(),
document: self,
}
}
pub(crate) fn dfs_filter_nodes(node: RoNode, filter: &dyn Fn(&RoNode) -> bool) -> Vec<RoNode> {
let mut found = Vec::new();
if filter(&node) {
found.push(node);
}
for child in node.get_child_nodes().into_iter() {
found.extend(Document::dfs_filter_nodes(child, filter));
}
found
}
pub fn filter_iter(&self, filter: &dyn Fn(&RoNode) -> bool) -> RoNodeIterator {
RoNodeIterator {
walker: Document::dfs_filter_nodes(self.dom.get_root_readonly().unwrap(), filter).into_iter(),
document: self,
}
}
}