<?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. 普通Java Web项目为什么lib包要放在WEB-INF下

    首先一个项目要编译好之后才能部署到Tomcat中运行. Tomcat运行时如何找编译好的.class文件呢,其实Tomcat下的web项目有两个预置的classpath(就是能找到.class文件的入 ...

  2. Mysql笔试题

    1.查询Student表中的所有记录的Sname.Ssex和Class列. SELECT Sname,Ssex,Class FROM Students; 2.查询教师所有的单位即不重复的Depart列 ...

  3. 利用MathType为公式编号并引用

    序言 在理工科的论文撰写过程中, 公式编辑.编号以及引用非常普遍, 但是笔者没有发现word本身对公式编号和引用有比较好的支持, 所以只好求助于第三方插件. MathType在公式编辑方面表现比较出色 ...

  4. How to Preloading content with rel preload

    The preload value of the <link> element's rel attribute allows you to write declarative fetch ...

  5. Visual Studio 独立 Shell 下载

    https://visualstudio.microsoft.com/zh-hans/vs/older-downloads/isolated-shell/ SSMS 2017 安装问题 https:/ ...

  6. c#之AES加密解密

    .Net已封装好算法,直接调用即可,代码如下: 转载请注明出处:https://www.cnblogs.com/jietian331/p/9707771.html using System; usin ...

  7. zipCrack-v1.1 工具介绍

    一个暴力破解zip的工具 用python开发 与kali 自带的fcrackzip类似 git地址:https://github.com/mapyJJJ/python3-for-linux-h-.gi ...

  8. SQL update select

    SQL update select语句 最常用的update语法是: UPDATE TABLE_NAME SET column_name1 = VALUE WHRER column_name2 = V ...

  9. Roomblock: a Platform for Learning ROS Navigation With Roomba, Raspberry Pi and RPLIDAR(转)

      What is this? "Roomblock" is a robot platform consists of a Roomba, a Raspberry Pi 2, a ...

  10. .NET Framework简介

    NET Framework 就是微软Web Services 引擎1.NET Framework 旨在实现下列目标:提供一个一致的面向对象的编程环境,而无论对象代码是在本地存储和执行,还是在本地执行但 ...