NLTK - Convert a chunked tree into a list (IOB tagging)
<p>I need to perform Named Entity Recognition / Classification, and generate output in IOB tagged format.</p>

<p>I'm using a NLTK chunker, as delivered by <a href="http://nltk-trainer.readthedocs.io/en/latest/train_chunker.html" rel="nofollow noreferrer">NLTK-train library</a>, but that produces a Tree, not a list of IOB tags.</p>

<pre><code>def chunk_iob(list_of_words):
    nltk_tagger = nltk.data.load("taggers/conll2002_aubt.pickle")
    nltk_chunker = nltk.data.load("chunkers/conll2002_NaiveBayes.pickle")

    t = nltk_tagger.tag(list_of_words)
    print(t)
    c = nltk_chunker.parse(t)
    print(c)
</code></pre>

<p>and we get c as a Tree, like:</p>

<pre><code>(S
  (LOC Barcelona/NC)
  (PER Juan/NC :/Fd)
</code></pre>

<p>...</p>

<p>But I am looking for something like:</p>

<pre><code>Barcelona - LOC
Juan - PER
...
</code></pre>

<p>which is the IOB tagged list of the list_of_words parameter, in the same order as list_of_words.</p>

<p>How can I get that tagged list from the tree? </p>