I apply a reconcile command for each item in the list. It works fine, but when it does not find a match, it outputs No matches. I would like him to ignore and print the results just for coincidence.
absol is a variable containing a list
Here is the relevant part of the script:
def get_all_phrases_containing_tar_wrd(target_word, tar_passage, left_margin = 10, right_margin = 10):
Ellis = nltk.word_tokenize(tar_passage)
text = nltk.Text(Ellis)
c = nltk.ConcordanceIndex(text.Ellis, key = lambda s: s.lower())
concordance_txt = ([text.Ellis[map(lambda x: x-5 if (x-left_margin)>[0] else 0, [offset])[0]:offset+right_margin]
for offset in c.offsets(target_word)])
return [''.join([x+' ' for x in con_sub]) for con_sub in concordance_txt]
Ellis = nltk.word_tokenize(raw)
text = nltk.Text(Ellis)
for t_word in absol:
text.concordance(t_word)
print
print 'Results from function'
results = get_all_phrases_containing_tar_wrd(absol, raw)
for result in results:
print result
source
share