<?php

class Document
{
protected $words;
protected $tf_matrix;
protected $tfidf_matrix;
public function __construct($string)
{
$this->tfidf_matrix = null;
if (isset($string))
{
$string = strtolower($string);
$this->words = preg_split('/((^\p{P}+)|(\p{P}*\s+\p{P}*)|(\p{P}+$))/', $string, -1, PREG_SPLIT_NO_EMPTY);
$this->build_tf();
}
else
{
$this->words = null;
$this->tf_matrix = null;
}
}
public function build_tf()
{
if (isset($this->tf_matrix) && $this->tf_matrix)
return ;
$this->tfidf_matrix = null;
$words_count = count($this->words);
$words_occ = array_count_values($this->words);
foreach ($words_occ as $word => $amount)
$this->tf_matrix[$word] = $amount / $words_count;
arsort($this->tf_matrix);
}
public function build_tfidf($idf)
{
if (isset($this->tfidf_matrix) && $this->tfidf_matrix)
return true;
if (!isset($this->tf_matrix) || !$this->tf_matrix)
return false;
if (!isset($idf) || !$idf)
return false; if(is_array($idf)){
foreach ($this->tf_matrix as $word => $word_tf){
$this->tfidf_matrix[$word] = $word_tf * $idf[$word];
} }else{
foreach ($this->tf_matrix as $word => $word_tf){
$this->tfidf_matrix[$word] = $word_tf * $idf;
}
}
arsort($this->tfidf_matrix);
return true;
}
public function getWords()
{
return ($this->words);
}
public function getTf()
{
return ($this->tf_matrix);
}
public function getTfidf()
{
return ($this->tfidf_matrix);
}
} /*
第一步,计算词频。
考虑到文章有长短之分,为了便于不同文章的比较,进行"词频"标准化。 第二步,计算逆文档频率。
这时,需要一个语料库(corpus),用来模拟语言的使用环境。
如果一个词越常见,那么分母就越大,逆文档频率就越小越接近0。分母之所以要加1,是为了避免分母为0(即所有文档都不包含该词)。log表示对得到的值取对数。 第三步,计算TF-IDF。
可以看到,TF-IDF与一个词在文档中的出现次数成正比,与该词在整个语言中的出现次数成反比。所以,自动提取关键词的算法就很清楚了,就是计算出文档的每个词的TF-IDF值,然后按降序排列,取排在最前面的几个词。
*/
$text = 'i very good, ha , i very nice, i is good'; $obj = new Document($text);
$obj->build_tf(); //词频率TF,一般是词出现次数/总词数 $idf = log(3 / 2); //逆文档频率,总文档数/包含该词的文档数
$obj->build_tfidf($idf); //越高则频率高
var_dump($obj->getWords(), 88, $obj->getTf(), 99, $obj->getTfidf());

http://www.ruanyifeng.com/blog/2013/03/tf-idf.html

