I have a text file having many paragraphs, i want to split it by sentences i.e. after every dot "." or ? it split and include the sentence in double Qoutes like:
This is a sentence. This is an excited sentence! And do you think this is a question? so what then .
"This is a sentence."
"This is an excited sentence!"
"And do you think this is a question?"
"so what then."
and save it in text file all the sentences.
def splitParagraphIntoSentences(paragraph):
import re
sentenceEnders = re.compile('[.!?]')
sentenceList = sentenceEnders.split(paragraph)
return sentenceList
if __name__ == '__main__':
p = """This is a sentence. This is an excited sentence! And do you think this is a question? so what to do then because many people will say this ok. and then what ?"""
sentences = splitParagraphIntoSentences(p)
for s in sentences:
sentence=(s.strip())
file = open("another.txt", "w")
file.write(sentence)
file.close()
it doesn't work, and not sure how to make each sentence in double quotes, any help ???
If I understood correctly what you are asking, try to modify your code to the following one:
import re
def splitParagraphIntoSentences(paragraph):
''' break a paragraph into sentences
and return a list '''
sentenceEnders = re.compile('[.!?]')
sentenceList = sentenceEnders.split(paragraph)
return sentenceList
if __name__ == '__main__':
p = "This is a sentence. This is an excited sentence! And do you think this is a question? so what to do then because many people will say this ok. and then what ?"
sentences = splitParagraphIntoSentences(p)
file = open('another.txt', "w")
for s in sentences:
if s.strip():
file.write('"' + s.strip() + '"\n') # Add a newline after each sentence
file.close()
In your case, instead of p
you need first of course to read the file, since yours (I guess) was just a simplification.