2 from pyspark.sql import SparkSession
3 from pyspark.sql import functions as f
4 from similarities_helper import build_tfidf_dataset, build_weekly_tfidf_dataset, select_topN_subreddits
6 def _tfidf_wrapper(func, inpath, outpath, topN, term_colname, exclude, included_subreddits):
7 spark = SparkSession.builder.getOrCreate()
9 df = spark.read.parquet(inpath)
11 df = df.filter(~ f.col(term_colname).isin(exclude))
13 if included_subreddits is not None:
14 include_subs = list(open(included_subreddits))
16 include_subs = select_topN_subreddits(topN)
18 dfwriter = func(df, include_subs, term_colname)
20 dfwriter.parquet(outpath,mode='overwrite',compression='snappy')
23 def tfidf(inpath, outpath, topN, term_colname, exclude, included_subreddits):
24 return _tfidf_wrapper(build_tfidf_dataset, inpath, outpath, topN, term_colname, exclude, included_subreddits)
26 def tfidf_weekly(inpath, outpath, topN, term_colname, exclude, included_subreddits):
27 return _tfidf_wrapper(build_weekly_tfidf_dataset, inpath, outpath, topN, term_colname, exclude, included_subreddits)
29 def tfidf_authors(outpath='/gscratch/comdata/output/reddit_similarity/tfidf/comment_authors.parquet',
32 return tfidf("/gscratch/comdata/output/reddit_ngrams/comment_authors.parquet",
36 ['[deleted]','AutoModerator'],
37 included_subreddits=None
40 def tfidf_terms(outpath='/gscratch/comdata/output/reddit_similarity/tfidf/comment_terms.parquet',
43 return tfidf("/gscratch/comdata/output/reddit_ngrams/comment_terms.parquet",
48 included_subreddits=None
51 def tfidf_authors_weekly(outpath='/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_authors.parquet',
54 return tfidf_weekly("/gscratch/comdata/output/reddit_ngrams/comment_authors.parquet",
58 ['[deleted]','AutoModerator'],
59 included_subreddits=None
62 def tfidf_terms_weekly(outpath='/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_terms.parquet',
66 return tfidf_weekly("/gscratch/comdata/output/reddit_ngrams/comment_terms.parquet",
71 included_subreddits=None
75 if __name__ == "__main__":
76 fire.Fire({'authors':tfidf_authors,
78 'authors_weekly':tfidf_authors_weekly,
79 'terms_weekly':tfidf_terms_weekly})