Everyone! just starting learning python and i have this task: without using nltk I have to find concordances for a target word in a text writing a function with 3 arguments (path, targetword, context size), just to be clear the context refers to characters not words. I started with slicing on, trying to get the position of the starting of the word in the text but finding all of them it just iterates on the same string... I have no idea how to solve the task... This should be my output: for target word "boat" I should have something like:
fellow I saw on the Yarmouth boat one day, I could account for
u get fooling about with the boat and slop me overboard. If y
in the morning, and take the boat up to Chertsey, and George,
import sys,re
def concordances(path,targetword,contextsize):
with open(path,'r')as fin:
#text=fin.read()
text=fin.read()
for line in text:
line=line.rstrip('\n')
targets=text.split(targetword,2)[1]
print(targets)
#for target in targets[i]:
# start=text.find(target[0])
#print(start)
slice(text)
j=list(enumerate(text))
contextsize=int(contextsize)
key=re.search(targetword,text)
start=-contextsize
stop=contextsize
stext=text[start:stop]
# print('{0}{1}{2}'.format(lcontext,target, rcontext))