Split the line into words; the simplest is to use str.split()
:
for line in sentences:
if any(q.lower() == word.lower() for word in line.split()):
outfile.write(line + '\n')
You can add a .strip('?!."()')
to remove most common punctuation as well, perhaps.
Note that Python files, opened in text mode, will already use \r\n
on Windows if you write out a \n
. The code above also directly writes the matched lines to the output file.
Alternatively, use a regular expression to find matches:
import re
def finding(q, sentences, outfile):
pattern = re.compile(r'\b{}\b'.format(re.escape(q), flags=re.IGNORE)
for line in sentences:
if pattern.match(line)
outfile.write(line + '\n')
re.IGNORE
makes the match ignore case, \b
adds word boundaries and re.escape()
removes any expression metacharacters from the input query.