Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:869413421 Ai moive studio TextParserService parse to models

From Leeroopedia
Knowledge Sources
Domains NLP, Preprocessing
Last Updated 2026-10-23 12:00 GMT

Overview

Parser that detects chapters via regex heuristics, splits chapters into paragraphs, and splits paragraphs into sentences, returning a three-level data structure.

Description

TextParserService.parse_to_models is the core text parsing logic of the Picture Narrative Pipeline. It normalizes line breaks, detects chapter boundaries using regex pattern matching (supporting Chinese 第X章, English Chapter X, and numeric heading formats), filters and merges empty/volume-only chapters, and optionally splits overly long single chapters.

Each chapter is then split into paragraphs (by blank-line delimiters) and each paragraph into sentences (by language-aware segmentation). The result is three parallel lists of dictionaries ready for database insertion.

Usage

Import when you need to decompose raw text into a chapter→paragraph→sentence hierarchy. Called by ProjectProcessingService.process_uploaded_file.

Code Reference

Source Location

  • Repository: ai-moive-studio
  • File: backend/src/services/text_parser.py:L390-505

Signature

class TextParserService:
    async def parse_to_models(
        self,
        project_id: str,
        text: str,
        options: Optional[Dict[str, Any]] = None
    ) -> Tuple[List[Dict], List[Dict], List[Dict]]:
        """
        Args:
            project_id: project UUID
            text: raw text to parse
            options: parsing options (min_chapter_length: int, default 1000)

        Returns:
            (chapters_data, paragraphs_data, sentences_data)
        """

Import

from src.services.text_parser import text_parser_service

I/O Contract

Inputs

Name Type Required Description
project_id str Yes Project UUID
text str Yes Raw text to parse
options dict No Parsing options; min_chapter_length (default 1000)

Outputs

Name Type Description
chapters_data Dicts: title, content, chapter_number, word_count, paragraph_count, sentence_count, status
paragraphs_data Dicts: chapter_id, content, order_index, word_count, sentence_count, action
sentences_data Dicts: paragraph_id, content, order_index, word_count, character_count, status

Usage Examples

Parsing a Novel

from src.services.text_parser import text_parser_service

chapters, paragraphs, sentences = await text_parser_service.parse_to_models(
    project_id="abc-123",
    text=open("novel.txt").read(),
    options={"min_chapter_length": 1500},
)
print(f"Detected {len(chapters)} chapters, {len(sentences)} sentences")

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment