mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-25 01:20:11 +08:00
60 lines
2.1 KiB
Python
60 lines
2.1 KiB
Python
#
|
|
# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
#
|
|
|
|
import pytest
|
|
|
|
from rag import nlp
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def stub_tokenize(monkeypatch):
|
|
def tokenize(doc, text, eng, language="English"):
|
|
doc["content_with_weight"] = text
|
|
|
|
monkeypatch.setattr(nlp, "tokenize", tokenize)
|
|
|
|
|
|
@pytest.mark.p2
|
|
@pytest.mark.parametrize(
|
|
("chunk", "parent"),
|
|
[
|
|
("first\nsecond", "first\nsecond"),
|
|
("\nfirst\nsecond", "first\nsecond"),
|
|
("\n\nfirst\nsecond", "\nfirst\nsecond"),
|
|
],
|
|
)
|
|
def test_tokenize_chunks_removes_one_leading_newline_from_parent(chunk, parent):
|
|
docs = nlp.tokenize_chunks([chunk], {}, True, child_delimiters_pattern="\n")
|
|
|
|
assert [doc["mom_with_weight"] for doc in docs] == [parent, parent]
|
|
assert [doc["content_with_weight"] for doc in docs] == ["first\n", "second"]
|
|
|
|
|
|
@pytest.mark.p2
|
|
def test_doc_tokenize_chunks_with_images_removes_leading_newline_from_parent():
|
|
docs = nlp.doc_tokenize_chunks_with_images([{"text": "\nfirst\nsecond", "ck_type": "text"}], {}, True, child_delimiters_pattern="\n")
|
|
|
|
assert [doc["mom_with_weight"] for doc in docs] == ["first\nsecond", "first\nsecond"]
|
|
assert [doc["content_with_weight"] for doc in docs] == ["first\n", "second"]
|
|
|
|
|
|
@pytest.mark.p2
|
|
def test_tokenize_chunks_with_images_removes_leading_newline_from_parent():
|
|
docs = nlp.tokenize_chunks_with_images(["\nfirst\nsecond"], {}, True, [object()], child_delimiters_pattern="\n")
|
|
|
|
assert [doc["mom_with_weight"] for doc in docs] == ["first\nsecond", "first\nsecond"]
|
|
assert [doc["content_with_weight"] for doc in docs] == ["first\n", "second"]
|