-1

I am trying to remove whole sentences that start with a certain phrase, but I want to retain the rest of the body text. For example:

text = "Hello I like dogs. I also like cats. Hello I like animals"

I want to remove any sentence that starts with "Hello" But retain the rest, therefore the function should only leave:

"I also like cats."

Currently I am experimenting with regex expressions, but I am unsure of a way to achieve this. Any help would be appreciated.

dm159
  • 29
  • Are the sentences on separate lines? – will-hedges May 26 '21 at 19:57
  • 2
    you should at least show what you've tried so far. please avoid asking for answers or people to write the code for you without having put some research into the problem yourself – Scinana May 26 '21 at 19:59

2 Answers2

1

Here is a basic approach. You may need to use something more fancy in order to split the sentences; see this post for more details.

>>> text = "Hello I like dogs. I also like cats. Hello I like animals"
>>> sentences = text.split(". ")
>>> ". ".join(s for s in sentences if not s.lower().startswith("hello")) + "."
'I also like cats.'
Andrew Eckart
  • 1,618
  • 9
  • 15
  • 2
    Not sure why this is being downvoted. This is a correct answer... – will-hedges May 26 '21 at 20:02
  • Hello, thank you so much for your response I really appreciate it. However, when I tried, it seems like it removes all of the text that begins with "Hello," rather than retaining the rest of the body text. – dm159 May 28 '21 at 19:06
0

read the code notes plaese:

text = "Hello I like dogs. I also like cats. Hello I like animals"
#get list of sentences, split by the DOT and space ". "
#like: ['Hello I like dogs', 'I also like cats', 'Hello I like animals']
t = text.split('. ')
#now lets loop for each value on our list
for v in t:
    #check if the first 5 letters are "Hello"
    if v[0:5] == 'Hello':
        #if it is - remove the value from the list.
        t.remove(v)
#now we have list of filtered strings:
#t

notice that the word 'Hello' may UPPER/LOWER case, so if you want to cover it all, add at the if:

if v[0:5].casefold() == 'hello':

It refers to the string as lowercase.

meni181818
  • 1,103
  • 1
  • 7
  • 11