Skip to content

A platform for community discussion. Free, open, simple.

UX: Handle frequent stemmer words in Rewind (#36584)

Martin Brennanba4d686e60a0
diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/action/top_words.rb b/plugins/discourse-rewind/app/services/discourse_rewind/action/top_words.rb
index 51013c4250c..97f42544666 100644
--- a/plugins/discourse-rewind/app/services/discourse_rewind/action/top_words.rb
+++ b/plugins/discourse-rewind/app/services/discourse_rewind/action/top_words.rb
@@ -3,6 +3,15 @@
module DiscourseRewind
module Action
class TopWords < BaseReport
+ STEMMER_WORKAROUNDS = {
+ "discour" => "discourse",
+ "discours" => "discourse",
+ "topical" => "topic",
+ "topically" => "topic",
+ "categori" => "category",
+ "categor" => "category",
+ }.freeze
+
FakeData = {
data: [
{ word: "seven", score: 100 },
@@ -17,85 +26,16 @@ module DiscourseRewind
def call
return FakeData if should_use_fake_data?
- words = DB.query(<<~SQL, user_id: user.id, date_start: date.first, date_end: date.last)
- WITH popular_words AS (
- SELECT
- *
- FROM
- ts_stat(
- $INNERSQL$
- SELECT
- search_data
- FROM
- post_search_data
- INNER JOIN
- posts ON posts.id = post_search_data.post_id
- WHERE
- posts.user_id = :user_id
- AND posts.created_at BETWEEN :date_start AND :date_end
- $INNERSQL$
- ) AS search_data
- WHERE LENGTH(word) >= 2
- AND word ~ '^[a-zA-Z]+$'
- AND word NOT IN (
- 'com', 'org', 'net', 'io', 'dev', 'co', 'uk', 'http', 'https',
- 'www', 'github', 'gitlab', 'google', 'youtube', 'twitter',
- 'slack', 'discord', 'drive'
- )
- ORDER BY
- nentry DESC,
- ndoc DESC,
- word
- LIMIT
- 100
- ), lex AS (
- SELECT
- DISTINCT ON (lexeme) to_tsvector('english', word) as lexeme,
- word as original_word
- FROM
- ts_stat ($INNERSQL$
- SELECT
- to_tsvector('simple',
- regexp_replace(raw, 'https?://[^\\s]+', ' ', 'g')
- )
- FROM
- posts AS p
- WHERE
- p.user_id = :user_id
- AND p.created_at BETWEEN :date_start AND :date_end
- $INNERSQL$)
- WHERE LENGTH(word) >= 2
- AND word ~ '^[a-zA-Z]+$'
- ), ranked_words AS (
- SELECT
- popular_words.*, lex.original_word,
- ROW_NUMBER() OVER (PARTITION BY word ORDER BY LENGTH(original_word) DESC) AS rn
- FROM
- popular_words
- INNER JOIN
- lex ON lex.lexeme @@ to_tsquery('english', popular_words.word)
- )
- SELECT
- word,
- ndoc,
- nentry,
- original_word
- FROM
- ranked_words
- WHERE
- rn = 1
- ORDER BY
- ndoc + nentry DESC
- LIMIT 10
- SQL
+ words = word_query
word_score =
words
.map do |word_data|
- # Little cheat, since sometimes the stemming process turns
- # "discourse" into "discour" or "discours"
- if word_data.original_word == "discour" || word_data.original_word == "discours"
- word_data.original_word = "discourse"
+ # Little cheat, since sometimes the stemming process uses words
+ # that we wouldn't normally use, especially for discourse-specific
+ # terms like "discourse" and "topic".
+ if STEMMER_WORKAROUNDS.key?(word_data.original_word)
+ word_data.original_word = STEMMER_WORKAROUNDS[word_data.original_word]
end
{ word: word_data.original_word, score: word_data.ndoc + word_data.nentry }
@@ -105,6 +45,80 @@ module DiscourseRewind
{ data: word_score, identifier: "top-words" }
end
+
+ def word_query
+ DB.query(<<~SQL, user_id: user.id, date_start: date.first, date_end: date.last)
+ WITH popular_words AS (
+ SELECT
+ *
+ FROM
+ ts_stat(
+ $INNERSQL$
+ SELECT
+ search_data
+ FROM
+ post_search_data
+ INNER JOIN
+ posts ON posts.id = post_search_data.post_id
+ WHERE
+ posts.user_id = :user_id
+ AND posts.created_at BETWEEN :date_start AND :date_end
+ $INNERSQL$
+ ) AS search_data
+ WHERE LENGTH(word) >= 2
+ AND word ~ '^[a-zA-Z]+$'
+ AND word NOT IN (
+ 'com', 'org', 'net', 'io', 'dev', 'co', 'uk', 'http', 'https',
+ 'www', 'github', 'gitlab', 'google', 'youtube', 'twitter',
+ 'slack', 'discord', 'drive'
+ )
+ ORDER BY
+ nentry DESC,
+ ndoc DESC,
+ word
+ LIMIT
+ 100
+ ), lex AS (
+ SELECT
+ DISTINCT ON (lexeme) to_tsvector('english', word) as lexeme,
+ word as original_word
+ FROM
+ ts_stat ($INNERSQL$
+ SELECT
+ to_tsvector('simple',
+ regexp_replace(raw, 'https?://[^\\s]+', ' ', 'g')
+ )
+ FROM
+ posts AS p
+ WHERE
+ p.user_id = :user_id
+ AND p.created_at BETWEEN :date_start AND :date_end
+ $INNERSQL$)
+ WHERE LENGTH(word) >= 2
+ AND word ~ '^[a-zA-Z]+$'
+ ), ranked_words AS (
+ SELECT
+ popular_words.*, lex.original_word,
+ ROW_NUMBER() OVER (PARTITION BY word ORDER BY LENGTH(original_word) DESC) AS rn
+ FROM
+ popular_words
+ INNER JOIN
+ lex ON lex.lexeme @@ to_tsquery('english', popular_words.word)
+ )
+ SELECT
+ word,
+ ndoc,
+ nentry,
+ original_word
+ FROM
+ ranked_words
+ WHERE
+ rn = 1
+ ORDER BY
+ ndoc + nentry DESC
+ LIMIT 10
+ SQL
+ end
end
end
end
diff --git a/plugins/discourse-rewind/spec/actions/top_words_spec.rb b/plugins/discourse-rewind/spec/actions/top_words_spec.rb
index 4f8c5c38b62..1a5983e4706 100644
--- a/plugins/discourse-rewind/spec/actions/top_words_spec.rb
+++ b/plugins/discourse-rewind/spec/actions/top_words_spec.rb
@@ -116,6 +116,34 @@ RSpec.describe DiscourseRewind::Action::TopWords do
expect(frequent_word_entry[:score]).to be > infrequent_word_entry[:score]
end
end
+
+ context "with stemmer workaround words" do
+ before do
+ DiscourseRewind::Action::TopWords
+ .any_instance
+ .stubs(:word_query)
+ .returns(
+ [
+ OpenStruct.new(original_word: "discours", ndoc: 5, nentry: 10),
+ OpenStruct.new(original_word: "topical", ndoc: 3, nentry: 7),
+ OpenStruct.new(original_word: "categori", ndoc: 4, nentry: 6),
+ ],
+ )
+ end
+
+ it "applies STEMMER_WORKAROUNDS to replace stemmed words with preferred forms" do
+ result = call_report
+
+ words = result[:data].map { |w| w[:word] }
+
+ expect(words).to include("discourse")
+ expect(words).to include("topic")
+ expect(words).to include("category")
+ expect(words).not_to include("topical")
+ expect(words).not_to include("categori")
+ expect(words).not_to include("discours")
+ end
+ end
end
context "when in rails development mode" do