def test_preprocess(nlp: Pipeline):
    # We split a document into spans (in this case separated by the new line).
    nlp.text_splitter = lambda text: text.split('\n')
    raw_document = ("This is the test sentence 1.\n"
                    "This is the test sentence 2.\n"
                    "This is the test sentence 3.")
    task = nlp.preprocess(text=raw_document, aspects=['aspect_1', 'aspect_2'])
    assert isinstance(task, Task)
    assert len(task.subtasks) == 2
    assert list(task.subtasks) == ['aspect_1', 'aspect_2']
    assert len(task.examples) == 6
    assert task.indices == [(0, 3), (3, 6)]
    subtask_1, subtask_2 = task
    assert subtask_1.text == subtask_2.text == raw_document
    assert subtask_1.aspect == 'aspect_1'
    assert len(subtask_1.examples) == 3
def test_postprocess(nlp: Pipeline):
    text = ("We are great fans of Slack.\n"
            "The Slack often has bugs.\n"
            "best of all is the warm vibe")
    # Define a naive text_splitter.
    nlp.text_splitter = lambda text: text.split('\n')

    task = nlp.preprocess(text, aspects=['slack', 'price'])
    predictions = nlp.transform(task.examples)
    completed_task = nlp.postprocess(task, predictions)

    assert len(completed_task.examples) == 6
    assert completed_task.indices == [(0, 3), (3, 6)]

    slack, price = completed_task
    assert slack.text == price.text == text
    # The sentiment among fragments are different. We normalize scores.
    assert np.allclose(slack.scores, [0.03, 0.48, 0.48], atol=0.01)
    # Please note that there is a problem with the neutral sentiment.
    assert np.allclose(price.scores, [0.02, 0.49, 0.49], atol=0.01)