host = $host; $this->dbh = Db::get(); $this->init_database(); $host->add_hook($host::HOOK_ARTICLE_FILTER, $this); $host->add_hook($host::HOOK_PREFS_TAB, $this); $host->add_hook($host::HOOK_ARTICLE_BUTTON, $this); } function trainArticle() { $article_id = (int) $_REQUEST["article_id"]; $train_up = sql_bool_to_bool($_REQUEST["train_up"]); //$category = $train_up ? "GOOD" : "UGLY"; $dst_category = "UGLY"; $nbs = new NaiveBayesianStorage($_SESSION["uid"]); $nb = new NaiveBayesianNgram($nbs, 3); $result = $this->dbh->query("SELECT score, guid, title, content FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND id = " . $article_id . " AND owner_uid = " . $_SESSION["uid"]); if ($this->dbh->num_rows($result) != 0) { $guid = $this->dbh->fetch_result($result, 0, "guid"); $title = $this->dbh->fetch_result($result, 0, "title"); $content = mb_strtolower($title . " " . strip_tags($this->dbh->fetch_result($result, 0, "content"))); $score = $this->dbh->fetch_result($result, 0, "score"); $this->dbh->query("BEGIN"); $ref = $nbs->getReference($guid, false); if (isset($ref['category_id'])) { $current_category = $nbs->getCategoryById($ref['category_id']); } else { $current_category = "UGLY"; } // set score to fixed value for now if ($train_up) { switch ($current_category) { case "UGLY": $dst_category = "GOOD"; $score = $this->score_modifier; break; case "BAD": $dst_category = "UGLY"; $score = 0; break; case "GOOD": $dst_category = "GOOD"; break; } } else { switch ($current_category) { case "UGLY": $dst_category = "BAD"; $score = -$this->score_modifier; break; case "BAD": $dst_category = "BAD"; break; case "GOOD": $dst_category = "UGLY"; $score = 0; break; } } $nb->untrain($guid, $content); $nb->train($guid, $nbs->getCategoryByName($dst_category), $content); $this->dbh->query("UPDATE ttrss_user_entries SET score = '$score' WHERE ref_id = $article_id AND owner_uid = " . $_SESSION["uid"]); $nb->updateProbabilities(); $this->dbh->query("COMMIT"); } print "$article_id :: $dst_category :: $score"; } function get_js() { return file_get_contents(__DIR__ . "/init.js"); } function get_prefs_js() { return file_get_contents(__DIR__ . "/init.js"); } function hook_article_button($line) { return "" . ""; } function init_database() { $prefix = $this->sql_prefix; // TODO there probably should be a way for plugins to determine their schema version to upgrade tables /*$this->dbh->query("DROP TABLE IF EXISTS ${prefix}_wordfreqs", false); $this->dbh->query("DROP TABLE IF EXISTS ${prefix}_references", false); $this->dbh->query("DROP TABLE IF EXISTS ${prefix}_categories", false);*/ $this->dbh->query("BEGIN"); // PG only for the time being if (DB_TYPE == "mysql") { $this->dbh->query("CREATE TABLE IF NOT EXISTS ${prefix}_categories ( id INTEGER NOT NULL PRIMARY KEY auto_increment, category varchar(100) NOT NULL DEFAULT '', probability DOUBLE NOT NULL DEFAULT '0', owner_uid INTEGER NOT NULL, FOREIGN KEY (owner_uid) REFERENCES ttrss_users(id) ON DELETE CASCADE, word_count BIGINT NOT NULL DEFAULT '0') ENGINE=InnoDB"); $this->dbh->query("CREATE TABLE IF NOT EXISTS ${prefix}_references ( id INTEGER NOT NULL PRIMARY KEY auto_increment, document_id VARCHAR(255) NOT NULL, category_id INTEGER NOT NULL, FOREIGN KEY (category_id) REFERENCES ${prefix}_categories(id) ON DELETE CASCADE, owner_uid INTEGER NOT NULL, FOREIGN KEY (owner_uid) REFERENCES ttrss_users(id) ON DELETE CASCADE) ENGINE=InnoDB"); $this->dbh->query("CREATE TABLE IF NOT EXISTS ${prefix}_wordfreqs ( word varchar(100) NOT NULL DEFAULT '', category_id INTEGER NOT NULL, FOREIGN KEY (category_id) REFERENCES ${prefix}_categories(id) ON DELETE CASCADE, owner_uid INTEGER NOT NULL, FOREIGN KEY (owner_uid) REFERENCES ttrss_users(id) ON DELETE CASCADE, count BIGINT NOT NULL DEFAULT '0') ENGINE=InnoDB"); } else { $this->dbh->query("CREATE TABLE IF NOT EXISTS ${prefix}_categories ( id SERIAL NOT NULL PRIMARY KEY, category varchar(100) NOT NULL DEFAULT '', probability DOUBLE PRECISION NOT NULL DEFAULT '0', owner_uid INTEGER NOT NULL REFERENCES ttrss_users(id) ON DELETE CASCADE, word_count BIGINT NOT NULL DEFAULT '0')"); $this->dbh->query("CREATE TABLE IF NOT EXISTS ${prefix}_references ( id SERIAL NOT NULL PRIMARY KEY, document_id VARCHAR(255) NOT NULL, category_id INTEGER NOT NULL REFERENCES ${prefix}_categories(id) ON DELETE CASCADE, owner_uid INTEGER NOT NULL REFERENCES ttrss_users(id) ON DELETE CASCADE)"); $this->dbh->query("CREATE TABLE IF NOT EXISTS ${prefix}_wordfreqs ( word varchar(100) NOT NULL DEFAULT '', category_id INTEGER NOT NULL REFERENCES ${prefix}_categories(id) ON DELETE CASCADE, owner_uid INTEGER NOT NULL REFERENCES ttrss_users(id) ON DELETE CASCADE, count BIGINT NOT NULL DEFAULT '0')"); } $owner_uid = @$_SESSION["uid"]; if ($owner_uid) { $result = $this->dbh->query("SELECT id FROM ${prefix}_categories WHERE owner_uid = $owner_uid LIMIT 1"); if ($this->dbh->num_rows($result) == 0) { $this->dbh->query("INSERT INTO ${prefix}_categories (category, owner_uid) VALUES ('GOOD', $owner_uid)"); $this->dbh->query("INSERT INTO ${prefix}_categories (category, owner_uid) VALUES ('BAD', $owner_uid)"); $this->dbh->query("INSERT INTO ${prefix}_categories (category, owner_uid) VALUES ('UGLY', $owner_uid)"); } } $this->dbh->query("COMMIT"); } function renderPrefsUI() { $result = $this->dbh->query("SELECT category, probability, word_count, (SELECT COUNT(id) FROM {$this->sql_prefix}_references WHERE category_id = {$this->sql_prefix}_categories.id) as doc_count FROM {$this->sql_prefix}_categories WHERE owner_uid = " . $_SESSION["uid"]); print "
".T_sprintf("Required UGLY word count for automatic matching: %d", $this->auto_categorize_threshold)."
"; print "Category | Probability | Words | Articles |
---|---|---|---|
$v | "; } print "