<jupyter_start><jupyter_text>http://www.storybench.org/how-to-scrape-reddit-with-python/<jupyter_code>import praw
import pandas as pd
import os
from time import sleep
import random
reddit = praw.Reddit(client_id=os.environ['REDDIT_PERSONAL_USE_SCRIPT'],
                     client_secret=os.environ['REDDIT_SECRET'],
                     user_agent=os.environ['REDDIT_APP_NAME'])
cybercrime_subreddit = reddit.subreddit('cybercrime')
crime_subreddit =  reddit.subreddit('crime')
cybercrime_posts = list((reddit.subreddit('all').search('cybercrime', limit=None)))
random_posts = list((reddit.subreddit('all').new(limit=None)))


SJA HERE



need to add in target: cybercrime if in search, otherwise not-cybercrime
    
    
    
    
    
all_posts = list(cybercrime_posts) + list(random_posts)
num_posts = len(all_posts)
random.shuffle(all_posts)

train_posts = all_posts[:int(num_posts/2)]
test_posts = all_posts[int(num_posts/2):]

# test_train['data'] = [post.selftext for post in all_posts]
# test_train['target'] = [post.subreddit.display_name for post in all_posts]<jupyter_output><empty_output><jupyter_text>Build a Classifier<jupyter_code>from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import SGDClassifier
from sklearn.pipeline import Pipeline
text_clf = Pipeline([
    ('vect', CountVectorizer()),
    ('tfidf', TfidfTransformer()),
    ('clf', MultinomialNB()),
])

text_clf.fit([post.selftest for post in train_posts],
             [post.selftest for post in train_posts])
[post.subreddit.display_name for post in train_posts][:20]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11511627906976744, 'fraction_numerical': 0.002325581395348837, 'mean_word_length': 5.5437262357414445, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '468161', 'license': 'MIT', 'n_tokens_mistral': 602, 'n_tokens_neox': 596, 'n_words': 106, 'path': 'notebooks/Scrape Reddit.ipynb', 'repo_name': 'cds-snc/rac-data-exploration', 'size': '1720'}