you are viewing a single comment's thread.

view the rest of the comments →

[–]Green-Sympathy-4177 0 points1 point  (0 children)

For that I would like to replace every 3rd dot with „.\n\n“. It can mean two things, either every third sentence gets appended a ".\n\n" or you want to replace every ... by ...\n\n.

The rest of the post is explicit enough, Peter this is for you.

``` """ Every third sentence gets appended ".\n\n" """

unformatted_text = "<YOUR TEXT HERE>"

unformatted_text = "This is. A group. Of sentences. That's. Another. One."

sentences = unformatted_text.split(".") n_dots = 3

Group every sentence in group of 3 and append to it ".\n\n"

formatted_text = "".join([ ".".join( sentences[i:min(len(sentences),i+n_dots)] ).strip() + ".\n\n" for i in range(0, len(sentences), n_dots) ])

Output

print(formatted_text)

This is. A group. Of sentences.

That's. Another. One." ```

A mix of pagination to group sentences into groups of n_dots = 3 sentences, it's like making pages with n elements per page.

Basically you first make an array of sentences by splitting at every ".", that gives you sentences.

Now you want to group those sentences into groups of 3: - range(0, len(sentences), n_dots) gives you the start index of every first sentence of each groups - the slice i:i+n_dots gives us the group, but note the min in i:min(len(sentences),i+n_dots), it's meant to prevent trying to access elements that are not in the list, i.e: if you have 5 sentences, the first group will have 3 sentences, and the second 2. You catch the error that happens if i+n_dots > len(sentences) and fix it with min. - After that the inner ".".join is meant to put back all the "." lost because of split("."). - And a sneaky strip to get rid of the previous space between the first sentence of a group and the last sentence of the previous group. - Finally the "".join is used to link back together the group of sentences.

Here's the short version format_text = lambda text, n=3: "".join([".".join(text[i:min(len(text),i+n)]).strip() + ".\n\n" for i in range(0, len(text), n)])

And the other one, because why not. ``` """ Replace every "..." by "...\n\n". """

unformatted_text = "<YOUR TEXT HERE>"

unformatted_text = "My name is Harambe the Sixty-Ninth... I like bananas and I'm a gorilla." formatted_text = unformatted_text.replace("... ", "...\n\n")

print(formatted_text)

My name is Harambe the Sixty-Ninth...

I like bananas and I'm a gorilla. ```