朴素贝叶斯识别中性PHP

时间:2011-12-30 05:51:25

标签: php sentiment-analysis

我正在使用Ian Barbers Naive Bayes分析课来分析学校项目的句子情绪。我创建了自己的正中性和负面数据集。我的问题是我不知道如何实现中立并让类找到它们。下面的链接是我正在使用的php类

http://phpir.com/bayesian-opinion-mining

1 个答案:

答案 0 :(得分:2)

Opinion类已经非常灵活地添加了新的“情绪类”。只是classify方法已经实现了“先前”静态的计算。但它可以很容易地替换为foreach

private $classes = array('pos', 'neg', 'neutr');
private $classTokCounts = array('pos' => 0, 'neg' => 0, 'neutr' => 0);
private $classDocCounts = array('pos' => 0, 'neg' => 0, 'neutr' => 0);
private $prior = array('pos' => 1.0/3.0, 'neg' => 1.0/3.0, 'neutr' => 1.0/3.0);

public function classify($document) {
    // remove those:
    //$this->prior['pos'] = $this->classDocCounts['pos'] / $this->docCount;
    //$this->prior['neg'] = $this->classDocCounts['neg'] / $this->docCount;
    // add this:
    foreach($this->classes as $class) {
        $this->prior[$class] = $this->classDocCounts[$class] / $this->docCount;
    }

    // the rest is fine