有一些事情需要处理,比如“糟糕的食物,糟糕的服务,糟糕的地方”。应该把“可怕”和“可怕”算在一起,还要去掉标点符号。这些可以通过正则表达式处理。此外,如果几个单词的计数相同,则应分别显示。下面的回答是肯定的。
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;
在此处查看fiddle。