Skip to content

Commit

Permalink
splitters: Add ensure_ascii parameter (#18485)
Browse files Browse the repository at this point in the history
- **Description:** Add ensure_ascii parameter
  • Loading branch information
liugddx authored and hinthornw committed Apr 26, 2024
1 parent 6850d36 commit 366283a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
12 changes: 9 additions & 3 deletions libs/text-splitters/langchain_text_splitters/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,32 @@ def split_json(
return chunks

def split_text(
self, json_data: Dict[str, Any], convert_lists: bool = False
self,
json_data: Dict[str, Any],
convert_lists: bool = False,
ensure_ascii: bool = True,
) -> List[str]:
"""Splits JSON into a list of JSON formatted strings"""

chunks = self.split_json(json_data=json_data, convert_lists=convert_lists)

# Convert to string
return [json.dumps(chunk) for chunk in chunks]
return [json.dumps(chunk, ensure_ascii=ensure_ascii) for chunk in chunks]

def create_documents(
self,
texts: List[Dict],
convert_lists: bool = False,
ensure_ascii: bool = True,
metadatas: Optional[List[dict]] = None,
) -> List[Document]:
"""Create documents from a list of json objects (Dict)."""
_metadatas = metadatas or [{}] * len(texts)
documents = []
for i, text in enumerate(texts):
for chunk in self.split_text(json_data=text, convert_lists=convert_lists):
for chunk in self.split_text(
json_data=text, convert_lists=convert_lists, ensure_ascii=ensure_ascii
):
metadata = copy.deepcopy(_metadatas[i])
new_doc = Document(page_content=chunk, metadata=metadata)
documents.append(new_doc)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test text splitting functionality using NLTK and Spacy based sentence splitters."""

import pytest

from langchain_text_splitters.nltk import NLTKTextSplitter
Expand Down
1 change: 1 addition & 0 deletions libs/text-splitters/tests/unit_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Configuration for unit tests."""

from importlib import util
from typing import Dict, Sequence

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test text splitting functionality."""

import random
import re
import string
Expand Down

0 comments on commit 366283a

Please sign in to comment.