We can then test this against the first tagged sentence treebank_chunk, to compare the results with the previous recipe:
Code # 1: Testing by the first tag offer treebank_chunk
Named Entities: [[(’Pierre’,’ NNP’), (’Vinken’,’ NNP’)], [(’Nov .’, ’NNP’)]]
Note. The above code returns all native nouns — Pierre, Vinken, November.
NAME chunker — this is a simple use of the RegexpParser class. All sequences of words marked with NNP are concatenated into NAME fragments.
PersonChunker class can be used if you only want to separate the names of people.
Code # 2: PersonChunker class
PersonChunker class checks if each word is in its names_set (generated from the name corpus) by iterating over the tag sentence ... It uses the tags I-BERSON or I-PERSON if the current word is in names_set, depending on whether the previous word was also in names_set. The IOB tag is assigned to a word that is not in the names_set argument. The IOB tag list is converted to a tree using
conlltags2tree () upon completion.
Code # 3: Using the PersonChunker class in the same tag sentence
Person name: [[(’Pierre’,’ NNP’)]]