Skip to content

Commit

Permalink
Add bearish and bullish sentiment ratios
Browse files Browse the repository at this point in the history
  • Loading branch information
tspenov committed Oct 26, 2023
1 parent 80adbc3 commit 623e35d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 19 deletions.
16 changes: 11 additions & 5 deletions lib/sanbase/social_data/trending_words.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ defmodule Sanbase.SocialData.TrendingWords do
datetme: DateTime.t(),
position: position
}
@table "trending_words_docs_v3"
@table "trending_words_docs_v4"

# When calculating the trending now words fetch the data for the last
# N hours to ensure that there is some data and we're not in the middle
Expand All @@ -72,13 +72,14 @@ defmodule Sanbase.SocialData.TrendingWords do
query_struct = get_trending_words_query(from, to, interval, size, source, word_type_filter)

ClickhouseRepo.query_reduce(query_struct, %{}, fn
[dt, word, project, score, context, summary, sentiment_ratios], acc ->
[dt, word, project, score, context, summary, sentiment_ratios, bb_sentiment_ratios], acc ->
slug = if project, do: String.split(project, "_", parts: 2) |> List.last()
datetime = DateTime.from_unix!(dt)
# The percentage of the documents that mention the word that have
# postive, negative or netural sentiment. The values are in the range [0, 1]
# and add up to 1
[pos_sentiment, neg_sentiment, neu_sentiment] = sentiment_ratios
[bull_bb_sentiment, bear_bb_sentiment, neu_bb_sentiment] = bb_sentiment_ratios

summaries = [%{source: source, datetime: datetime, summary: summary}]
context = transform_context(context)
Expand All @@ -93,7 +94,10 @@ defmodule Sanbase.SocialData.TrendingWords do
summaries: summaries,
positive_sentiment_ratio: pos_sentiment,
negative_sentiment_ratio: neg_sentiment,
neutral_sentiment_ratio: neu_sentiment
neutral_sentiment_ratio: neu_sentiment,
bullish_bb_sentiment_ratio: bull_bb_sentiment,
bearish_bb_sentiment_ratio: bear_bb_sentiment,
neutral_bb_sentiment_ratio: neu_bb_sentiment
}

