1 from pyspark.sql import functions as f
2 from pyspark.sql import SparkSession
3 from pyspark.sql import Window
4 from pyspark.mllib.linalg.distributed import RowMatrix, CoordinateMatrix
9 from itertools import islice
10 from pathlib import Path
11 from similarities_helper import cosine_similarities
13 spark = SparkSession.builder.getOrCreate()
14 conf = spark.sparkContext.getConf()
16 df = spark.read.parquet("/gscratch/comdata/output/reddit_comments_by_subreddit.parquet")
19 df = df.filter(~df.subreddit.like("u_%"))
21 df = df.groupBy('subreddit').agg(f.count('id').alias("n_comments"))
23 win = Window.orderBy(f.col('n_comments').desc())
24 df = df.withColumn('comments_rank',f.rank().over(win))
28 df = df.sort_values("n_comments")
30 df.to_csv('/gscratch/comdata/users/nathante/cdsc-reddit/subreddits_by_num_comments.csv',index=False)