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)>[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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With