]> git.wh0rd.org - tt-rss.git/commitdiff
add button to clear bayes database
authorAndrew Dolgov <noreply@madoka.volgo-balt.ru>
Wed, 17 Jun 2015 13:50:21 +0000 (16:50 +0300)
committerAndrew Dolgov <noreply@madoka.volgo-balt.ru>
Wed, 17 Jun 2015 13:50:21 +0000 (16:50 +0300)
plugins/af_sort_bayes/init.js
plugins/af_sort_bayes/init.php

index a6335ef813e9ae52a8096bc90c37d76e62cc205a..e6523bc4f7d1d1028e26c0f5bcffc8d427eee500 100644 (file)
@@ -15,3 +15,23 @@ function bayesTrain(id, train_up) {
        }
 }
 
+function bayesClearDatabase() {
+       try {
+
+               if (confirm(__("Clear classifier database?"))) {
+
+                       var query = "backend.php?op=pluginhandler&plugin=af_sort_bayes&method=clearDatabase";
+
+                       new Ajax.Request("backend.php", {
+                               parameters: query,
+                               onComplete: function (transport) {
+                                       notify(transport.responseText);
+                               }
+                       });
+               }
+
+       } catch (e) {
+               exception_error("showTrgmRelated", e);
+       }
+}
+
index 79d287158483287d067b092955b1d66b7deb737b..958127689f6ea0b3742f1b57989b45a083fc86e0 100644 (file)
@@ -6,6 +6,7 @@ class Af_Sort_Bayes extends Plugin {
        private $filters = array();
        private $dbh;
        private $score_modifier = 50;
+       private $sql_prefix = "ttrss_plugin_af_sort_bayes";
 
        function about() {
                return array(1.0,
@@ -71,6 +72,10 @@ class Af_Sort_Bayes extends Plugin {
                return file_get_contents(__DIR__ . "/init.js");
        }
 
+       function get_prefs_js() {
+               return file_get_contents(__DIR__ . "/init.js");
+       }
+
        function hook_article_button($line) {
                return "<img src=\"plugins/af_sort_bayes/thumb_up.png\"
                        style=\"cursor : pointer\" style=\"cursor : pointer\"
@@ -84,7 +89,7 @@ class Af_Sort_Bayes extends Plugin {
        }
 
        function init_database() {
-               $prefix = "ttrss_plugin_af_sort_bayes";
+               $prefix = $this->sql_prefix;
 
                // TODO there probably should be a way for plugins to determine their schema version to upgrade tables
 
@@ -163,7 +168,27 @@ class Af_Sort_Bayes extends Plugin {
        function hook_prefs_tab($args) {
                if ($args != "prefPrefs") return;
 
-               print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('af_sort_bayes')."\">";
+               print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Bayesian classifier (af_sort_bayes)')."\">";
+
+               $result = $this->dbh->query("SELECT category, probability, word_count FROM {$this->sql_prefix}_categories WHERE owner_uid = " . $_SESSION["uid"]);
+
+               print "<table>";
+               print "<tr><th>Category</th><th>Probability</th><th>Word count</th></tr>";
+
+               while ($line = $this->dbh->fetch_assoc($result)) {
+                       print "<tr>";
+                       foreach ($line as $k => $v) {
+                               if ($k == "probability") $v = sprintf("%.3f", $v);
+
+                               print "<td>$v</td>";
+                       }
+                       print "</tr>";
+               }
+
+               print "</table>";
+
+               print "<button dojoType=\"dijit.form.Button\" onclick=\"return bayesClearDatabase()\">".
+                       __('Clear database')."</button> ";
 
                //
 
@@ -224,6 +249,19 @@ class Af_Sort_Bayes extends Plugin {
 
        }
 
+       function clearDatabase() {
+               $prefix = $this->sql_prefix;
+
+               $this->dbh->query("BEGIN");
+               $this->dbh->query("DELETE FROM ${prefix}_references WHERE owner_uid = " . $_SESSION["uid"]);
+               $this->dbh->query("DELETE FROM ${prefix}_wordfreqs WHERE owner_uid = " . $_SESSION["uid"]);
+               $this->dbh->query("COMMIT");
+
+               $nbs = new NaiveBayesianStorage($_SESSION["uid"]);
+               $nb = new NaiveBayesian($nbs);
+               $nb->updateProbabilities();
+       }
+
        function api_version() {
                return 2;
        }