In this post, we will semi-randomly select a tweet based on probability, giving a weight to tweets that have been tweeted less frequently.
The tweets have been fetched from a DynamoDb table which have an attribute called TweetCount. We will be using this for our algorithm.
In our TwitterBotService project, create an interface called ITweetSelector and add the following method:
If all TweetCounts are equal then we calculate a single tweet's probability with:
TweetProbability = (TweetCount / TweetCountTotal) * 100
However, if we have multiple TweetCounts we then we want to give the highest probability to the lowest TweetCount:
To do this, for each Tweet we:
NewTweetCountTotal += TweetCountTotal - TweetCount
This represents our new total where the Tweet with the lowest TweetCount has the highest ratio of the total.
We then calculate a single tweet's probability with:
TweetProbability = ((TweetCountTotal - TweetCount) / NewTweetCountTotal) * 100
This formula gives the Tweets with the lowest TweetCount, the highest probability of being selected.
We then select a random number between 1 and 100.
We share the Tweets with their corresponding probability ratios that's shared within that 1 to 100% range.
The number that falls within the range of a tweets share of the 1 to 100% range will be the tweet selected.
For example:
We have three Tweets and the number of times they have been tweeted:
Tweet TweetCount
A 5
B 16
C 24
TweetCountTotal = 45
First, let's calculate the new total:
NewTweetCountTotal += TweetCountTotal - TweetCount
A = 45 - 5 = 40
B = 45 - 16 = 29
C = 45 - 24 = 21
NewTweetCountTotal = 90
Using the formula:
TweetProbability = ((TweetCountTotal - TweetCount) / NewTweetCountTotal) * 100
A probability = ((45 - 5) / 90) * 100 = 44.44%
B probability = ((45 - 16) / 90) * 100 = 32.22%
C probability = ((45 - 24) / 90) * 100 = 23.33%
We can see that Tweet A, which only had 5 previous tweets, now has the highest probability of being selected with a 44.44% chance.
<< DynamoDb Table of Contents Reading Images from S3 >>
No comments:
Post a Comment