Есть пара вещей, с которыми нужно обращаться, например, такие слова, как «Ужасная еда, ужасное обслуживание, плохое место». Должны ли "ужасные" и "ужасные" считаться вместе и устранение пунктуации. Они могут быть обработаны с помощью регулярных выражений. Также, если несколько слов имеют одинаковое количество, каждое должно быть показано. Следующий ответ это положительный.
with review as
-- CTE (Oracle: Subquery Factoring) for test data. TO BE Replaced by actual table.
(select 'Terrible food, terrible service, bad, bad place' || chr(13) || chr(10) || 'Just stay away!!' review_text from dual)
, review_words as
-- Strip target string of Punctuation and Control characters, also reduce multiple spaces to single space
(select regexp_replace(regexp_replace(review_text,'[[:punct:][:cntrl:]]',' '),'\s{2,}',' ') rwords
from review
)
, word_list as
-- Now from result of above the individual words and convert to lower case.
( select lower(regexp_substr(rwords,'[^ ]+',1,rownum)) words
from review_words connect by level <= regexp_count(rwords,' ')
)
-- get each word and count highest ranked words.
select word, cnt
from ( -- Rank the Word count
select word, cnt, rank() over(order by cnt desc) rnk
from (-- get the number of occurrence of eah word.
select words word, count(*) cnt
from word_list
group by words
)
)
where rnk = 1;
См. скрипка здесь.