0

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))
Lady R
  • 1
  • 3
  • Please give us a small example of the input and what the output should be. I gather that you are trying to find all occurrences of the target word, but I don't understand what role the context size plays. – saulspatz Oct 08 '15 at 05:09
  • Sorry I don't understand. I guess the text starting with "fellow" and ending with "George" is the text, but what is the context size? What output are you expecting? Is it just the three indices where the word "boat" appears in the text? – saulspatz Oct 08 '15 at 05:37

0 Answers0