File size: 1,425 Bytes
36425a4
 
 
 
bbafb55
 
 
 
36425a4
bbafb55
36425a4
bbafb55
36425a4
 
bbafb55
36425a4
 
bbafb55
 
36425a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import markdown
import re
from typing import List, Dict, Any

def strip_frontmatter(content: str) -> str:
    """Remove YAML frontmatter (---...---) from markdown content."""
    return re.sub(r'^---\s*\n.*?\n---\s*\n', '', content, flags=re.DOTALL)

def semantic_chunking(markdown_content: str, min_tokens: int = 100, max_tokens: int = 512, overlap_tokens: int = 50) -> List[Dict[str, Any]]:
    content = strip_frontmatter(markdown_content)
    chunks = []
    paragraphs = content.split('\n\n')

    current_chunk_content = ""
    current_heading = "Introduction"

    for paragraph in paragraphs:
        if paragraph.strip().startswith('#'):
            current_heading = paragraph.strip().lstrip('#').strip()

        paragraph_tokens = len(paragraph.split())

        if len(current_chunk_content.split()) + paragraph_tokens > max_tokens:
            if current_chunk_content.strip():
                chunks.append({
                    "content": current_chunk_content.strip(),
                    "metadata": {"source": "unknown", "heading": current_heading}
                })
            current_chunk_content = paragraph
        else:
            current_chunk_content += "\n\n" + paragraph

    if current_chunk_content.strip():
        chunks.append({
            "content": current_chunk_content.strip(),
            "metadata": {"source": "unknown", "heading": current_heading}
        })

    return chunks