Search code examples
python-3.xstringfull-text-search

Search for a string in .txt file, print whole sentence (everything before string, till '.' /fullstop sign/ and everything after string, till '.' sign)


This is my code, it currently prints the whole line, that contains the string. I want to print the whole sentence, that contains the searchString.

searchString = 'doctorate'
files = os.listdir() # goes directory with *.txt files
for eachFile in files:
    with open(eachFile, 'r', encoding='utf-8') as currentFile:
        allLines = currentFile.readlines()
        for line in allLines:
            if searchString in line:
                print (line)

Any help will be apreciated...


Solution

  • Here's a very simple, naïve approach to split the text into sentences:

    import re
    
    search = "doctorate"
    with open(path_to_your_file, 'r') as file:
        text = file.read().replace('\n', ' ')
        sentences = re.split(r"([\.!?])\s+", text)
        for index,sentence in enumerate(sentences):
            if search in sentence.lower():
                print(sentence + sentences[index+1])
    

    However, if your text is more complex, you will need more advanced parsing.