-5

I've encountered an issue in python. I have a string that contains both a message and code, and I need to separate them and pass each to different functions. An example:

text = """
Can you change sum operation to multiplication?

def process(a: int, b: int):
    print(a + b)
"""

The text and code sections can appear in any order. I've tried using regex to separate them, but it fails with other languages. Do you have any suggestions on how to handle this without involving LLMS?
Thanks!

I've searched about how LLMS differentiate between code and text and found out they use Markdown formatting to separate them, like:

"See if anything is wrong with this code:"

```python
def process(a: int, b: int):
    print(a - b)
- ```

and I'm afraid I cannot follow this structure because the given input or string is not categorized like this

7
  • 2
    Please show examples of "text and code" and the corresponding expected output Commented Oct 31, 2024 at 11:03
  • The code lines in Python usually start with def, import, class and whitespace, but that is just an idea, it depends on how your Python files are formatted. Commented Oct 31, 2024 at 11:22
  • can the code have comments? please define other languages. Commented Oct 31, 2024 at 15:56
  • @WiktorStribiżew I see your point, I've tried this solution by defining some keywords and then applying regex on them. It’s somewhat acceptable, but as I mentioned, it can't be accurate as it is with python, if we bring up the matter of other languages like go, rust and more ... Commented Nov 2, 2024 at 17:52
  • @SIGHUP consider the example I mentioned in question, the expected output should be Sentences: Can you change sum operation to multiplication? -------------- Code: def process(a: int, b: int): print(a + b) Commented Nov 2, 2024 at 17:55

1 Answer 1

-2

Need more context, but here's a little simple example of one approach to separate text and code:

text = """
Can you change sum operation to multiplication?

def process(a: int, b: int):
    print(a + b)
"""

lines = text.strip().split('\n')
code_lines = []
text_lines = []

# Define where the code starts
code_start = False
for line in lines:
    if line.startswith('def '):  # Supposing as in this case line starts with function def
        code_start = True
    if code_start:
        code_lines.append(line)
    else:
        text_lines.append(line)


text_only = '\n'.join(text_lines).strip()
code_only = '\n'.join(code_lines).strip()

print(f"Text: \n{text_only}")
print(f"\nCode: \n{code_only}")

Expected output:

Text: 
Can you change sum operation to multiplication?
Code:
def process(a: int, b: int):
    print(a + b)
Sign up to request clarification or add additional context in comments.

6 Comments

your code throws AttributeError. even if i correct this behavior the output is not what you claim it is. you are not taking into account that The text and code sections can appear in any order.
You tried it adapting it in your code or you copied and pasted in a new python file? Please be more specific (also note that this was only an example of the logic you could implement/approach for what you needed)
@Yes Thanks for the answer, but it does not always work like this and the major problem is they can appear in any order. for example : first the code part, second the sentences or text
@Callme-Milad understood, I only gave a generic example, a possible starting 'schema' for what the solution could be. Anyway, as you said, it does not always work like that. Let me know if you find a solution. (also fixed the code, there was a typo)
@folengateis sorry, there was a typo in this code, anyway as said before, this was only an idea of how this could be handled, so in case it need implementing and adapting based to the code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.