问题
Using python how to retrieve only the tags - 'NN', 'JJ' etc from
('[', 'NN'),
("u'Tradus-Under", 'NN'),
("'", "''"),
(',', ','),
("u'Maintenance", 'JJ'),
("'", "''"),
(']', ':')
ie. from POS tagging result.
回答1:
Assuming those elements are in a list (I call that list lst
):
import string
lst = [
('[', 'NN'),
("u'Tradus-Under", 'NN'),
("'", "''"),
(',', ','),
("u'Maintenance", 'JJ'),
("'", "''"),
(']', ':')
]
tags = []
for _,poss_tag in lst:
if(len(poss_tag) == 2 and
poss_tag[0] == poss_tag[1] and
poss_tag[0] in string.ascii_letters):
tags.append(poss_tag)
print(tags)
Output:
['NN', 'NN', 'JJ']
If you want a unique set, you could bounce it to a set and back:
print(list(set(tags)))
Output:
['JJ', 'NN']
来源:https://stackoverflow.com/questions/29073796/retrieving-tags-from-result-of-pos-tagging