]> code.communitydata.science - cdsc_reddit.git/blob - similarities/tfidf.py
Merge branch 'master' of code:cdsc_reddit into excise_reindex
[cdsc_reddit.git] / similarities / tfidf.py
1 import fire
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
5
6 def _tfidf_wrapper(func, inpath, outpath, topN, term_colname, exclude, included_subreddits):
7     spark = SparkSession.builder.getOrCreate()
8
9     df = spark.read.parquet(inpath)
10
11     df = df.filter(~ f.col(term_colname).isin(exclude))
12
13     if included_subreddits is not None:
14         include_subs = set(map(str.strip,open(included_subreddits)))
15     else:
16         include_subs = select_topN_subreddits(topN)
17
18     dfwriter = func(df, include_subs, term_colname)
19
20     dfwriter.parquet(outpath,mode='overwrite',compression='snappy')
21     spark.stop()
22
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)
25
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)
28
29 def tfidf_authors(outpath='/gscratch/comdata/output/reddit_similarity/tfidf/comment_authors.parquet',
30                   topN=None,
31                   included_subreddits=None):
32
33     return tfidf("/gscratch/comdata/output/reddit_ngrams/comment_authors.parquet",
34                  outpath,
35                  topN,
36                  'author',
37                  ['[deleted]','AutoModerator'],
38                  included_subreddits=included_subreddits
39                  )
40
41 def tfidf_terms(outpath='/gscratch/comdata/output/reddit_similarity/tfidf/comment_terms.parquet',
42                 topN=None,
43                 included_subreddits=None):
44
45     return tfidf("/gscratch/comdata/output/reddit_ngrams/comment_terms.parquet",
46                  outpath,
47                  topN,
48                  'term',
49                  [],
50                  included_subreddits=included_subreddits
51                  )
52
53 def tfidf_authors_weekly(outpath='/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_authors.parquet',
54                          topN=None,
55                          included_subreddits=None):
56
57     return tfidf_weekly("/gscratch/comdata/output/reddit_ngrams/comment_authors.parquet",
58                         outpath,
59                         topN,
60                         'author',
61                         ['[deleted]','AutoModerator'],
62                         included_subreddits=included_subreddits
63                         )
64
65 def tfidf_terms_weekly(outpath='/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_terms.parquet',
66                        topN=None,
67                        included_subreddits=None):
68
69
70     return tfidf_weekly("/gscratch/comdata/output/reddit_ngrams/comment_terms.parquet",
71                         outpath,
72                         topN,
73                         'term',
74                         [],
75                         included_subreddits=included_subreddits
76                         )
77
78
79 if __name__ == "__main__":
80     fire.Fire({'authors':tfidf_authors,
81                'terms':tfidf_terms,
82                'authors_weekly':tfidf_authors_weekly,
83                'terms_weekly':tfidf_terms_weekly})

Community Data Science Collective || Want to submit a patch?