私のニュース ページ プロジェクトには、次の構造を持つデータベース テーブルnewsがあります。
- id: [integer] unique number identifying the news entry, e.g.: *1983*
- title: [string] title of the text, e.g.: *New Life in America No Longer Means a New Name*
- topic: [string] category which should be chosen by the classificator, e.g: *Sports*
さらに、単語の頻度に関する情報を含むテーブルベイがあります。
- word: [string] a word which the frequencies are given for, e.g.: *real estate*
- topic: [string] same content as "topic" field above, e.h. *Economics*
- count: [integer] number of occurrences of "word" in "topic" (incremented when new documents go to "topic"), e.g: *100*
ここで、PHP スクリプトですべてのニュース エントリを分類し、いくつかの可能なカテゴリ (トピック) の 1 つをそれらに割り当てたいと考えています。
これは正しい実装ですか?改善できますか?
<?php
include 'mysqlLogin.php';
$get1 = "SELECT id, title FROM ".$prefix."news WHERE topic = '' LIMIT 0, 150";
$get2 = mysql_abfrage($get1);
// pTOPICS BEGIN
$pTopics1 = "SELECT topic, SUM(count) AS count FROM ".$prefix."bayes WHERE topic != '' GROUP BY topic";
$pTopics2 = mysql_abfrage($pTopics1);
$pTopics = array();
while ($pTopics3 = mysql_fetch_assoc($pTopics2)) {
$pTopics[$pTopics3['topic']] = $pTopics3['count'];
}
// pTOPICS END
// pWORDS BEGIN
$pWords1 = "SELECT word, topic, count FROM ".$prefix."bayes";
$pWords2 = mysql_abfrage($pWords1);
$pWords = array();
while ($pWords3 = mysql_fetch_assoc($pWords2)) {
if (!isset($pWords[$pWords3['topic']])) {
$pWords[$pWords3['topic']] = array();
}
$pWords[$pWords3['topic']][$pWords3['word']] = $pWords3['count'];
}
// pWORDS END
while ($get3 = mysql_fetch_assoc($get2)) {
$pTextInTopics = array();
$tokens = tokenizer($get3['title']);
foreach ($pTopics as $topic=>$documentsInTopic) {
if (!isset($pTextInTopics[$topic])) { $pTextInTopics[$topic] = 1; }
foreach ($tokens as $token) {
echo '....'.$token;
if (isset($pWords[$topic][$token])) {
$pTextInTopics[$topic] *= $pWords[$topic][$token]/array_sum($pWords[$topic]);
}
}
$pTextInTopics[$topic] *= $pTopics[$topic]/array_sum($pTopics); // #documentsInTopic / #allDocuments
}
asort($pTextInTopics); // pick topic with lowest value
if ($chosenTopic = each($pTextInTopics)) {
echo '<p>The text belongs to topic '.$chosenTopic['key'].' with a likelihood of '.$chosenTopic['value'].'</p>';
}
}
?>
トレーニングは手動で行われ、このコードには含まれていません。「不動産を売ればお金を稼ぐことができる」というテキストがカテゴリ/トピック「経済」に割り当てられている場合、すべての単語 (you,can,make,...) が「経済」のテーブルベイに挿入されます。トピックと 1が標準カウントです。単語が同じトピックとの組み合わせで既に存在する場合、カウントがインクリメントされます。
サンプル学習データ:
単語のトピック数
カチンスキー 政治 1
ソニーテクノロジー1
銀行経済学1
電話技術 1
ソニー経済学3
エリクソンテクノロジー2
サンプル出力/結果:
テキストのタイトル: 電話テスト Sony Ericsson Aspen - 敏感な Winberry
政治
....電話 ....テスト ....ソニー ....エリクソン ....アスペン ....センシティブ ....ウィンベリー
テクノロジー
....phone FOUND ....test ....sony FOUND ....ericsson FOUND ....aspen ....sensitive ....winberry
経済
....電話 ....test ....sony FOUND ....ericsson ....aspen ....sensitive ....winberry
結果: テキストはトピック Technology に属し、可能性は 0.013888888888889 です。
事前にどうもありがとうございました!