如何使用SQL计算文档频率?
文档频率是一个术语出现的文档(行)的数量,而不是一个术语的总数(即术语频率)。
我可以这样计算频率一词:
create table countries (
iso char(2) primary key,
name text not null unique
);
insert into countries values
('GS', 'South Georgia and the South Sandwich Islands'),
('ZA', 'South Africa');
select
term
, count(*) as term_frequency
from
countries
, regexp_split_to_table(name, '[^\.\w]') term
where
term <> ''
group by
term;但是,我不太确定如何获得文档频率(应该是2表示“南方”,而不是3)。
输出应该如下所示:
term document_frequency
---------------------------
South 2
Georgia 1
and 1
the 1
Sandwich 1
Islands 1
Africa 1发布于 2014-12-03 06:46:00
因此,计算每个术语的不同文档数:
select term, count(DISTINCT iso) as doc_frequency
from countries
, regexp_split_to_table(name, '[^\.\w]') term
where term <> ''
group by term;发布于 2014-12-02 23:36:32
不如:
select count(*) from countries where name similar to concat('\w', term, '\w');
以上都是未经测试的,可能有语法错误或4,但我认为一般的想法应该有效。
https://stackoverflow.com/questions/27260847
复制相似问题