]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_sort_bayes/init.php
e7822c23e836749d4cbf5b359cd09900c7a31815
3 class Af_Sort_Bayes
extends Plugin
{
6 private $filters = array();
8 private $score_modifier = 50;
12 "Bayesian classifier for tt-rss (WIP)",
16 function init($host) {
17 require_once __DIR__
. "/lib/class.naivebayesian.php";
18 require_once __DIR__
. "/lib/class.naivebayesianstorage.php";
21 $this->dbh
= Db
::get();
23 $this->init_database();
25 $host->add_hook($host::HOOK_ARTICLE_FILTER
, $this);
26 $host->add_hook($host::HOOK_PREFS_TAB
, $this);
27 $host->add_hook($host::HOOK_ARTICLE_BUTTON
, $this);
31 function trainArticle() {
32 $article_id = (int) $_REQUEST["article_id"];
33 $train_up = sql_bool_to_bool($_REQUEST["train_up"]);
35 $category = $train_up ?
"GOOD" : "NEUTRAL";
37 $nbs = new NaiveBayesianStorage($_SESSION["uid"]);
38 $nb = new NaiveBayesian($nbs);
40 $result = $this->dbh
->query("SELECT score, guid, title, content FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND id = " .
41 $article_id . " AND owner_uid = " . $_SESSION["uid"]);
43 if ($this->dbh
->num_rows($result) != 0) {
44 $guid = $this->dbh
->fetch_result($result, 0, "guid");
45 $title = $this->dbh
->fetch_result($result, 0, "title");
46 $content = mb_strtolower($title . " " . strip_tags($this->dbh
->fetch_result($result, 0, "content")));
47 $score = $this->dbh
->fetch_result($result, 0, "score");
49 $this->dbh
->query("BEGIN");
51 if ($nb->untrain($guid, $content)) {
52 if ($score >= $this->score_modifier
) $score -= $this->score_modifier
;
55 $nb->train($guid, $nbs->getCategoryByName($category), $content);
57 if ($category == "GOOD") $score +
= $this->score_modifier
;
59 $this->dbh
->query("UPDATE ttrss_user_entries SET score = '$score' WHERE ref_id = $article_id AND owner_uid = " . $_SESSION["uid"]);
61 $nb->updateProbabilities();
63 $this->dbh
->query("COMMIT");
67 print "$article_id :: $category";
71 return file_get_contents(__DIR__
. "/init.js");
74 function hook_article_button($line) {
75 return "<img src=\"plugins/af_sort_bayes/thumb_up.png\"
76 style=\"cursor : pointer\" style=\"cursor : pointer\"
77 onclick=\"bayesTrain(".$line["id"].", true)\"
78 class='tagsPic' title='".__('+1')."'>" .
79 "<img src=\"plugins/af_sort_bayes/thumb_down.png\"
80 style=\"cursor : pointer\" style=\"cursor : pointer\"
81 onclick=\"bayesTrain(".$line["id"].", false)\"
82 class='tagsPic' title='".__('-1')."'>";
86 function init_database() {
87 $prefix = "ttrss_plugin_af_sort_bayes";
89 // TODO there probably should be a way for plugins to determine their schema version to upgrade tables
91 /*$this->dbh->query("DROP TABLE IF EXISTS ${prefix}_wordfreqs", false);
92 $this->dbh->query("DROP TABLE IF EXISTS ${prefix}_references", false);
93 $this->dbh->query("DROP TABLE IF EXISTS ${prefix}_categories", false);*/
95 $this->dbh
->query("BEGIN");
97 // PG only for the time being
99 $this->dbh
->query("CREATE TABLE IF NOT EXISTS ${prefix}_categories (
100 id SERIAL NOT NULL PRIMARY KEY,
101 category varchar(100) NOT NULL DEFAULT '',
102 probability DOUBLE PRECISION NOT NULL DEFAULT '0',
103 owner_uid INTEGER NOT NULL REFERENCES ttrss_users(id) ON DELETE CASCADE,
104 word_count BIGINT NOT NULL DEFAULT '0')");
106 $this->dbh
->query("CREATE TABLE IF NOT EXISTS ${prefix}_references (
107 id SERIAL NOT NULL PRIMARY KEY,
108 document_id VARCHAR(255) NOT NULL,
109 category_id INTEGER NOT NULL REFERENCES ${prefix}_categories(id) ON DELETE CASCADE,
110 owner_uid INTEGER NOT NULL REFERENCES ttrss_users(id) ON DELETE CASCADE,
111 content text NOT NULL)");
113 $this->dbh
->query("CREATE TABLE IF NOT EXISTS ${prefix}_wordfreqs (
114 word varchar(100) NOT NULL DEFAULT '',
115 category_id INTEGER NOT NULL REFERENCES ${prefix}_categories(id) ON DELETE CASCADE,
116 owner_uid INTEGER NOT NULL REFERENCES ttrss_users(id) ON DELETE CASCADE,
117 count BIGINT NOT NULL DEFAULT '0')");
119 $owner_uid = @$_SESSION["uid"];
122 $result = $this->dbh
->query("SELECT id FROM ${prefix}_categories WHERE owner_uid = $owner_uid LIMIT 1");
124 if ($this->dbh
->num_rows($result) == 0) {
125 $this->dbh
->query("INSERT INTO ${prefix}_categories (category, owner_uid) VALUES ('GOOD', $owner_uid)");
126 $this->dbh
->query("INSERT INTO ${prefix}_categories (category, owner_uid) VALUES ('NEUTRAL', $owner_uid)");
130 $this->dbh
->query("COMMIT");
133 function hook_prefs_tab($args) {
134 if ($args != "prefPrefs") return;
136 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('af_sort_bayes')."\">";
143 function hook_article_filter($article) {
144 $owner_uid = $article["owner_uid"];
146 $nbs = new NaiveBayesianStorage($owner_uid);
147 $nb = new NaiveBayesian($nbs);
149 $categories = $nbs->getCategories();
151 if (count($categories) > 0) {
158 foreach ($categories as $id => $cat) {
159 if ($cat["category"] == "GOOD") {
161 $count_good +
= $cat["word_count"];
162 } else if ($cat["category"] == "NEUTRAL") {
164 $count_neutral +
= $cat["word_count"];
168 $dst_category = $id_neutral;
170 $bayes_content = mb_strtolower($article["title"] . " " . strip_tags($article["content"]));
172 if ($count_neutral >= 3000 && $count_good >= 1000) {
173 // enable automatic categorization
175 $result = $nb->categorize($bayes_content);
177 if (count($result) == 2) {
178 $prob_good = $result[$id_good];
179 $prob_neutral = $result[$id_neutral];
181 if ($prob_good > 0.90 && $prob_good > $prob_neutral) {
182 $dst_category = $id_good; // should we autofile as good or not? idk
183 $article["score_modifier"] +
= $this->score_modifier
;
188 $nb->train($article["guid_hashed"], $dst_category, $bayes_content);
190 $nb->updateProbabilities();
197 function api_version() {