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
//! Post-processing and cleanup utilities
//!
//! This module provides helper functions for cleaning and processing
//! document elements after initial parsing.
use super::models::*;
pub(crate) fn is_likely_sentence(text: &str) -> bool {
let text = text.trim();
// If it contains multiple sentences, it's probably not a heading
if text.matches(". ").count() > 1 {
return true;
}
// If it ends with common sentence endings and is long, it's probably a sentence
if text.len() > 80 && (text.ends_with('.') || text.ends_with('!') || text.ends_with('?')) {
return true;
}
// If it contains common sentence connectors, it's likely a sentence
if text.contains(" and ")
|| text.contains(" but ")
|| text.contains(" however ")
|| text.contains(" therefore ")
{
return true;
}
false
}
pub(crate) fn estimate_page_count(word_count: usize) -> usize {
// Rough estimate: 250 words per page
(word_count as f32 / 250.0).ceil() as usize
}
pub(crate) fn clean_word_list_markers(elements: Vec<DocumentElement>) -> Vec<DocumentElement> {
elements
.into_iter()
.map(|element| match element {
DocumentElement::Paragraph { runs } => {
let cleaned_runs = runs
.into_iter()
.map(|mut run| {
if run.text.starts_with("__WORD_LIST__") {
run.text = run
.text
.strip_prefix("__WORD_LIST__")
.unwrap_or(&run.text)
.to_string();
}
run
})
.collect();
DocumentElement::Paragraph { runs: cleaned_runs }
}
DocumentElement::List { items, ordered } => {
let cleaned_items = items
.into_iter()
.map(|item| {
let combined_text: String =
item.runs.iter().map(|run| run.text.as_str()).collect();
let cleaned_runs = if combined_text.starts_with("__WORD_LIST__") {
// Remove the __WORD_LIST__ prefix from the first run
let mut new_runs = item.runs.clone();
if let Some(first_run) = new_runs.first_mut() {
first_run.text = first_run
.text
.strip_prefix("__WORD_LIST__")
.unwrap_or(&first_run.text)
.to_string();
}
new_runs
} else {
item.runs.clone()
};
ListItem {
runs: cleaned_runs,
level: item.level,
}
})
.collect();
DocumentElement::List {
items: cleaned_items,
ordered,
}
}
other => other,
})
.collect()
}