Reddit is a network of communities based on people’s interests. Each of these communities is called a subreddit. Users can subscribe to multiple subreddits to post, comment and interact with them.
A Reddit bot is something that automatically responds to a user’s post or automatically posts things at certain intervals. This could depend on what content the users post. It can be triggered by certain key phrases and also depends on various subreddits regarding their content.
In order to implement a Reddit bot, we will use the Python Reddit API Wrapper (PRAW). It allows us to login to the Reddit API to directly interact with the backend of the website. More information about this library can be found here – PRAW – Python Reddit API Wrapper.
Our bot will tell the similar words for a given word. We will use the enchant
module’s suggest()
method to find the similar words.
Algorithm :
- Import the modules praw and enchant.
- Create an authorized Reddit instance with valid parameters.
- Choose the subreddit where the bot is to be live on.
- Choose a word that will trigger the bot in that subreddit.
- Inspect every comment in the subreddit for the trigger phrase.
- On finding the trigger phrase, extract the word from the comment and find its similar words using the
enchant
module. - Reply to the comment with the similar words.
import praw
import enchant
client_id = ""
client_secret = ""
username = ""
password = ""
user_agent = ""
reddit = praw.Reddit(client_id = client_id,
client_secret = client_secret,
username = username,
password = password,
user_agent = user_agent)
target_sub = "GRE"
subreddit = reddit.subreddit(target_sub)
trigger_phrase = "! GfGBot"
d = enchant. Dict ( "en_US" )
for comment in subreddit.stream.comments():
if trigger_phrase in comment.body:
word = comment.body.replace(trigger_phrase, "")
reply_text = ""
similar_words = d.suggest(word)
for similar in similar_words:
reply_text + = similar + " "
comment.reply(reply_text)
|
Triggering the bot :

The bot replying with the similar words :
