词性标注领域存在多种技术方案,但推荐使用平均感知器算法(Averaged Perceptron)。该算法需要:
def predict(self, features):
scores = defaultdict(float)
for feat in features:
if feat not in self.weights: continue
for clas, weight in self.weights[feat].items():
scores[clas] += weight
return max(self.classes, key=lambda clas: (scores[clas], clas))采用迭代更新策略:
def train(self, nr_iter, examples):
for i in range(nr_iter):
for features, true_tag in examples:
guess = self.predict(features)
if guess != true_tag:
for f in features:
self.weights[f][true_tag] += 1
self.weights[f][guess] -= 1通过跟踪权重累计值和时间戳实现高效平均:
def update(self, truth, guess, features):
def upd_feat(c, f, v):
nr_iters = self.i - self._timestamps[f][c]
self._totals[f][c] += nr_iters * self.weights[f][c]
self.weights[f][c] += v
self._timestamps[f][c] = self.i
self.i += 1
for f in features:
upd_feat(truth, f, 1.0)
upd_feat(guess, f, -1.0)预处理策略:
核心特征包括:
add('i suffix', word[-3:]) # 词尾3字符
add('i-1 tag', prev) # 前一个标签
add('i-2 tag', prev2) # 前两个标签
add('i+1 suffix', context[i+1][-3:]) # 后一个词尾标注器 | WSJ准确率 | ABC新闻准确率 | 网络文本准确率 |
|---|---|---|---|
某机构Pattern | 93.5% | 90.7% | 88.1% |
某工具包NLTK | 94.0% | 91.5% | 88.4% |
本实现 | 96.8% | 94.8% | 91.8% |
完整实现包含在taggers.py和perceptron.py文件中,采用纯Python编写保证可读性。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。