寻找目标词语境

时间:2015-10-08 04:46:59

标签: python nlp target

大家好!刚刚开始学习python,我有这个任务:不使用nltk我必须在文本中找到一个目标词的索引,用3个参数(路径,目标词,上下文大小)编写一个函数,只是为了清楚上下文指的是字符而不是话。我开始切片,试图获得文本中单词开头的位置,但找到所有这些只是迭代在同一个字符串上...我不知道如何解决任务... 这应该是我的输出:对于目标词" boat"我应该有类似的东西:

 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))

0 个答案:

没有答案