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.
P.S.: Always search first, Check Make Academy. If this is helpful, Mark Best as Solutions and give If you need expert help or have questions? Contact or comment below!
My Assistant and I also found a way without using Ai:
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.
Split the input string into an array of paragraphs:
{{split(1.input; “\n”)}}
Iterate through the array using the “Iterator” module.
For each iteration, generate a random number between 0 and 1:
{{random}}
Use an “If-then-else” module to determine whether to keep the newline or replace it with a space:
{{if(random < 0.25; “\n”; " ")}}
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.