Stop print concordance "no matches" in python

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)&gt[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
+4
source share
1 answer

In your program, you have the following lines:

text = nltk.Text(Ellis)
for t_word in absol:
    text.concordance(t_word)

You can replace these lines with the following:

ci = nltk.ConcordanceIndex(Ellis)
for t_word in absol:
    if ci.offsets(t_word):
        ci.print_concordance(t_word)

if , script , . , Text ConcordanceIndex.

+1

Source: https://habr.com/ru/post/1583826/


All Articles