I have a list of keywords that I need to know if they are within 4 words of the word "access' in a sentence from a list. At the end, I want to total the number of times a keyword was matched with the word "access" for a particular sentence from a list.
Current output:
['Minority', 'patients', 'often', 'have', 'barrier', 'with', 'their', 'access', 'to', 'healthcare.'] 0
['Rural', 'patients', 'often', 'cite', 'distance', 'as', 'a', 'barrier', 'to', 'access', 'health', 'services.']
['Minority', 'patients', 'often', 'have', 'barriers', 'with', 'their', 'access', 'to', 'healthcare.'] 0
['Minority', 'patients', 'often', 'have', 'barriers', 'with', 'their', 'access', 'to', 'healthcare.'] 1
Desired output:
['Minority', 'patients', 'often', 'have', 'barriers', 'with', 'their', 'access', 'to', 'healthcare.'] 2
["I, am, an, avid, user, of, Microsoft, Access, databases"] 0
['Rural', 'patients', 'often', 'cite', 'distance', 'as', 'a', 'barrier', 'to', 'access', 'healthcare', 'services.'] 3
accessdesc = ["care", "services", "healthcare", "barriers"]
sentences = ["Minority patients often have barriers with their access to
healthcare.", "I am an avid user of Microsoft Access databases", "Rural
patients often cite distance as one of the barriers to access healthcare
services."]
for sentence in sentences:
nummatches = 0
for desc in accessdesc:
sentence = sentence.replace(".","") if "." in sentence else ''
sentence = sentence.replace(",","") if "," in sentence else ''
if 'access' in sentence.lower() and desc in sentence.lower():
sentence = sentence.lower().split()
access_position = sentence.index('access') if "access" in
sentence else 0
desc_position = sentence.index(desc) if desc in sentence else 0
if abs(access_position - desc_position) < 5 :
nummatches = nummatches + 1
else:
nummatches = nummatches + 0
print(sentence, nummatches)