TF-IDF 提取关键词的更多相关文章

  1. TF-IDF与余弦类似性的应用(一):自己主动提取关键词

    作者: 阮一峰 日期: 2013年3月15日 原文链接:http://www.ruanyifeng.com/blog/2013/03/tf-idf.html 这个标题看上去好像非常复杂,事实上我要谈的 ...

  2. TF/IDF(term frequency/inverse document frequency)

    TF/IDF(term frequency/inverse document frequency) 的概念被公认为信息检索中最重要的发明. 一. TF/IDF描述单个term与特定document的相 ...

  3. 基于TF/IDF的聚类算法原理

        一.TF/IDF描述单个term与特定document的相关性TF(Term Frequency): 表示一个term与某个document的相关性. 公式为这个term在document中出 ...

  4. TF/IDF计算方法

    FROM:http://blog.csdn.net/pennyliang/article/details/1231028 我们已经谈过了如何自动下载网页.如何建立索引.如何衡量网页的质量(Page R ...

  5. 信息检索中的TF/IDF概念与算法的解释

    https://blog.csdn.net/class_brick/article/details/79135909 概念 TF-IDF(term frequency–inverse document ...

  6. 使用solr的函数查询,并获取tf*idf值

    1. 使用函数df(field,keyword) 和idf(field,keyword). http://118.85.207.11:11100/solr/mobile/select?q={!func ...

  7. TextRank算法提取关键词的Java实现

    转载:码农场 » TextRank算法提取关键词的Java实现 谈起自动摘要算法,常见的并且最易实现的当属TF-IDF,但是感觉TF-IDF效果一般,不如TextRank好. TextRank是在 G ...

  8. tf–idf算法解释及其python代码实现(下)

    tf–idf算法python代码实现 这是我写的一个tf-idf的简单实现的代码,我们知道tfidf=tf*idf,所以可以分别计算tf和idf值在相乘,首先我们创建一个简单的语料库,作为例子,只有四 ...

  9. tf–idf算法解释及其python代码实现(上)

    tf–idf算法解释 tf–idf, 是term frequency–inverse document frequency的缩写,它通常用来衡量一个词对在一个语料库中对它所在的文档有多重要,常用在信息 ...

  10. 文本分类学习(三) 特征权重(TF/IDF)和特征提取

    上一篇中,主要说的就是词袋模型.回顾一下,在进行文本分类之前,我们需要把待分类文本先用词袋模型进行文本表示.首先是将训练集中的所有单词经过去停用词之后组合成一个词袋,或者叫做字典,实际上一个维度很大的 ...

随机推荐

  1. 使用STS创建springboot项目pom.xml文件报错org.apache.maven.archiver.MavenArchiver.getManifest

    首先我的STS版本时:3.7.3 解决办法:->help->Install New Software -> add->location ->输入: http://repo ...

  2. input type = file 在部分安卓手机上无法调起摄像头和相册

    移动端H5web 用input type = file 在部分安卓手机上无法调起摄像头拍照,有的也无法访问相册而是直接访问了文档,解决办法是: 加上 accept = "image/*&qu ...

  3. 以管理员身份运行 cmd 删除无权限删除的文件夹

    rd 文件夹 PS:删除空文件夹 rd /s 文件夹 PS:删除文件夹,及所有子目录和文件 rd /s/q 文件夹 PS:强制删除文件夹,及所有子目录和文件,不需要提示

  4. UML作业第三次:分析《书店图书销售管理系统,绘制类图

    plantuml类图绘制方法的学习: 1.关于类图的学习: 类图显示了系统的静态结构. 类:类图中的主要元素,用矩形表示.矩形的上层表示类名.中层表示属性.下层表示方法. 类之间的关系:关联.依赖.聚 ...

  5. C# Json处理相关

    最近工作中遇到的Json问题确实很头大,主要是各种转义符的处理,想了一种通用的方式,来处理任意转移方式的Json字符串: /// <summary> /// 去除返回值中的转义符,返回js ...

  6. MongoDB在Windows系统下的安装和启动

    版本选择MongoDB的版本命名规范如:x.y.z: y为奇数时表示当前版本为开发版,如:2.3.0.2.1.1: y为偶数时表示当前版本为稳定版,如:2.0.1.2.2.0: 目前官网上最新的版本为 ...

  7. Oracle exp和expdp对数据进行备份

    以下给出两个示例,详细内容需要查阅手册: exp system OWNER=ZLTX FILE=ZLTX20190123.DMP expdp system DUMPFILE=ZLTX20190123. ...

  8. Installing ROS Indigo on the Raspberry Pi

    Installing ROS Indigo on the Raspberry Pi Description: This instruction covers the installation of R ...

  9. html5 css折叠导航栏

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  10. React脚手架create-react-app

    1.安装过程 $cnpm install -g -create-react-app $create-react-app apps $cd apps/ $npm start 2.图片路径 不能用相对路径 ...