Optimizing Code for Adding Spaces between Letters and Symbols, Excluding Parenthesized Words

98 Views Asked by At

I'm working on a script that processes a text file, and I'm facing a challenge in adding spaces between letters and symbols, excluding words within parentheses. I have implemented a solution using regular expressions, but it seems to have some issues.

I have a file called Sample.txt, and the texts in the "TEXT:" line in this file contain the names of the folders to be created.

Here is the current code snippet:

import re

def add_space_between_letter_and_symbol(text):
     # Add space between letter and symbol, excluding letters followed by a parenthesis
     return re.sub(r'(?<=[a-zA-Z(])\s*([^\w\s)(])|(?<=[a-zA-Z])\s+( ?=\b)', r'\1', text)

def process_text(text):
     # Add space between letter and symbol, excluding letters in parentheses
     text = add_space_between_letter_and_symbol(text)

I want to achieve the following:

1) When creating folder names, if any symbol and a letter are next to each other, add a space between the symbol and the letters.

2) Avoid adding spaces inside words in parentheses.

Examples

• TEXT: (Example)Code "or" [Example]Code

• Folder Name: (Example) Code | [Example] Code

Additional Description:

There is another process in the code directory that handles invalid characters in the "Text:" line or any file name. These characters are replaced with valid ones, specifically:

# Replace invalid characters from the folder name with a hyphen
replacements = {
    "<": "(", ">": ")",
    ":": ";", "/": "-", "\\": "-", "|": "-", "*": "-", '"': "''"
}

For instance, if the input is:

TEXT: |Example|Code

The replacements should be applied first (-Example-Code), and then the process of adding spaces (- Example - Code) between letters and symbols should take place.

I'm seeking advice on how to modify the code to handle this scenario gracefully without conflicts. Any help or suggestions would be highly appreciated.

(FOOTNOTE: I am encountering issues with this code and am looking for suggestions for improvement or a fixed version. I also want to apply this to other instances that may require a name change, not just folder names within the TEXT: line.)

0

There are 0 best solutions below