kbariotis/documer

PHP中贝叶斯算法实现,用于自动文档分类。

0.2.3 2014-12-23 22:35 UTC

This package is not auto-updated.

Last update: 2024-09-28 16:17:46 UTC


README

PHP中贝叶斯算法实现,用于自动文档分类。

概念

每个文档都有关键词,例如 玛格丽特·撒切尔

每个文档都有一个标签,例如 政治

假设在每个文档中,所有以大写字母开头的 关键词 都是我们需要识别的。我们将这些词存储在我们的数据库中,每次需要针对特定的 标签 对文档进行分类时,我们使用贝叶斯算法。

让我们澄清一下

训练

首先,我们将文档进行分词,并只保留我们的关键词(所有以大写字母开头的单词)到一个数组中。我们将这个数组存储到我们的数据库中。

猜测

这非常简单。再次,我们解析我们想要分类的文档,并创建一个包含关键词的数组。以下是伪代码

for every label in DB
	for every key word in document
		P(label/word) = P(word/label)P(label) /	( P(word/label)P(label) + (1 - P(word/label))(1 - P(label)) )

用法

通过composer安装

"require": {
    "kbariotis/documer": "dev-master"
  },

实例化

将存储适配器对象传递给Documer构造函数。

$documer = new Documer\Documer(new \Documer\Storage\Memory());

训练

$documer->train('politics', 'This is text about Politics and more');
$documer->train('philosophy', 'Socrates is an ancent Greek philosopher');
$documer->train('athletic', 'Have no idea about athletics. Sorry.');
$documer->train('athletic', 'Not a clue.');
$documer->train('athletic', 'It is just not my thing.');

猜测

$scores = $documer->guess('What do we know about Socrates?');

$scores 将包含一个数组,其中包含系统中的所有标签以及文档属于每个标签的可能性。

存储适配器 实现 Documer\Storage\Adapter 以创建您自己的存储适配器。