Home / File/ nltk.py — langchain Source File

nltk.py — langchain Source File

Architecture documentation for nltk.py, a python file in the langchain codebase. 4 imports, 2 dependents.

File python DocumentProcessing TextSplitters 4 imports 2 dependents 2 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  0a45c4a1_846f_03df_b842_eb6b566c6404["nltk.py"]
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  0a45c4a1_846f_03df_b842_eb6b566c6404 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  0a45c4a1_846f_03df_b842_eb6b566c6404 --> 91721f45_4909_e489_8c1f_084f8bd87145
  885a8262_5dd0_fc53_460c_b7a8de727b5e["langchain_text_splitters.base"]
  0a45c4a1_846f_03df_b842_eb6b566c6404 --> 885a8262_5dd0_fc53_460c_b7a8de727b5e
  0a45c4a1_846f_03df_b842_eb6b566c6404["nltk.py"]
  0a45c4a1_846f_03df_b842_eb6b566c6404 --> 0a45c4a1_846f_03df_b842_eb6b566c6404
  e3efe57c_5b49_c26c_6ca5_45acccb8037f["html.py"]
  e3efe57c_5b49_c26c_6ca5_45acccb8037f --> 0a45c4a1_846f_03df_b842_eb6b566c6404
  0a45c4a1_846f_03df_b842_eb6b566c6404["nltk.py"]
  0a45c4a1_846f_03df_b842_eb6b566c6404 --> 0a45c4a1_846f_03df_b842_eb6b566c6404
  style 0a45c4a1_846f_03df_b842_eb6b566c6404 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""NLTK text splitter."""

from __future__ import annotations

from typing import Any

from typing_extensions import override

from langchain_text_splitters.base import TextSplitter

try:
    import nltk

    _HAS_NLTK = True
except ImportError:
    _HAS_NLTK = False


class NLTKTextSplitter(TextSplitter):
    """Splitting text using NLTK package."""

    def __init__(
        self,
        separator: str = "\n\n",
        language: str = "english",
        *,
        use_span_tokenize: bool = False,
        **kwargs: Any,
    ) -> None:
        """Initialize the NLTK splitter.

        Args:
            separator: The separator to use when combining splits.
            language: The language to use.
            use_span_tokenize: Whether to use `span_tokenize` instead of
                `sent_tokenize`.

        Raises:
            ImportError: If NLTK is not installed.
            ValueError: If `use_span_tokenize` is `True` and separator is not `''`.
        """
        super().__init__(**kwargs)
        self._separator = separator
        self._language = language
        self._use_span_tokenize = use_span_tokenize
        if self._use_span_tokenize and self._separator:
            msg = "When use_span_tokenize is True, separator should be ''"
            raise ValueError(msg)
        if not _HAS_NLTK:
            msg = "NLTK is not installed, please install it with `pip install nltk`."
            raise ImportError(msg)
        if self._use_span_tokenize:
            self._tokenizer = nltk.tokenize._get_punkt_tokenizer(self._language)  # noqa: SLF001
        else:
            self._tokenizer = nltk.tokenize.sent_tokenize

    @override
    def split_text(self, text: str) -> list[str]:
        # First we naively split the large input into a bunch of smaller ones.
        if self._use_span_tokenize:
            spans = list(self._tokenizer.span_tokenize(text))
            splits = []
            for i, (start, end) in enumerate(spans):
                if i > 0:
                    prev_end = spans[i - 1][1]
                    sentence = text[prev_end:start] + text[start:end]
                else:
                    sentence = text[start:end]
                splits.append(sentence)
        else:
            splits = self._tokenizer(text, language=self._language)
        return self._merge_splits(splits, self._separator)

Subdomains

Functions

Dependencies

  • langchain_text_splitters.base
  • nltk.py
  • typing
  • typing_extensions

Frequently Asked Questions

What does nltk.py do?
nltk.py is a source file in the langchain codebase, written in python. It belongs to the DocumentProcessing domain, TextSplitters subdomain.
What functions are defined in nltk.py?
nltk.py defines 2 function(s): _HAS_NLTK, nltk.
What does nltk.py depend on?
nltk.py imports 4 module(s): langchain_text_splitters.base, nltk.py, typing, typing_extensions.
What files import nltk.py?
nltk.py is imported by 2 file(s): html.py, nltk.py.
Where is nltk.py in the architecture?
nltk.py is located at libs/text-splitters/langchain_text_splitters/nltk.py (domain: DocumentProcessing, subdomain: TextSplitters, directory: libs/text-splitters/langchain_text_splitters).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free