如何使用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;

但是我不太确定如何获得文档频率(应该是“South”的2而不是3)。
输出应该如下所示:
term     document_frequency
---------------------------
South    2
Georgia  1
and      1
the      1
Sandwich 1
Islands  1
Africa   1

最佳答案

因此,请计算每个术语的不同文档数:

select term, count(DISTINCT iso) as doc_frequency
from   countries
     , regexp_split_to_table(name, '[^\.\w]') term
where  term <> ''
group  by term;

关于sql - 在SQL中计算文档频率,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27260847/

10-15 21:03