Map.update(acc, datetime, [elem], fn words -> [elem | words] end)
Expand Down Expand Up @@ -244,7 +248,8 @@ defmodule Sanbase.SocialData.TrendingWords do
score,
context,
summary,
sentiment_ratios
sentiment_ratios,
bb_sentiment_ratios
FROM
(
SELECT
Expand All @@ -256,7 +261,8 @@ defmodule Sanbase.SocialData.TrendingWords do
score / {{score_equalizer}} AS score,
words_context AS context,
summary,
tuple(pos_ratio, neg_ratio, neu_ratio) AS sentiment_ratios
tuple(pos_ratio, neg_ratio, neu_ratio) AS sentiment_ratios,
tuple(bull_bb_ratio, bear_bb_ratio, neu_bb_ratio) AS bb_sentiment_ratios
FROM #{@table}
WHERE
dt >= toDateTime({{from}}) AND
Expand Down
4 changes: 4 additions & 0 deletions lib/sanbase_web/graphql/schema/types/social_data_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ defmodule SanbaseWeb.Graphql.SocialDataTypes do
field(:positive_sentiment_ratio, :float)
field(:negative_sentiment_ratio, :float)
field(:neutral_sentiment_ratio, :float)
# bearish/bullish sentiment ratios
field(:bullish_bb_sentiment_ratio, :float)
field(:bearish_bb_sentiment_ratio, :float)
field(:neutral_bb_sentiment_ratio, :float)

field :project, :project do
cache_resolve(&SocialDataResolver.project_from_root_slug/3)
Expand Down
79 changes: 65 additions & 14 deletions test/sanbase/social_data/trending_words/trending_words_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ defmodule Sanbase.SocialData.TrendingWordsTest do
word: "ethereum",
negative_sentiment_ratio: 0.3,
neutral_sentiment_ratio: 0.5,
positive_sentiment_ratio: 0.2
positive_sentiment_ratio: 0.2,
bearish_bb_sentiment_ratio: 0.3,
neutral_bb_sentiment_ratio: 0.5,
bullish_bb_sentiment_ratio: 0.2
},
%{
context: [%{score: 1.0, word: "usd"}, %{score: 0.5, word: "money"}],
Expand All @@ -61,7 +64,10 @@ defmodule Sanbase.SocialData.TrendingWordsTest do
word: "bitcoin",
negative_sentiment_ratio: 0.3,
neutral_sentiment_ratio: 0.5,
positive_sentiment_ratio: 0.2
positive_sentiment_ratio: 0.2,
bearish_bb_sentiment_ratio: 0.3,
neutral_bb_sentiment_ratio: 0.5,
bullish_bb_sentiment_ratio: 0.2
}
],
dt2 => [
Expand All @@ -80,7 +86,10 @@ defmodule Sanbase.SocialData.TrendingWordsTest do
word: "boom",
negative_sentiment_ratio: 0.1,
neutral_sentiment_ratio: 0.7,
positive_sentiment_ratio: 0.2
positive_sentiment_ratio: 0.2,
bearish_bb_sentiment_ratio: 0.1,
neutral_bb_sentiment_ratio: 0.7,
bullish_bb_sentiment_ratio: 0.2
},
%{
context: [%{score: 1.0, word: "usd"}, %{score: 0.5, word: "money"}],
Expand All @@ -97,7 +106,10 @@ defmodule Sanbase.SocialData.TrendingWordsTest do
word: "san",
negative_sentiment_ratio: 0.1,
neutral_sentiment_ratio: 0.5,
positive_sentiment_ratio: 0.4
positive_sentiment_ratio: 0.4,
bearish_bb_sentiment_ratio: 0.1,
neutral_bb_sentiment_ratio: 0.5,
bullish_bb_sentiment_ratio: 0.4
}
],
dt3 => [
Expand All @@ -116,7 +128,10 @@ defmodule Sanbase.SocialData.TrendingWordsTest do
word: "xrp",
negative_sentiment_ratio: 0.3,
neutral_sentiment_ratio: 0.5,
positive_sentiment_ratio: 0.2
positive_sentiment_ratio: 0.2,
bearish_bb_sentiment_ratio: 0.3,
neutral_bb_sentiment_ratio: 0.5,
bullish_bb_sentiment_ratio: 0.2
},
%{
context: [%{score: 1.0, word: "usd"}, %{score: 0.5, word: "money"}],
Expand All @@ -133,7 +148,10 @@ defmodule Sanbase.SocialData.TrendingWordsTest do
word: "eth",
negative_sentiment_ratio: 0.3,
neutral_sentiment_ratio: 0.5,
positive_sentiment_ratio: 0.2
positive_sentiment_ratio: 0.2,
bearish_bb_sentiment_ratio: 0.3,
neutral_bb_sentiment_ratio: 0.5,
bullish_bb_sentiment_ratio: 0.2
}
]
}
Expand Down Expand Up @@ -181,7 +199,10 @@ defmodule Sanbase.SocialData.TrendingWordsTest do
word: "xrp",
negative_sentiment_ratio: 0.3,
neutral_sentiment_ratio: 0.5,
positive_sentiment_ratio: 0.2
positive_sentiment_ratio: 0.2,
bearish_bb_sentiment_ratio: 0.3,
neutral_bb_sentiment_ratio: 0.5,
bullish_bb_sentiment_ratio: 0.2
},
%{
context: [%{score: 1.0, word: "usd"}, %{score: 0.5, word: "money"}],
Expand All @@ -198,7 +219,10 @@ defmodule Sanbase.SocialData.TrendingWordsTest do
word: "eth",
negative_sentiment_ratio: 0.3,
neutral_sentiment_ratio: 0.5,
positive_sentiment_ratio: 0.2
positive_sentiment_ratio: 0.2,
bearish_bb_sentiment_ratio: 0.3,
neutral_bb_sentiment_ratio: 0.5,
bullish_bb_sentiment_ratio: 0.2
}
]
}
Expand Down Expand Up @@ -308,12 +332,39 @@ defmodule Sanbase.SocialData.TrendingWordsTest do
]

[
[dt1_unix, "bitcoin", "BTC_bitcoin", 10, context, "summary1", [0.2, 0.3, 0.5]],
[dt1_unix, "ethereum", "ETH_ethereum", 5, context, "summary2", [0.2, 0.3, 0.5]],
[dt2_unix, "san", "SAN_santiment", 2, context, "summary3", [0.4, 0.1, 0.5]],
[dt2_unix, "boom", nil, 70, context, "summary4", [0.2, 0.1, 0.7]],
[dt3_unix, "eth", "ETH_ethereum", 1, context, "summary5", [0.2, 0.3, 0.5]],
[dt3_unix, "xrp", "XRP_ripple", 2, context, "summary6", [0.2, 0.3, 0.5]]
[
dt1_unix,
"bitcoin",
"BTC_bitcoin",
10,
context,
"summary1",
[0.2, 0.3, 0.5],
[0.2, 0.3, 0.5]
],
[
dt1_unix,
"ethereum",
"ETH_ethereum",
5,
context,
"summary2",
[0.2, 0.3, 0.5],
[0.2, 0.3, 0.5]
],
[
dt2_unix,
"san",
"SAN_santiment",
2,
context,
"summary3",
[0.4, 0.1, 0.5],
[0.4, 0.1, 0.5]
],
[dt2_unix, "boom", nil, 70, context, "summary4", [0.2, 0.1, 0.7], [0.2, 0.1, 0.7]],
[dt3_unix, "eth", "ETH_ethereum", 1, context, "summary5", [0.2, 0.3, 0.5], [0.2, 0.3, 0.5]],
[dt3_unix, "xrp", "XRP_ripple", 2, context, "summary6", [0.2, 0.3, 0.5], [0.2, 0.3, 0.5]]
]
end
end

0 comments on commit 623e35d

Please sign in to comment.