从你的问题来看,我也觉得这是一个NER问题。关于数据集,除非有一个标记预订号码并且与您的应用程序类似的数据集,否则您将必须创建自己的数据集。
我之前研究过类似的问题,我的数据集看起来像这样:
<TEAM>Northern</TEAM> NNP
<TEAM>Ireland</TEAM> NNP
man NN
<PLAYER>James</PLAYER> NNP
<PLAYER>McIlroy</PLAYER> NNP
is VBZ
confident JJ
he PRP
can MD
win VB
his PRP$
first JJ
major JJ
title NN
at IN
this DT
weekend NN
's POS
<COMPETITION>Spar</COMPETITION> JJ
<COMPETITION>European</COMPETITION> JJ
<COMPETITION>Indoor</COMPETITION> NNP
<COMPETITION>Championships</COMPETITION> NNP
in IN
<LOCATION>Madrid</LOCATION> NNP
您可以看到我在单词中有实体标签和词性标签。当我解析这个数据集进行训练时,我还添加了 IOB 标签(Inside、Outside 和Beginning)
[(('Claxton', 'NNP\n'), 'B-PLAYER'),
(('hunting', 'VBG\n'), 'O'),
(('first', 'RB\n'), 'O'),
(('major', 'JJ\n'), 'O'),
(('medal', 'NNS\n'), 'O'),
(('.', '.\n'), 'O'),
(('British', 'JJ\n'), 'O'),
(('hurdler', 'NN\n'), 'O'),
(('Sarah', 'NNP\n'), 'B-PLAYER'),
(('Claxton', 'NNP\n'), 'I-PLAYER')......]
然后我只使用了 ClassifierBasedTagger(还有其他标记器)。我找不到源代码,但我使用了以下代码:
class NamedEntityChunker(ChunkParserI):
def __init__(self, train_sents, **kwargs):
assert isinstance(train_sents, Iterable), 'The training set should be an Iterable'
self.feature_detector = features
self.tagger = ClassifierBasedTagger(
train = train_sents,
feature_detector = features,
**kwargs)
def parse(self, tagged_sents):
chunks = self.tagger.tag(tagged_sents)
iob_triplets = [(w, t, c) for ((w, t), c) in chunks]
return conlltags2tree(iob_triplets)
这里的 features 是一个函数,它返回要使用的特征的字典,例如前一个单词、前一个单词的 pos 标签等。只是用来训练模型的特征。
{
'word' : word,
'lemma' : stemmer.stem(word),
'pos' : pos,
'allascii' : allascii,
'next-word' : nextword,
'next-lemma' : stemmer.stem(nextword),
'next-pos' : nextpos,
'prev-word' : prevword,
'prev-lemma': stemmer.stem(prevword),
'prev-pos' : prevpos
}
你可以在这里找到有用的理论
我希望这有帮助。