('chapters', 'NNS')] # forth chunker = RegexpParser(r''' NP: {<DT><NN.*><.*>*<NN.*>} }<VB.*>{''') print(chunker.parse(s)) # back t = Tree('S', s) cs = ChunkString(t) print(cs) ur = ChunkRule('<DT><NN.*><.*>*<NN.*>', 'chunk determiners and nouns') ur.apply(cs) print(cs) ir = ChinkRule('<VB.*>', 'chink verbs') ir.apply(cs) print(cs) print(cs.to_chunkstruct()) # cs.to_chunkstruct().draw() chunker = RegexpChunkParser([ur, ir]) print(chunker.parse(t)) # set chunk name chunker = RegexpChunkParser([ur, ir], chunk_label='CP') print(chunker.parse(t))
# Loading Libraries from nltk.chunk.regexp import ChunkString, ChunkRule, ChinkRule from nltk.tree import Tree # ChunkString() starts with the flat tree tree = Tree('S', [('the', 'DT'), ('book', 'NN'), ('has', 'VBZ'), ('many', 'JJ'), ('chapters', 'NNS')]) # Initializing ChunkString() chunk_string = ChunkString(tree) print("Chunk String : ", chunk_string) # Initializing ChunkRule chunk_rule = ChunkRule('<DT><NN.*><.*>*<NN.*>', 'chunk determiners and nouns') chunk_rule.apply(chunk_string) print("\nApplied ChunkRule : ", chunk_string) # Another ChinkRule ir = ChinkRule('<VB.*>', 'chink verbs') ir.apply(chunk_string) print("\nApplied ChinkRule : ", chunk_string, "\n") # Back to chunk sub-tree chunk_string.to_chunkstruct()
# forth chunker = RegexpParser(r''' NP: {<DT><NN.*><.*>*<NN.*>} }<VB.*>{''' ) print(chunker.parse(s)) # back t = Tree('S', s) cs = ChunkString(t) print(cs) ur = ChunkRule('<DT><NN.*><.*>*<NN.*>', 'chunk determiners and nouns') ur.apply(cs) print(cs) ir = ChinkRule('<VB.*>', 'chink verbs') ir.apply(cs) print(cs) print(cs.to_chunkstruct()) # cs.to_chunkstruct().draw() chunker = RegexpChunkParser([ur, ir]) print(chunker.parse(t)) # set chunk name chunker = RegexpChunkParser([ur, ir], chunk_label='CP') print(chunker.parse(t))