Keep a random newline in a string

Hi,

I have a string input, with several paragraphs separated by newlines.
My goal is to keep 25% of the newline seperations and replace 75% of them by a space. It needs to be random (but still at 25/75).

I thought of many ways to achieve this and tried them all today but didn’t succeed.

I need your fresh ideas on this, how would you approach it ?

Thank you !

P.S.: I would appreciate if the solution didn’t involve looping on each paragraph because the inputs are massive and I’m trying to optimize the process and number of operations of each run.

Hello @PG,
Welcome to the community.

I recommend you use any of the AI Models like GPT, Claude or Gemini.
Then give proper instructions(prompt) based on your requirements.

Note: In the prompt explain all the things properly like Input-Process-Output. A Beginner's Guide to Effective AI Prompts: The Input-Process-Output Framework

I hope this helps.


:bulb:P.S.: Always search first, Check Make Academy. If this is helpful, Mark Best as Solutions :white_check_mark: and give :+1:
If you need expert help or have questions? Contact or comment below! :point_down:

My Assistant and I also found a way without using Ai: :wink:

To achieve this goal of randomly keeping 25% of newline separations and replacing 75% with spaces, you can use a combination of string manipulation functions and random number generation.

  1. Split the input string into an array of paragraphs:
    {{split(1.input; “\n”)}}

  2. Iterate through the array using the “Iterator” module.

  3. For each iteration, generate a random number between 0 and 1:
    {{random}}

  4. Use an “If-then-else” module to determine whether to keep the newline or replace it with a space:
    {{if(random < 0.25; “\n”; " ")}}

  5. Combine the processed paragraphs back into a single string using the “Text Aggregator” module.

Here’s a more detailed breakdown of the process:

Step 1: Split the Input

Use the “Set Variable” module to split the input string into an array of paragraphs:

{{split(1.input; "\n")}}

Step 2: Iterate Through Paragraphs

Add an “Iterator” module and map it to the array created in Step 1.

Step 3: Process Each Paragraph

Add a “Set Variable” module inside the Iterator. Use this formula to determine whether to keep the newline or replace it with a space:

{{if(random < 0.25; "\n"; " ")}}

Step 4: Combine Processed Paragraphs

After the Iterator, add a “Text Aggregator” module. Set the following:

  • Text: Map to the processed paragraph from Step 3
  • Separator: Leave empty

This approach will randomly keep approximately 25% of the newline separations and replace 75% with spaces. The exact ratio may vary slightly due to the random nature of the process, but it will average out to the desired 25/75 split over a large number of paragraphs.

1 Like

Hello @PG,
The solution given by @nicon_ws looks good.
This approach saves AI tokens.

But I’m afraid about this line.

You need to use all the things like Iterator, Formulas, Text Aggregator and Variables.

tue! Didn’t read the last line :slight_smile:
But LLMs currently also don’t have unlimited context.
I would suggest Gemini which has a quite large max tokens.