标签

PostgreSQL , 全文检索 , 词频统计 , ts_stat , madlib


背景

TF(Term Frequency 词频)/IDF(Inverse Document Frequency 逆向文本频率)是文本分析中常见的术语。

《PostgreSQL结合余弦、线性相关算法 在文本、图片、数组相似 等领域的应用 - 1 文本(关键词)分析理论基础 - TF(Term Frequency 词频)/IDF(Inverse Document Frequency 逆向文本频率)》

PostgreSQL支持全文检索,支持tsvector文本向量类型。

如何在一堆文本中,找到热词,或者对词频进行分析呢?

方法1,ts_stat

第一种方法来自PostgreSQL的内置函数,ts_stat,用于生成lexeme的统计信息,例如我想知道某个问答知识库中,出现最多的词是哪个,出现在了多少篇文本中。

ts_stat介绍如下

https://www.postgresql.org/docs/devel/static/functions-textsearch.html

https://www.postgresql.org/docs/devel/static/textsearch.html

https://www.postgresql.org/docs/devel/static/textsearch-features.html

12.4.4. Gathering Document Statistics

The function ts_stat is useful for checking your configuration and for finding stop-word candidates.

ts_stat(sqlquery text, [ weights text, ]  OUT word text, OUT ndoc integer,  OUT nentry integer) returns setof record

sqlquery is a text value containing an SQL query which must return a single tsvector column. ts_stat executes the query and returns statistics about each distinct lexeme (word) contained in the tsvector data. The columns returned are

  • word text — the value of a lexeme

  • ndoc integer — number of documents (tsvectors) the word occurred in

  • nentry integer — total number of occurrences of the word

If weights is supplied, only occurrences having one of those weights are counted.

For example, to find the ten most frequent words in a document collection:

SELECT * FROM ts_stat('SELECT vector FROM apod')
ORDER BY nentry DESC, ndoc DESC, word
LIMIT 10;

The same, but counting only word occurrences with weight A or B:

SELECT * FROM ts_stat('SELECT vector FROM apod', 'ab')
ORDER BY nentry DESC, ndoc DESC, word
LIMIT 10;

测试

1、创建生成随机字符串的函数

create or replace function gen_rand_str(int) returns text as $$    select substring(md5(random()::text), 4, $1);
$$ language sql strict stable;

2、创建生成若干个随机词的函数

create or replace function gen_rand_tsvector(int,int) returns tsvector as $$    select array_to_tsvector(array_agg(gen_rand_str($1))) from generate_series(1,$2);
$$ language sql strict;
postgres=# select gen_rand_tsvector(4,10);  gen_rand_tsvector
-----------------------------------------------------------------------  '21eb' '2c9c' '4406' '5d9c' '9ac4' 'a27b' 'ab13' 'ba77' 'e3f2' 'f198'
(1 row)

3、创建测试表,并写入测试数据

postgres=# create table ts_test(id int, info tsvector);
CREATE TABLE
postgres=# insert into ts_test select generate_series(1,100000), gen_rand_tsvector(4,10);
INSERT 0 100000

4、查看词频,总共出现了多少次,在多少篇文本(多少条记录中出现过)

postgres=# SELECT * FROM ts_stat('SELECT info FROM ts_test')
ORDER BY nentry DESC, ndoc DESC, word
LIMIT 10;  word | ndoc | nentry
------+------+--------  e4e6 |   39 |     39  9596 |   36 |     36  a84c |   35 |     35  2b44 |   32 |     32  5146 |   32 |     32  92f6 |   32 |     32  cd56 |   32 |     32  fd00 |   32 |     32  4258 |   31 |     31  5f18 |   31 |     31
(10 rows)

5、再写入一批测试数据,查看词频,总共出现了多少次,在多少篇文本(多少条记录中出现过)

postgres=# insert into ts_test select generate_series(1,100000), gen_rand_tsvector(2,10);
INSERT 0 100000
postgres=# SELECT * FROM ts_stat('SELECT info FROM ts_test')
ORDER BY nentry DESC, ndoc DESC, word
LIMIT 10;  word | ndoc | nentry
------+------+--------  30   | 4020 |   4020  a7   | 4005 |   4005  20   | 3985 |   3985  c5   | 3980 |   3980  e6   | 3970 |   3970  f1   | 3965 |   3965  70   | 3948 |   3948  5e   | 3943 |   3943  e4   | 3937 |   3937  2b   | 3934 |   3934
(10 rows)

方法2,madlib

实际上MADlib也提供了词频统计的训练函数

http://madlib.apache.org/docs/latest/group__grp__text__utilities.html

Term frequency

Term frequency tf(t,d) is to the raw frequency of a word/term in a document, i.e. the number of times that word/term t occurs in document d. For this function, 'word' and 'term' are used interchangeably. Note: the term frequency is not normalized by the document length.

    term_frequency(input_table,  doc_id_col,  word_col,  output_table,  compute_vocab)

Arguments:

input_table

TEXT. The name of the table storing the documents. Each row is in the form <doc_id, word_vector> where doc_id is an id, unique to each document, and word_vector is a text array containing the words in the document. The word_vector should contain multiple entries of a word if the document contains multiple occurrence of that word.

id_col

TEXT. The name of the column containing the document id.

word_col

TEXT. The name of the column containing the vector of words/terms in the document. This column should of type that can be cast to TEXT[].

output_table

TEXT. The name of the table to store the term frequency output. The output table contains the following columns:

  • id_col: This the document id column (same as the one provided as input).

  • word: A word/term present in a document. This is either the original word present in word_col or an id representing the word (depending on the value of compute_vocab below).

  • count: The number of times this word is found in the document.

compute_vocab

