This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def search(text,n): | |
'''Searches for text, and retrieves n words either side of the text, which are retuned seperatly''' | |
word = r"\W*([\w]+)" | |
groups = re.search(r'{}\W*{}{}'.format(word*n,'place',word*n), text).groups() | |
return groups[:n],groups[n:] | |
t = "The world is a small place, we should try to take care of it." | |
search(t,3) | |
#(('is', 'a', 'small'), ('we', 'should', 'try')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def search(text,n): | |
'''Searches for text, and retrieves n words either side of the text, which are retuned seperatly''' | |
word = r"\W*([\w]+)" | |
groups = re.search(r'{}\W*{}{}'.format(word*n,'place',word*n), text).groups() | |
return groups[:n],groups[n:] | |
t = "The world is a small place, we should try to take care of it." | |
search(t,3) | |
#(('is', 'a', 'small'), ('we', 'should', 'try')) |
NewerOlder