跳转到主要内容

隐藏对齐条件随机场,一种判别性字符串编辑距离

项目描述

https://travis-ci.org/dedupeio/pyhacrf.svg?branch=master https://ci.appveyor.com/api/projects/status/kibqrd7wnsk2ilpf/branch/master?svg=true

用于对字符串对进行分类的隐藏对齐条件随机场 - 可学习的编辑距离。

这是Dedupe.io云服务和开源工具集的一部分,用于在您的数据中去除重复项并找到模糊匹配:[https://dedupe.io](https://dedupe.io)

此包旨在以类似sklearn的接口实现HACRF机器学习模型。它包括将模型拟合到训练示例并评分新示例的方法。

该模型接受字符串对作为输入并将它们分类到任意数量的类别。在McCallum的原始论文中,该模型应用于数据库去重问题。每个数据库条目都与每个其他条目配对,然后根据匹配和不匹配的训练示例对配对进行分类,判断该配对是“匹配”还是“不匹配”。

我还尝试将其用作可学习的字符串编辑距离来规范化噪声文本。参见McCallum、Bellare和Pereira的《判别性训练的有限状态字符串编辑距离的条件随机场》以及Dirko Coetsee的报告《噪声文本归一化的条件随机场》。

示例

from pyhacrf import StringPairFeatureExtractor, Hacrf

training_X = [('helloooo', 'hello'), # Matching examples
              ('h0me', 'home'),
              ('krazii', 'crazy'),
              ('non matching string example', 'no really'), # Non-matching examples
              ('and another one', 'yep')]
training_y = ['match',
              'match',
              'match',
              'non-match',
              'non-match']

# Extract features
feature_extractor = StringPairFeatureExtractor(match=True, numeric=True)
training_X_extracted = feature_extractor.fit_transform(training_X)

# Train model
model = Hacrf(l2_regularization=1.0)
model.fit(training_X_extracted, training_y)

# Evaluate
from sklearn.metrics import confusion_matrix
predictions = model.predict(training_X_extracted)

print(confusion_matrix(training_y, predictions))
> [[0 3]
>  [2 0]]

print(model.predict_proba(training_X_extracted))
> [[ 0.94914812  0.05085188]
>  [ 0.92397711  0.07602289]
>  [ 0.86756034  0.13243966]
>  [ 0.05438812  0.94561188]
>  [ 0.02641275  0.97358725]]

依赖项

此包依赖于numpy。使用pylbfgs中的LBFGS优化器,但可以传递其他优化器。

安装

通过运行以下命令进行安装:

python setup.py install

或从pypi

pip install pyhacrf

开发

从仓库克隆,然后

pip install -r requirements.txt
cython pyhacrf/*.pyx
python setup.py install

要将软件包部署到 PyPI,请确保您已将 *.pyx 文件编译成 *.c 文件

由以下机构支持