2020-12-09 01:32:20 +00:00
|
|
|
import fire
|
|
|
|
from pyspark.sql import SparkSession
|
|
|
|
from pyspark.sql import functions as f
|
2022-01-19 23:05:49 +00:00
|
|
|
from cdsc_ecology_utils.similarity.similarity_functions import tfidf_dataset, \
|
|
|
|
build_weekly_tfidf_dataset, select_topN_communities
|
2020-12-09 01:32:20 +00:00
|
|
|
|
2021-02-23 02:38:34 +00:00
|
|
|
def _tfidf_wrapper(func, inpath, outpath, topN, term_colname, exclude, included_subreddits):
|
2022-01-19 23:05:49 +00:00
|
|
|
spark = SparkSession.builder.getOrCreate()
|
2020-12-09 01:32:20 +00:00
|
|
|
|
|
|
|
df = spark.read.parquet(inpath)
|
|
|
|
|
|
|
|
df = df.filter(~ f.col(term_colname).isin(exclude))
|
|
|
|
|
2021-02-23 02:38:34 +00:00
|
|
|
if included_subreddits is not None:
|
2021-08-03 22:06:48 +00:00
|
|
|
include_subs = set(map(str.strip,open(included_subreddits)))
|
2021-02-23 02:38:34 +00:00
|
|
|
else:
|
2022-01-19 23:05:49 +00:00
|
|
|
include_subs = select_topN_communities(topN)
|
2020-12-09 01:32:20 +00:00
|
|
|
|
2021-05-03 06:39:55 +00:00
|
|
|
dfwriter = func(df, include_subs, term_colname)
|
2020-12-09 01:32:20 +00:00
|
|
|
|
2021-05-03 06:39:55 +00:00
|
|
|
dfwriter.parquet(outpath,mode='overwrite',compression='snappy')
|
2020-12-09 01:32:20 +00:00
|
|
|
spark.stop()
|
|
|
|
|
2021-02-23 02:38:34 +00:00
|
|
|
def tfidf(inpath, outpath, topN, term_colname, exclude, included_subreddits):
|
2021-08-03 22:06:48 +00:00
|
|
|
return _tfidf_wrapper(tfidf_dataset, inpath, outpath, topN, term_colname, exclude, included_subreddits)
|
2020-12-09 01:32:20 +00:00
|
|
|
|
2021-04-06 06:21:06 +00:00
|
|
|
def tfidf_weekly(inpath, outpath, topN, term_colname, exclude, included_subreddits):
|
|
|
|
return _tfidf_wrapper(build_weekly_tfidf_dataset, inpath, outpath, topN, term_colname, exclude, included_subreddits)
|
2020-12-09 01:32:20 +00:00
|
|
|
|
2021-08-12 05:48:33 +00:00
|
|
|
def tfidf_authors(inpath="/gscratch/comdata/output/reddit_ngrams/comment_authors.parquet",
|
|
|
|
outpath='/gscratch/comdata/output/reddit_similarity/tfidf/comment_authors.parquet',
|
2021-08-03 22:06:48 +00:00
|
|
|
topN=None,
|
|
|
|
included_subreddits=None):
|
2020-12-09 01:32:20 +00:00
|
|
|
|
2021-08-12 05:48:33 +00:00
|
|
|
return tfidf(inpath,
|
2020-12-09 01:32:20 +00:00
|
|
|
outpath,
|
|
|
|
topN,
|
|
|
|
'author',
|
2021-02-23 02:38:34 +00:00
|
|
|
['[deleted]','AutoModerator'],
|
2021-08-03 22:06:48 +00:00
|
|
|
included_subreddits=included_subreddits
|
2020-12-09 01:32:20 +00:00
|
|
|
)
|
|
|
|
|
2021-08-12 05:48:33 +00:00
|
|
|
def tfidf_terms(inpath="/gscratch/comdata/output/reddit_ngrams/comment_terms.parquet",
|
|
|
|
outpath='/gscratch/comdata/output/reddit_similarity/tfidf/comment_terms.parquet',
|
2021-08-03 22:06:48 +00:00
|
|
|
topN=None,
|
|
|
|
included_subreddits=None):
|
2020-12-09 01:32:20 +00:00
|
|
|
|
2021-08-12 05:48:33 +00:00
|
|
|
return tfidf(inpath,
|
2020-12-09 01:32:20 +00:00
|
|
|
outpath,
|
|
|
|
topN,
|
|
|
|
'term',
|
2021-02-23 02:38:34 +00:00
|
|
|
[],
|
2021-08-03 22:06:48 +00:00
|
|
|
included_subreddits=included_subreddits
|
2020-12-09 01:32:20 +00:00
|
|
|
)
|
|
|
|
|
2021-08-12 05:48:33 +00:00
|
|
|
def tfidf_authors_weekly(inpath="/gscratch/comdata/output/reddit_ngrams/comment_authors.parquet",
|
|
|
|
outpath='/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_authors.parquet',
|
2021-08-03 22:06:48 +00:00
|
|
|
topN=None,
|
2021-04-26 18:16:28 +00:00
|
|
|
included_subreddits=None):
|
2020-12-09 01:32:20 +00:00
|
|
|
|
2021-08-12 05:48:33 +00:00
|
|
|
return tfidf_weekly(inpath,
|
2021-02-23 02:38:34 +00:00
|
|
|
outpath,
|
|
|
|
topN,
|
|
|
|
'author',
|
|
|
|
['[deleted]','AutoModerator'],
|
2021-08-03 22:06:48 +00:00
|
|
|
included_subreddits=included_subreddits
|
2021-02-23 02:38:34 +00:00
|
|
|
)
|
2020-12-09 01:32:20 +00:00
|
|
|
|
2021-08-12 05:48:33 +00:00
|
|
|
def tfidf_terms_weekly(inpath="/gscratch/comdata/output/reddit_ngrams/comment_terms.parquet",
|
|
|
|
outpath='/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_terms.parquet',
|
2021-08-03 22:13:21 +00:00
|
|
|
topN=None,
|
2021-04-26 18:16:28 +00:00
|
|
|
included_subreddits=None):
|
2020-12-09 01:32:20 +00:00
|
|
|
|
2021-02-23 00:03:48 +00:00
|
|
|
|
2021-08-12 05:48:33 +00:00
|
|
|
return tfidf_weekly(inpath,
|
2021-02-23 00:03:48 +00:00
|
|
|
outpath,
|
|
|
|
topN,
|
|
|
|
'term',
|
2021-02-23 02:38:34 +00:00
|
|
|
[],
|
2021-04-26 18:16:28 +00:00
|
|
|
included_subreddits=included_subreddits
|
2021-02-23 00:03:48 +00:00
|
|
|
)
|
2020-12-09 01:32:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
fire.Fire({'authors':tfidf_authors,
|
|
|
|
'terms':tfidf_terms,
|
|
|
|
'authors_weekly':tfidf_authors_weekly,
|
|
|
|
'terms_weekly':tfidf_terms_weekly})
|