use regex::Regex;
use std::sync::LazyLock;
pub(crate) static CHAPTER_LABEL: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^(?i:chapter|part|appendix|lesson|unit)\s+(?:\d{1,3}|[IVXLC]{1,6}|[A-Z])[.:]?(?:\s|$)").unwrap()
});
pub(crate) static DIALOGUE_LINE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^(?:[\s*例\s*]\s*▶?\s*)?[A-ZA-Z]\s*[::]\s*\S").unwrap());
pub(crate) static HEADING_NUM: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^(?P<num>\d{1,2}(?:\.\d{1,2}){0,3})\.?\s+(?P<rest>\S.*)$").unwrap());
pub(crate) static HEADING_APP: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^(?P<word>Appendix\s+|APPENDIX\s+)?(?P<num>[A-Z](?:\.\d{1,2}){0,3})[.:]?\s+(?P<rest>\S.*)$").unwrap()
});
pub(crate) static HEADING_ROMAN: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^(?P<num>[IVX]{1,5})\.\s+(?P<rest>[A-Z].*)$").unwrap());
pub(crate) static HEADING_LETTER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^(?P<num>[A-Z])\.\s+(?P<rest>[A-Z].*)$").unwrap());
pub(crate) static INTRO_HEADING: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)^introduction$").unwrap());
pub(crate) static THEOREM_LABEL: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r"^(?:(?:\d+(?:\.\d+)*\.?\s+)?(?:Theorem|Lemma|Corollary|Proposition|Definition|Remark|Claim|Conjecture|Fact|Observation|Notation|Assumption|Hypothesis|Axiom|Property)\b(?:\s+[A-Z]?\d+(?:\.\d+)*[a-z]?)?\s*[.(]|(?:Theorem|Lemma|Corollary|Proposition|Definition|Remark|Claim|Conjecture|Example)\b(?:\s+[A-Z]?\d+(?:\.\d+)*[a-z]?)?\s*[.:(])",
)
.unwrap()
});
pub(crate) static NOTE_LABEL: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[A-Z]{2,5}:\s+\S").unwrap());
pub(crate) static ABSTRACT_INLINE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^(?i:abstract)\s*[—–:.\-]+\s*").unwrap());
pub(crate) static REF_HEADING: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r"(?i)^(?:\d+\.?\s+|[ivx]+\.\s+)?(references|bibliography|references and notes|literature cited|works cited)$",
)
.unwrap()
});
pub(crate) static REF_LABEL: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^(?:\[(?P<a>[^\]]{1,24})\]|(?P<b>\d{1,3})\.(?:\s|$))").unwrap());
pub(crate) static KNOWN_HEADINGS: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"(?i)^(abstract|references|bibliography|acknowledge?ments?|appendix|appendices|supplementary materials?|conclusions?|introduction|related work|contents|keywords|index terms|はじめに|まえがき|目次|索引|参考文献|あとがき|前言|序言|目录|lời nói đầu|lời giới thiệu|mục lục|tài liệu tham khảo)$").unwrap()
});
pub(crate) static CONTENTS_HEADING: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(?i)^(?:(?:table of )?contents|目次|目录|mục lục)$").unwrap());
pub(crate) static CONTENTS_ENTRY_END: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(?:(?:\.\s?){4,}\s*(?:\d{1,4}|[ivxlc]{1,6})?|\s(?:\d{1,4}|[ivxlc]{1,6}))$").unwrap());
#[cfg(test)]
#[allow(clippy::missing_docs_in_private_items)]
mod tests {
use super::*;
#[test]
fn heading_patterns() {
assert!(HEADING_NUM.is_match("3.2.1 Scaled Dot-Product Attention"));
assert!(HEADING_ROMAN.is_match("IV. EXPERIMENTS"));
assert!(REF_HEADING.is_match("References"));
assert!(REF_HEADING.is_match("7 References"));
assert!(KNOWN_HEADINGS.is_match("はじめに"));
assert!(KNOWN_HEADINGS.is_match("Mục lục"));
assert!(!REF_HEADING.is_match("References to prior work"));
assert!(ABSTRACT_INLINE.is_match("Abstract—We propose"));
}
#[test]
fn contents_entries() {
assert!(CONTENTS_HEADING.is_match("Table of Contents"));
assert!(CONTENTS_HEADING.is_match("Mục lục"));
assert!(!CONTENTS_HEADING.is_match("Contents of the box"));
assert!(CONTENTS_ENTRY_END.is_match("1.1 What is a program? . . . . . . . ."));
assert!(CONTENTS_ENTRY_END.is_match("0.1 How I wrote this book . . . . . vii"));
assert!(CONTENTS_ENTRY_END.is_match("2.4 Script mode.......... 12"));
assert!(CONTENTS_ENTRY_END.is_match("Preface v"));
assert!(CONTENTS_ENTRY_END.is_match("1 Exploratory data analysis 1"));
assert!(!CONTENTS_ENTRY_END.is_match("1 The way of the program"));
assert!(!CONTENTS_ENTRY_END.is_match("Preface"));
}
#[test]
fn theorem_labels() {
assert!(THEOREM_LABEL.is_match("8.3 Lemma. Suppose we have a solution"));
assert!(THEOREM_LABEL.is_match("9.3. Corollary. Under the same assumptions"));
assert!(THEOREM_LABEL.is_match("Theorem 2 (Main). Let G be"));
assert!(THEOREM_LABEL.is_match("Remark: this holds"));
assert!(!THEOREM_LABEL.is_match("3 Definitions and notation"));
assert!(!THEOREM_LABEL.is_match("5 Example Applications"));
assert!(!THEOREM_LABEL.is_match("3 Example: Variational Auto-Encoder"));
assert!(!THEOREM_LABEL.is_match("Lemmatization of words"));
}
#[test]
fn note_labels() {
assert!(NOTE_LABEL.is_match("CW: Section contains content related to bias"));
assert!(!NOTE_LABEL.is_match("Setup: the datasets"));
assert!(!NOTE_LABEL.is_match("RQ1: Does it work"));
}
}