Search code examples
pythonnlpnltkstanford-nlp

Extract tuple containing 'PERSON' in StanforNER list result


I am trying to create a name filter to filter out names from a given text. I am using the StanfordNER and NLTK to do so and doing this actually gives me a list containing tuples. (As shown below)

    [(u'I', u'O'), (u'met', u'O'), (u'with', u'O'), (u'Alan', u'PERSON'), (u'yesterday', u'O')]

What I want to do is that I just want to extract the exact name that has been tagged as 'PERSON' from the list above which contains tuples to get only 'Alan' at the end of the process. Please help.


Solution

  • weird_list = [(u'I', u'O'), (u'met', u'O'), (u'with', u'O'), (u'Alan', u'PERSON'), (u'yesterday', u'O')]
    for word, tag in weird_list:
        if tag == 'PERSON':
            print word