PHP code example of abelzhou / php-tfidf

1. Go to this page and download the library: Download abelzhou/php-tfidf library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

abelzhou / php-tfidf example snippets


use AbelZhou\TfIdf\TiDocument;
use AbelZhou\TfIdf\AbsTransformer;
use AbelZhou\TfIdf\Transformer;
use AbelZhou\Tree\TrieTree;


//构造分类树
$tree = new TrieTree();
$tree->append("设计图");
$tree->append("大哭");
$tree->append("衣服");
$tree->append("友谊");

$contants = array(
    "我的设计图有点儿问题,我得修改一下。",
    "因为这个比较难看的设计图,她大哭了起来。",
    "老妈命令我把这些过时的衣服处理掉。",
    "幸运的是,爆炸没有造成人员伤亡,老小区的设计图有问题。但是很多人丢掉了衣服。",
    "为了准备这场考试,他花费了大量的时间和精力联系了很多设计图纸。",
    "在公共场合随地吐痰是很粗俗的行为。",
    "杯子装得太满了,果汁都溢出来了,洒到了衣服上。",
    "她趁母亲接电话时偷偷溜出去了。",
    "友谊是从互相信任开始的。"
);


$loadmem = 0;
$documents = array();
for ($i = 0; $i < count($contants); $i++) {
    $documents[] = new TiDocument($tree, $contants[$i], $i);
}

$transformer = new Transformer();
//添加文档书
$transformer->addContext($documents);
$res = $transformer->fit_transform($documents[3]);
var_dump($res);

array(2) {
  [0]=>
  array(5) {
    ["word"]=>
    string(9) "设计图"
    ["count"]=>
    int(1)
    ["tf"]=>
    float(0.5)
    ["idf"]=>
    float(0.58778666490212)
    ["tfidf"]=>
    float(0.29389333245106)
  }
  [1]=>
  array(5) {
    ["word"]=>
    string(6) "衣服"
    ["count"]=>
    int(1)
    ["tf"]=>
    float(0.5)
    ["idf"]=>
    float(0.81093021621633)
    ["tfidf"]=>
    float(0.40546510810816)
  }
}


class Transformer extends AbsTransformer {

    /**
     * 开放读取分类文章数据权限
     * @return mixed
     */
    protected function getSort(){
        return $this->_sort;
    }

    /**
     * 开放读取当前文章总数权限
     * @return mixed
     */
    protected function getDocumentCount(){
        return $this->_document_count;
    }

    /**
     * 在增加单个文档中埋点
     * @param TiDocument $document
     * @return mixed
     */
    function addDocument(TiDocument $document) {
        // TODO: Implement addDocument() method.
        
        //这里可以做一些存储数据到Redis的事情
        //@$redis->incr($document->getUnid(),1);
    }

    /**
     * 在增加单个文档中统计分类埋点
     * @param TiDocument $document
     * @param $tf_key
     * @param $tf
     * @return mixed
     */
    function addSort(TiDocument $document, $tf_key, $tf) {
        // TODO: Implement addSort() method.
        
        //这里也可以做一些存储数据到Redis的事情
    }
}
shell
    composer