def format_output(doc, sents):
    """
    Breaks the summarized text into paragraphs.

    Args:
        doc: a spacy.Doc object
        sents: a list of spacy.Spans, the sentences in the summary
    Returns:
        Text summary as a string with newlines
    """
    sent_iter = iter(sents)
    output = [next(sent_iter)]
    par_breaks = (idx for idx, tok in enumerate(doc) if '\n' in tok.text)

    try:
        # Find the first newline after first sentence
        idx = next(i for i in par_breaks if i >= output[0].end)
        for sent in sent_iter:
            if '\n' not in output[-1].text:
                if idx < sent.start:
                    # If there was no newline in the previous sentence
                    # and there is one in the text between the two sentences, add it
                    output.append(doc[idx])
            output.append(sent)
            idx = next(i for i in par_breaks if i >= sent.end)
    except StopIteration:
        # Add the rest of sentences if there are no more newlines
        output.extend(sent_iter)

    return ''.join(elem.text_with_ws for elem in output)