Skip to content

Instantly share code, notes, and snippets.

@Echocage
Last active August 29, 2015 14:13
Show Gist options
  • Save Echocage/a4de65f86fb0c2e38a8c to your computer and use it in GitHub Desktop.
Save Echocage/a4de65f86fb0c2e38a8c to your computer and use it in GitHub Desktop.
from collections import Counter
import praw
import time
female_indicators = ["I", "me", "my", "mine", "myself", "you", "your", "yours", "yourself", "she", "her", "hers",
"herself"]
male_indicators = ["we", "us", "they", "them", "he", "him", "his", "himself"]
def calculate_gender(comment_text):
# What I'm doing here is called list comprehension.
# They're like one line for loops that create a list with the results.
# Here's the breakdown: [(resulting item placed in list) (for statement) (condition)]
word_list = comment_text.split()
matched_female_words = [word for word in word_list if word in female_indicators]
matched_male_words = [word for word in word_list if word in male_indicators]
# Now we have lists of all the words matching each set of indicators, now we return their lengths
return len(matched_female_words), len(matched_male_words)
def get_gender_numbers(target_user):
user_comments = target_user.get_comments(sort="new", limit=10)
gender_counter = Counter(female=0, male=0)
for comment in user_comments:
text = comment.body
female, male = calculate_gender(text)
gender_counter.update(female=female, male=male) # increases female and male by the appropriate amounts
return gender_counter
def main():
now = time.time()
r = praw.Reddit("Reddit User Demographics by /u/MusaTheRedGuard {}".format(now))
username = input("Enter your reddit username: ")
user = r.get_redditor(username)
counter = get_gender_numbers(user)
decision = 'female' if counter['female'] >= counter['male'] else 'male'
print("The user {username} has {female_words} female comments and {male_words} male comments\n"
"Therefore the user is likely {decision}.".format(username=username, female_words=counter['female'],
male_words=counter['male'], decision=decision))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment