(KWIC).
. , . re.split re.findall, .
, , .
, deque maxlen .
itertools:
from re import finditer
from itertools import tee, islice, izip, chain, repeat
def kwic(text, tgtword, width=10):
'Find all occurrences of tgtword and show the surrounding context'
matches = (mo.span() for mo in finditer(r"[A-Za-z\'\-]+", text))
padded = chain(repeat((0,0), width), matches, repeat((-1,-1), width))
t1, t2, t3 = tee((padded), 3)
t2 = islice(t2, width, None)
t3 = islice(t3, 2*width, None)
for (start, _), (i, j), (_, stop) in izip(t1, t2, t3):
if text[i: j] == tgtword:
context = text[start: stop]
yield context
print list(kwic(text, 'Snow-White'))