BOOLEAN. (Optional, Default=FALSE) Flag to indicate if a vocabulary is to be created. If TRUE, an additional output table is created containing the vocabulary of all words, with an id assigned to each word. The table is called output_table_vocabulary (suffix added to the output_table name) and contains the following columns:

  • wordid: An id assignment for each word

  • word: The word/term

参考

《PostgreSQL结合余弦、线性相关算法 在文本、图片、数组相似 等领域的应用 - 1 文本(关键词)分析理论基础 - TF(Term Frequency 词频)/IDF(Inverse Document Frequency 逆向文本频率)》

《一张图看懂MADlib能干什么》

http://madlib.apache.org/docs/latest/group__grp__text__utilities.html

https://www.postgresql.org/docs/devel/static/functions-textsearch.html

https://www.postgresql.org/docs/devel/static/textsearch.html

https://www.postgresql.org/docs/devel/static/textsearch-features.html

http://madlib.incubator.apache.org/

PostgreSQL 全文检索 - 词频统计相关推荐

  1. hive进行词频统计

    统计文件信息: $ /opt/cdh-5.3.6/hadoop-2.5.0/bin/hdfs dfs -text /user/hadoop/wordcount/input/wc.input hadoo ...

  2. 用R语言做词频统计_R语言 | 词频统计

    Python网络爬虫与文本数据分析 本章内容 导入停用词 读数据,分词 剔除停用词 导入停用词表 library(dplyr) ## [1] "?" "." & ...

  3. 统计csv词频_中文词频统计

    中文词频统计 1. 下载一长篇中文小说. <倚天屠龙记> 2. 从文件读取待分析文本. 3. 安装并使用jieba进行中文分词. pip install jieba import jieb ...

  4. 201671010417 金振兴 词频统计软件项目报告

    1.需求分析 按照<构建之法>第2章中2.3所述PSP流程,使用JAVA编程语言,独立完成一个英文文本词频统计的软件开发. .程序可读入任意英文文本文件,该文件中英文词数大于等于1个. . ...

  5. 字符串操作、文件操作,英文词频统计预处理

    1.字符串操作: 解析身份证号:生日.性别.出生地等 凯撒密码编码与解码 网址观察与批量生成 (1)解析身份证: 编译结果: (2)凯撒密码编码与解码 编译结果: 2.英文词频统计预处理 下载一首英文 ...

  6. Python_note6 组合数据类型+jieba库+文本词频统计

    集合类型和操作 集合元素不可修改,由不可变数据类型组成,元素不可重复 a = {"python",123,("python",123)}使用{}建立集合 b = ...

  7. 软工作业3: 词频统计

    词频统计 一.编译环境 (1)IDE:PyCharm 2018 (2)python版本:python3.6.3(Anaconda3-5.1.0  ) 二.程序分析 (1)读文件到缓冲区(process ...

  8. python 词频统计,分词笔记

    Python的中文分词库有很多,常见的有: jieba(结巴分词) THULAC(清华大学自然语言处理与社会人文计算实验室) pkuseg(北京大学语言计算与机器学习研究组) SnowNLP pynl ...

  9. Hadoop综合大作业补交4次作业:获取全部校园新闻,网络爬虫基础练习,中文词频统计,熟悉常用的Linux操作...

    1.用Hive对爬虫大作业产生的文本文件(或者英文词频统计下载的英文长篇小说)进行词频统计. (1)开启所有的服务,并创建文件夹wwc (2)查看目录下所有文件 (3)把hdfs文件系统中文件夹里的文 ...

  10. 201671010456-张琼 实验二 词频统计软件项目报

    一.需求分析 1.程序要能够读入英文文本,并且要求此文本不能为空. 2.程序能容纳的英文单词要在10万以上. 3.程序要能输入想查找的一个或者多个英文单词,并且统计出它出现的次数,并且把它的统计结果以 ...

最新文章

  1. 软件安装:原始码与 Tarball
  2. 转: 从微信的故障谈谈服务可用性
  3. 如何在多域下安装Exchang服务方法(二)
  4. EditPlus3.21注册码
  5. 圆方圆python入门:如何学习(二)
  6. P4219-[BJOI2014]大融合【LCT】
  7. TinyWeb:C#中的简单Web服务器
  8. 青蛙的约会 数论 拓展欧几里德
  9. python读取csv时keyerror_python – 错误:pandas hashtable keyerror
  10. 18.Linux/Unix 系统编程手册(上) -- 目录与链接
  11. 浅析融媒体的发展现状和未来前景
  12. php中round(),PHP round( )用法及代码示例
  13. UBUNTU-16.04.07和PREEMPT_RT日志
  14. 什么是善辩型人格?善辩性人格的职业分析
  15. MySQL 8 的学习——4从表中检索信息
  16. 干货 | 区块链项目估值的逻辑和误区
  17. linux core文件调试,Linux程序调试助手–core,解决段错误!
  18. 1.SD卡初始化、写入、读取、数据比较
  19. 【Derivation】正态分布特征函数证明-X~N(a,sigma^2)
  20. 动态代理:1 个经纪人如何代理 N 个明星

热门文章

  1. 团队-象棋游戏-开发环境搭建过程
  2. c += c-- | ++b;
  3. 文件的HANDLE转化为FILE*!!!
  4. 入门排序(冒泡、选择、直接)
  5. web项目继承ServletContainerInitializer进行访问HttpServlet(WebServlet)
  6. 解决intellij idea卡顿的方法
  7. 左神-05 二分搜索(多看几遍)
  8. 运行 Docker 容器时的安全风险:别丢了你的套接字
  9. iOS之UI--CAShapeLayer
  10. 程序员:如何成为一个全栈的工程师? 1