2 from pyspark.sql import SparkSession
3 from pyspark.sql import functions as f
4 from similarities_helper import 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()y
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 = set(map(str.strip,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(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(inpath="/gscratch/comdata/output/reddit_ngrams/comment_authors.parquet",
30 outpath='/gscratch/comdata/output/reddit_similarity/tfidf/comment_authors.parquet',
32 included_subreddits=None):
38 ['[deleted]','AutoModerator'],
39 included_subreddits=included_subreddits
42 def tfidf_terms(inpath="/gscratch/comdata/output/reddit_ngrams/comment_terms.parquet",
43 outpath='/gscratch/comdata/output/reddit_similarity/tfidf/comment_terms.parquet',
45 included_subreddits=None):
52 included_subreddits=included_subreddits
55 def tfidf_authors_weekly(inpath="/gscratch/comdata/output/reddit_ngrams/comment_authors.parquet",
56 outpath='/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_authors.parquet',
58 included_subreddits=None):
60 return tfidf_weekly(inpath,
64 ['[deleted]','AutoModerator'],
65 included_subreddits=included_subreddits
68 def tfidf_terms_weekly(inpath="/gscratch/comdata/output/reddit_ngrams/comment_terms.parquet",
69 outpath='/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_terms.parquet',
71 included_subreddits=None):
74 return tfidf_weekly(inpath,
79 included_subreddits=included_subreddits
83 if __name__ == "__main__":
84 fire.Fire({'authors':tfidf_authors,
86 'authors_weekly':tfidf_authors_weekly,
87 'terms_weekly':tfidf_terms_weekly})