This repository was archived by the owner on Apr 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
#68-Cpp documentation preprocessor #69
Open
jonathanMLDev
wants to merge
1
commit into
CppDigest:main
Choose a base branch
from
jonathanMLDev:dev-68
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| """ | ||
| Documentation preprocessing pipeline for LangChain RAG | ||
| """ | ||
|
|
||
| from pathlib import Path | ||
| from typing import List | ||
| from langchain_core.documents import Document | ||
| import re | ||
| from tqdm import tqdm | ||
| from datetime import datetime | ||
|
|
||
| from config import DocuConfig | ||
|
|
||
|
|
||
| class DocuPreprocessor: | ||
| """Process Boost library documentation for RAG""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| ): | ||
| """ | ||
| Initialize Documentation preprocessor. | ||
|
|
||
| Args: | ||
| docu_config: DocuConfig instance (loads from env vars if not provided) | ||
| """ | ||
| self.docu_config = DocuConfig() | ||
| self.raw_data_dir = Path(self.docu_config.raw_data_dir) | ||
| self.md_data_dir = Path(self.docu_config.md_data_dir) | ||
|
|
||
| def load_documents(self) -> List[Document]: | ||
| """Load and process all documents from data directory""" | ||
| docs_path = self.md_data_dir | ||
| if docs_path.exists(): | ||
| return self._get_documents_from_markdown_path(docs_path) | ||
| return [] | ||
|
|
||
| def _get_documents_from_markdown_path(self, docs_path: Path) -> List[Document]: | ||
| documents = [] | ||
| build_time = datetime.now().timestamp() | ||
|
|
||
| for file_path in tqdm(docs_path.rglob("*.md"), desc="Processing documentation"): | ||
| with open(file_path, "r", encoding="utf-8", errors="ignore") as f: | ||
| content = f.read() | ||
| url, content = self._extract_url_and_content(file_path, content) | ||
| if len(content) < 50: | ||
| continue | ||
| lib_or_field = self._extract_library_from_path(file_path) | ||
| doc = Document( | ||
| page_content=content, | ||
| metadata={ | ||
| "doc_id": url, | ||
| "lang": "en", | ||
| "type": "documentation", | ||
| "library": lib_or_field, | ||
| "version": "", | ||
| "build_time": build_time, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing metadata: hash, last_crawled. |
||
| }, | ||
| ) | ||
| documents.append(doc) | ||
| return documents | ||
|
|
||
| def _extract_library_from_path(self, file_path: Path) -> str: | ||
| """Extract library/source name from file path""" | ||
| try: | ||
| parts = file_path.parts | ||
| for i, part in enumerate(parts): | ||
| if "documentation" in part.lower() and i + 1 < len(parts): | ||
| return parts[i + 1] | ||
| return parts[0] if parts else "unknown" | ||
| except Exception: | ||
| return "unknown" | ||
|
|
||
| def _extract_url_and_content( | ||
| self, file_path: Path, content: str | ||
| ) -> tuple[str, str]: | ||
| """Get URL from file path""" | ||
| lines = content.split("\n") | ||
| url = "" | ||
| for line in lines[:3]: | ||
| if "Source URL:" in line: | ||
| url = line.split("Source URL:")[1].strip() | ||
| break | ||
| elif "**Source:**" in line: | ||
| url = line.split("**Source:**")[1].strip() | ||
| break | ||
|
|
||
| if url == "": | ||
| url = str(file_path).split("documentation")[-1].replace("\\", "/") | ||
| url = url.replace(".html", "").replace("_", "/") | ||
| if url.startswith("git_"): | ||
| url = url.replace("git_", "https://github.com/") | ||
| else: | ||
| url = "https:/" + url | ||
| else: | ||
| content = "\n".join(lines[3:]) | ||
| content = re.sub(r"\n\s*\n+", "\n\n", content).strip() | ||
| return url, content | ||
|
|
||
|
|
||
| def main(): | ||
| docu_preprocessor = DocuPreprocessor() | ||
| documents = docu_preprocessor.load_documents() | ||
| print(documents) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The self.raw_data_dir is never used.