Postgres and Word Clouds
function, postgresql, word-cloud
Solution
There is a simple way, but it can be slow (depending on your table size). You can split your text into an array:
SELECT string_to_array(lower(words), ' ') FROM table;
With those arrays, you can use `unnest` to aggregate them:
WITH words AS (
SELECT unnest(string_to_array(lower(words), ' ')) AS word
FROM table
)
SELECT word, count(*) FROM words
GROUP BY word;
This is a simple way of doing that and, has some issues, like, it only split words by space not punctuation marks.
Other, and probably better option, is to use PostgreSQL full text search.
Problem
I would like to know if its possible to create a Postgres function to scan some table rows and create a table that contains `WORD` and `AMOUNT` (frequency)? My goal is to use this table to create a Word Cloud.