Nisa
Nisa

Reputation: 227

How to retrieve only those words from file which have noun tags?

I have a program to extract only those words from file which have pos tags present pos-tags variable. My program does not give any error but it does not show anything as well. It only executes. Here is my sample input:

[['For,IN', ',,,', 'We,PRP', 'the,DT', 'divine,NN', 'caused,VBD', 'apostle,NN', 'We,PRP', 'vouchsafed,VBD', 'unto,JJ', 'Jesus,NNP', 'the,DT', 'son,NN', 'of,IN', 'Mary,NNP', 'all,DT', 'evidence,NN', 'of,IN', 'the,DT', 'truth,NN', ',,,', 'and,CC', 'strengthened,VBD', 'him,PRP', 'with,IN', 'holy,JJ'], [ 'be,VB', 'nor,CC', 'ransom,NN', 'taken,VBN', 'from,IN', 'them,PRP', 'and,CC', 'none,NN', '\n']]

Here is my code:

import nltk
import os.path
import re
import os
sample_text4='E://QuranCopies45.txt'
file2 = open(sample_text4,'r',encoding='utf8')
arr=[]
for line in file2.readlines():
    words=re.split(' ',line)
    words=[line.replace('/',",")for line in words]
    arr.append(words)
pos_tags = ('NN', 'NNP', 'NNS', 'NNPS')
nouns=[s.split(',')[0] for sub in arr for s in sub if s.endswith(pos_tags)]
print(nouns)

Here is my expected output:

[ 'divine', 'apostle','Jesus', 'son','Mary',  'evidence',  'truth',  'ransom', 'none']

Upvotes: 1

Views: 57

Answers (1)

cs95
cs95

Reputation: 403128

You're really close, but you'd need to fix your if statement. The goal is to check if any of the elements from pos_tags exists in those list items... so, use any!

>>> [j.split(',')[0] for i in arr for j in i if any(j.endswith(p) for p in pos_tags)]     
['divine',
 'apostle',
 'Jesus',
 'son',
 'Mary',
 'evidence',
 'truth',
 'ransom',
 'none']

any performs short circuiting comparison, checking to see if any of the elements in pos_tags is present at the end of a list item. any returns True the moment it finds a tag for which the condition is satisfied. Otherwise, if, after iterating through pos_tags, none of the conditions are True, then any returns False.

For more information, see How do Python's any and all functions work?.

Upvotes: 1

Related Questions