Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop concordance printing 'no matches' in python

I'm applying a concordance command over each item in a list. It works fine, but when it does not find a match, it prints out No matches. I would like it to ignore those and only print out results for matches.

absol is the variable that contains the 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
like image 331
Mohammed Al-mosaiwi Avatar asked Oct 31 '22 06:10

Mohammed Al-mosaiwi


1 Answers

In your program, you have these lines:

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

You can replace those lines with these:

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

The extra if will cause the script to ignore the items it cannot match. Note that you have to switch from using the Text object to the more specific ConcordanceIndex object.

like image 108
Robᵩ Avatar answered Nov 15 '22 04:11

Robᵩ