-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathchunk_parser.py
More file actions
23 lines (19 loc) · 761 Bytes
/
chunk_parser.py
File metadata and controls
23 lines (19 loc) · 761 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from nltk.chunk.named_entity import NEChunkParser, NEChunkParserTagger
from nltk.classify import NaiveBayesClassifier
from nltk.tag.simplify import simplify_wsj_tag
from nltk.tree import Tree
def simplify_chunk(chunk):
if isinstance(chunk, Tree):
return Tree(chunk.node, [simplify_chunk(c) for c in chunk])
elif isinstance(chunk, tuple):
word, tag = chunk
return (word, simplify_wsj_tag(tag))
else:
return chunk
# custom classes are required to use a custom classifier, the default is megam
class ChunkTagger(NEChunkParserTagger):
def _classifier_builder(self, train):
return NaiveBayesClassifier.train(train)
class ChunkParser(NEChunkParser):
def _train(self, corpus):
self._tagger = ChunkTagger([self._parse_to_tagged(s) for s in corpus])