retrieving tags from result of POS tagging

拥有回忆 提交于 2019-12-12 02:33:59

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!