Skip to main content

graphrag_core/text/parsers/
plaintext.rs

1//! Plain text layout parser with heuristic-based detection
2
3use crate::text::{
4    document_structure::{DocumentStructure, Heading, HeadingHierarchy, Section},
5    layout_parser::LayoutParser,
6    TextAnalyzer,
7};
8
9/// Parser for plain text documents using heuristics
10pub struct PlainTextLayoutParser;
11
12impl PlainTextLayoutParser {
13    /// Create new plain text parser
14    pub fn new() -> Self {
15        Self
16    }
17
18    /// Build sections from headings
19    fn build_sections(&self, headings: &[Heading], content: &str) -> Vec<Section> {
20        let mut sections = Vec::new();
21
22        for (i, heading) in headings.iter().enumerate() {
23            let content_start = heading.end_offset;
24            let content_end = headings
25                .get(i + 1)
26                .map(|h| h.start_offset)
27                .unwrap_or(content.len());
28
29            sections.push(Section::new(heading.clone(), content_start, content_end));
30        }
31
32        sections
33    }
34
35    /// Build hierarchy from sections
36    fn build_hierarchy(&self, sections: &mut [Section]) -> HeadingHierarchy {
37        let mut hierarchy = HeadingHierarchy::new();
38        let mut stack: Vec<usize> = Vec::new();
39
40        for idx in 0..sections.len() {
41            let section_level = sections[idx].heading.level;
42
43            while let Some(&parent_idx) = stack.last() {
44                if sections[parent_idx].heading.level < section_level {
45                    break;
46                }
47                stack.pop();
48            }
49
50            if let Some(&parent_idx) = stack.last() {
51                sections[parent_idx].child_sections.push(idx);
52                sections[idx].parent_section = Some(parent_idx);
53            } else {
54                hierarchy.root_sections.push(idx);
55            }
56
57            stack.push(idx);
58        }
59
60        // Build depth map
61        for (idx, section) in sections.iter().enumerate() {
62            let mut depth = 0;
63            let mut current = section.parent_section;
64            while let Some(parent_idx) = current {
65                depth += 1;
66                current = sections[parent_idx].parent_section;
67            }
68            hierarchy.depth_map.insert(idx, depth);
69        }
70
71        hierarchy
72    }
73}
74
75impl Default for PlainTextLayoutParser {
76    fn default() -> Self {
77        Self::new()
78    }
79}
80
81impl LayoutParser for PlainTextLayoutParser {
82    fn parse(&self, content: &str) -> DocumentStructure {
83        let mut headings = Vec::new();
84        let lines: Vec<&str> = content.lines().collect();
85        let mut current_offset = 0;
86
87        let mut i = 0;
88        while i < lines.len() {
89            let line = lines[i];
90            let trimmed = line.trim();
91
92            // Skip empty lines
93            if trimmed.is_empty() {
94                current_offset += line.len() + 1;
95                i += 1;
96                continue;
97            }
98
99            let mut detected_level: Option<u8> = None;
100            let heading_text = trimmed.to_string();
101
102            // Heuristic 1: Check if next line is underline
103            if i + 1 < lines.len() {
104                let next_line = lines[i + 1].trim();
105                if let Some(level) = TextAnalyzer::is_underline(next_line) {
106                    detected_level = Some(level);
107                    // Skip the underline in next iteration
108                    i += 1;
109                    current_offset += line.len() + 1;
110                    current_offset += next_line.len() + 1;
111                }
112            }
113
114            // Heuristic 2: ALL CAPS detection
115            if detected_level.is_none() {
116                if let Some(level) = TextAnalyzer::detect_heading_level(line) {
117                    detected_level = Some(level);
118                }
119            }
120
121            // If heading detected, add it
122            if let Some(level) = detected_level {
123                let heading = Heading::new(level, heading_text, current_offset, current_offset + line.len())
124                    .with_line_number(i);
125
126                headings.push(heading);
127            }
128
129            if detected_level.is_none() {
130                current_offset += line.len() + 1;
131            }
132
133            i += 1;
134        }
135
136        let mut sections = self.build_sections(&headings, content);
137        let hierarchy = self.build_hierarchy(&mut sections);
138
139        DocumentStructure {
140            headings,
141            sections,
142            hierarchy,
143        }
144    }
145
146    fn supports_format(&self, format: &str) -> bool {
147        matches!(format.to_lowercase().as_str(), "text" | "txt" | "plain")
148    }
149
150    fn name(&self) -> &'static str {
151        "PlainTextLayoutParser"
152    }
153}
154
155#[cfg(test)]
156mod tests {
157    use super::*;
158
159    #[test]
160    fn test_underline_detection() {
161        let parser = PlainTextLayoutParser::new();
162        let content = "Chapter One\n===========\n\nSome text\n\nSection 1.1\n-----------\n\nMore text";
163
164        let structure = parser.parse(content);
165
166        assert!(structure.headings.len() >= 2);
167        assert_eq!(structure.headings[0].level, 1);
168        assert_eq!(structure.headings[0].text, "Chapter One");
169    }
170
171    #[test]
172    fn test_all_caps_detection() {
173        let parser = PlainTextLayoutParser::new();
174        let content = "INTRODUCTION\n\nThis is the intro.\n\nBACKGROUND\n\nSome background.";
175
176        let structure = parser.parse(content);
177
178        assert!(structure.headings.len() >= 2);
179        assert!(structure.headings[0].text.contains("INTRODUCTION"));
180    }
181
182    #[test]
183    fn test_numbered_sections() {
184        let parser = PlainTextLayoutParser::new();
185        let content = "1. First Chapter\n\nText here.\n\n1.1 Subsection\n\nMore text.";
186
187        let structure = parser.parse(content);
188
189        // Should detect numbered headings
190        assert!(!structure.headings.is_empty());
191    }
192}