File size: 12,164 Bytes
e05ae01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
"""
Enhanced Document Processor - Ensure complete document generation with all sections
Fixes the issue where only Title, TOC, Executive Summary, and References were appearing
"""

import logging
from typing import Dict, List, Optional, Tuple, Any
from datetime import datetime

logger = logging.getLogger(__name__)


class EnhancedDocumentProcessor:
    """
    Process and assemble complete documents ensuring all sections are included.
    Validates that generated documents contain all expected sections before output.
    """

    def __init__(self):
        """Initialize enhanced document processor."""
        self.required_sections_by_type = {
            "research": [
                "Introduction",
                "Literature Review",
                "Methodology",
                "Results",
                "Discussion",
                "Conclusion",
            ],
            "essay": [
                "Introduction",
                "Body",
                "Analysis",
                "Conclusion",
            ],
            "report": [
                "Executive Summary",
                "Introduction",
                "Findings",
                "Analysis",
                "Recommendations",
                "Conclusion",
            ],
            "lab": [
                "Objective",
                "Procedure",
                "Results",
                "Analysis",
                "Conclusion",
            ],
            "thesis": [
                "Introduction",
                "Literature Review",
                "Methodology",
                "Results",
                "Discussion",
                "Implications",
                "Conclusion",
            ],
        }

    def assemble_complete_document(
        self,
        title: str,
        content_sections: Dict[str, str],
        author: str = "AI Academic Suite",
        document_type: str = "research",
        include_toc: bool = True,
        include_citations: bool = False,
        citations: Optional[List[str]] = None,
    ) -> Tuple[Dict[str, str], List[str]]:
        """
        Assemble complete document with all sections and metadata.

        Args:
            title: Document title
            content_sections: Dictionary of section_name -> content
            author: Document author
            document_type: Type of document
            include_toc: Include table of contents
            include_citations: Include bibliography
            citations: List of citations

        Returns:
            Tuple of (complete_document_dict, validation_messages)
        """
        validation_messages = []

        # Validate content sections
        validation_messages.extend(self._validate_sections(content_sections, document_type))

        # Ensure all required sections are present
        complete_sections = self._ensure_all_sections_present(
            content_sections, document_type, title
        )

        # Build complete document with proper ordering
        ordered_document = self._create_ordered_document(
            title=title,
            sections=complete_sections,
            author=author,
            include_toc=include_toc,
            include_citations=include_citations,
            citations=citations,
        )

        # Final validation
        validation_messages.extend(self._validate_final_document(ordered_document))

        return ordered_document, validation_messages

    def _validate_sections(
        self, sections: Dict[str, str], document_type: str
    ) -> List[str]:
        """Validate that sections have appropriate content."""
        messages = []

        for section_name, content in sections.items():
            if not content or len(content.strip()) < 50:
                messages.append(
                    f"⚠️ WARNING: Section '{section_name}' appears empty or too short"
                )
            
            # Check for placeholder content
            if self._contains_placeholder(content):
                messages.append(
                    f"⚠️ WARNING: Section '{section_name}' contains placeholder text"
                )

        return messages

    def _contains_placeholder(self, text: str) -> bool:
        """Check if text contains placeholder patterns."""
        placeholders = [
            "[content here]",
            "[placeholder]",
            "TODO:",
            "[INSERT",
            "[REMOVE",
            "placeholder",
            "blah blah",
            "lorem ipsum",
        ]
        text_lower = text.lower()
        return any(placeholder in text_lower for placeholder in placeholders)

    def _ensure_all_sections_present(
        self, content_sections: Dict[str, str], document_type: str, title: str
    ) -> Dict[str, str]:
        """
        Ensure all required sections are present in the document.
        If missing, generate or provide guidance.
        """
        from src.ai_engine import AdvancedContentGenerator

        required = self.required_sections_by_type.get(
            document_type.lower(),
            self.required_sections_by_type["research"]
        )

        complete_sections = dict(content_sections)

        # Check for missing sections
        missing_sections = [s for s in required if s not in [k.lower() for k in content_sections.keys()]]

        if missing_sections:
            logger.warning(f"Missing sections detected: {missing_sections}")
            
            # Generate missing sections
            try:
                generator = AdvancedContentGenerator()
                for missing_section in missing_sections:
                    logger.info(f"Generating missing section: {missing_section}")
                    generated_content = generator.generate_complete_document_sections(
                        sections=[missing_section],
                        title=title,
                        context="",
                        topics=[title],
                        style="academic",
                        total_words=500,
                    )
                    if generated_content:
                        complete_sections[missing_section] = generated_content.get(
                            missing_section, ""
                        )

            except Exception as e:
                logger.error(f"Error generating missing sections: {e}")

        return complete_sections

    def _create_ordered_document(
        self,
        title: str,
        sections: Dict[str, str],
        author: str,
        include_toc: bool,
        include_citations: bool,
        citations: Optional[List[str]],
    ) -> Dict[str, str]:
        """Create properly ordered document with all components."""
        ordered = {}

        # 1. Title
        ordered["Document Title"] = title

        # 2. Metadata
        ordered["Document Metadata"] = (
            f"Author: {author}\n"
            f"Date: {datetime.now().strftime('%B %d, %Y')}\n"
            f"Document Type: Research Document\n"
            f"Status: Complete"
        )

        # 3. Abstract/Executive Summary (if present)
        for key in ["Abstract", "Executive Summary", "abstract", "executive summary"]:
            if key in sections:
                ordered[key] = sections[key]
                break

        # 4. Table of Contents
        if include_toc:
            toc_content = self._generate_table_of_contents(sections)
            ordered["Table of Contents"] = toc_content

        # 5. Body sections (in logical order)
        body_section_order = [
            "Introduction",
            "Literature Review",
            "Background",
            "Methodology",
            "Methods",
            "Results",
            "Findings",
            "Discussion",
            "Analysis",
            "Recommendations",
            "Implications",
            "Conclusion",
            "Conclusions",
        ]

        added_sections = set()

        # Add sections in preferred order
        for section in body_section_order:
            for key in sections:
                if key.lower() == section.lower() and key not in added_sections:
                    ordered[key] = sections[key]
                    added_sections.add(key)

        # Add any remaining sections not in preferred order
        for key, content in sections.items():
            if key not in added_sections and key not in ordered:
                ordered[key] = content

        # 6. References/Bibliography
        if include_citations and citations:
            references_content = "\n\n".join(
                f"{i}. {citation}" for i, citation in enumerate(citations, 1)
            )
            ordered["References"] = references_content

        return ordered

    def _generate_table_of_contents(self, sections: Dict[str, str]) -> str:
        """Generate table of contents from sections."""
        toc_lines = ["# Table of Contents\n"]

        for i, section_name in enumerate(sections.keys(), 1):
            # Skip metadata and title from TOC
            if section_name not in ["Document Title", "Document Metadata"]:
                toc_lines.append(f"{i}. {section_name}")

        return "\n".join(toc_lines)

    def _validate_final_document(self, document: Dict[str, str]) -> List[str]:
        """Validate final assembled document."""
        messages = []

        if not document:
            messages.append("❌ ERROR: Document is empty")
            return messages

        # Check for minimum content
        total_chars = sum(len(v) for v in document.values())
        if total_chars < 1000:
            messages.append(
                f"⚠️ WARNING: Document is very short ({total_chars} characters)"
            )

        # Verify key sections exist
        section_names = [k.lower() for k in document.keys()]
        
        if not any(
            name in section_names for name in ["introduction", "conclusion", "results", "findings"]
        ):
            messages.append("⚠️ WARNING: Missing core content sections")

        # Success message
        messages.append(
            f"✅ Document assembled successfully with {len(document)} sections "
            f"({total_chars} total characters)"
        )

        return messages

    def validate_document_completeness(self, document: Dict[str, str]) -> Tuple[bool, List[str]]:
        """
        Validate that document is complete and ready for output.

        Returns:
            Tuple of (is_complete, validation_messages)
        """
        messages = []
        
        # Check each section
        for section_name, content in document.items():
            if not content:
                messages.append(f"❌ Empty section: {section_name}")
            elif len(content) < 100:
                messages.append(f"⚠️ Very short section: {section_name} ({len(content)} chars)")

        # Check overall completeness
        total_length = sum(len(c) for c in document.values())
        section_count = len(document)

        if section_count < 3:
            messages.append(f"❌ Too few sections: {section_count} (expected minimum 4-6)")
            return False, messages

        if total_length < 2000:
            messages.append(f"⚠️ Document very short: {total_length} characters")

        # Generate success message with stats
        messages.insert(0, f"✅ Document Complete: {section_count} sections, {total_length} characters")

        return True, messages

    def get_section_statistics(self, document: Dict[str, str]) -> Dict[str, Any]:
        """Get statistics about document sections."""
        stats = {
            "total_sections": len(document),
            "total_characters": sum(len(v) for v in document.values()),
            "total_words": sum(len(v.split()) for v in document.values()),
            "section_details": {},
        }

        for section_name, content in document.items():
            stats["section_details"][section_name] = {
                "characters": len(content),
                "words": len(content.split()),
                "paragraphs": len([p for p in content.split("\n\n") if p.strip()]),
            }

        return stats