<commit_before>Bayes algorithm implementation in PHP for auto document classification.
==============
_author: [kbariotis](mailto:<EMAIL>)_

Description
-----------------------------
Inspired by [<NAME>](http://burakkanber.com/blog/machine-learning-naive-bayes-1/). Thank you for that great
article!

Concept
-----------------------------

_every document has key words e.g. *Margaret Thatcher*_

_every document has a label e.g. *Politics*_

Suppose, that in every document there are *key words all starting with an uppercase letter*. We store these words in our DB end every time we need to guess a document against a particular *label*, we use Bayes algorithm.

Let's clear that out:

**Training:**

	First, we tokenize the document and keep only our key words(All words starting with an uppercase letter) in an array. We store that array in our DB(See example schema below).

**Guessing:**

	This is very simple. Again, we parse the document we want to be classified and create an array with the key words. Here is the pseudo code:

		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)) )

Usage
------------
Documer uses Spot2 to store it's knowledge. Spot2 supports MySQL/SQLite.

**Install through composer**

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

**Instantiate**

Pass a [Spot](https://github.com/vlucas/spot2) object with your configuration to `getInstance`.

```php
$cfg = new \Spot\Config();
$cfg->addConnection('mysql', 'mysql://user:password@localhost/documer');
$spot = new \Spot\Locator($cfg);

$documer = Classifier\Classifier::getInstance($spot);

```

**Train**

```php
$documer->train("politics", "A big and long text about a political act");
```

**Guess**

```php
$scores = $documer->guess("And an other big and long text about a political act");
```

`$scores` will hold an array with all labels of your system and the posibbility which the document will belong to
each label.

**I am looking forward for your thoughts on this. Thank you!**

_<NAME>_<commit_msg>README updated<commit_after>Documer
==============
Bayes algorithm implementation in PHP for auto document classification.

Concept
-----------------------------

_every document has key words e.g. *<NAME>*_

_every document has a label e.g. *Politics*_

Suppose, that in every document there are *key words all starting with an uppercase letter*. We store these words in our DB end every time we need to guess a document against a particular *label*, we use Bayes algorithm.

Let's clear that out:

**Training:**

	First, we tokenize the document and keep only our key words(All words starting with an uppercase letter) in an array. We store that array in our DB(See example schema below).

**Guessing:**

	This is very simple. Again, we parse the document we want to be classified and create an array with the key words. Here is the pseudo code:

		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)) )

Usage
------------
Documer uses Spot2 to store it's knowledge. Spot2 supports MySQL/SQLite.

**Install through composer**

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

**Instantiate**

Pass a [Spot](https://github.com/vlucas/spot2) object with your configuration to `getInstance`.

```php
$cfg = new \Spot\Config();
$cfg->addConnection('mysql', 'mysql://user:password@localhost/documer');
$spot = new \Spot\Locator($cfg);

$documer = Classifier\Classifier::getInstance($spot);

```

**Train**

```php
$documer->train("politics", "A big and long text about a political act");
```

**Guess**

```php
$scores = $documer->guess("And an other big and long text about a political act");
```

`$scores` will hold an array with all labels of your system and the posibbility which the document will belong to
each label.
{'fraction_non_alphanumeric': 0.14904330312185296, 'fraction_numerical': 0.0027693856998992953, 'mean_word_length': 3.874846625766871, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6970941', 'n_tokens_mistral': 1227, 'n_tokens_neox': 1160, 'n_words': 486}