+ spark = SparkSession.builder.getOrCreate()
+ conf = spark.sparkContext.getConf()
+ print(exclude_phrases)
+ tfidf_weekly = spark.read.parquet(infile)
+
+ # create the time interval
+ if from_date is not None:
+ if type(from_date) is str:
+ from_date = datetime.fromisoformat(from_date)
+
+ tfidf_weekly = tfidf_weekly.filter(tfidf_weekly.week >= from_date)
+
+ if to_date is not None:
+ if type(to_date) is str:
+ to_date = datetime.fromisoformat(to_date)
+ tfidf_weekly = tfidf_weekly.filter(tfidf_weekly.week < to_date)
+
+ tfidf = tfidf_weekly.groupBy(["subreddit","week", term_id, term]).agg(f.sum("tf").alias("tf"))
+ tfidf = _calc_tfidf(tfidf, term_colname, tf_weight.Norm05)
+ tempdir = prep_tfidf_entries(tfidf, term_colname, min_df, max_df, included_subreddits)
+ tfidf = spark.read_parquet(tempdir.name)
+ subreddit_names = tfidf.select(['subreddit','subreddit_id_new']).distinct().toPandas()
+ subreddit_names = subreddit_names.sort_values("subreddit_id_new")
+ subreddit_names['subreddit_id_new'] = subreddit_names['subreddit_id_new'] - 1
+ return(tempdir, subreddit_names)
+
+def reindex_tfidf(infile, term_colname, min_df=None, max_df=None, included_subreddits=None, topN=500, exclude_phrases=False):
+ spark = SparkSession.builder.getOrCreate()
+ conf = spark.sparkContext.getConf()
+ print(exclude_phrases)
+
+ tfidf = spark.read.parquet(infile)
+
+ if included_subreddits is None:
+ included_subreddits = select_topN_subreddits(topN)
+ else:
+ included_subreddits = set(open(included_subreddits))
+
+ if exclude_phrases == True:
+ tfidf = tfidf.filter(~f.col(term_colname).contains("_"))
+
+ print("creating temporary parquet with matrix indicies")
+ tempdir = prep_tfidf_entries(tfidf, term_colname, min_df, max_df, included_subreddits)
+
+ tfidf = spark.read.parquet(tempdir.name)
+ subreddit_names = tfidf.select(['subreddit','subreddit_id_new']).distinct().toPandas()
+ subreddit_names = subreddit_names.sort_values("subreddit_id_new")
+ subreddit_names['subreddit_id_new'] = subreddit_names['subreddit_id_new'] - 1
+ spark.stop()
+ return (tempdir, subreddit_names)
+
+
+def similarities(infile, simfunc, term_colname, outfile, min_df=None, max_df=None, included_subreddits=None, topN=500, exclude_phrases=False, from_date=None, to_date=None, tfidf_colname='tf_idf'):
+ '''
+ tfidf_colname: set to 'relative_tf' to use normalized term frequency instead of tf-idf, which can be useful for author-based similarities.
+ '''
+ if from_date is not None or to_date is not None:
+ tempdir, subreddit_names = reindex_tfidf_time_interval(infile, term_colname=term_colname, min_df=min_df, max_df=max_df, included_subreddits=included_subreddits, topN=topN, exclude_phrases=False, from_date=from_date, to_date=to_date)
+
+ else:
+ tempdir, subreddit_names = reindex_tfidf(infile, term_colname=term_colname, min_df=min_df, max_df=max_df, included_subreddits=included_subreddits, topN=topN, exclude_phrases=False)
+
+ print("loading matrix")
+ # mat = read_tfidf_matrix("term_tfidf_entries7ejhvnvl.parquet", term_colname)
+ mat = read_tfidf_matrix(tempdir.name, term_colname, tfidf_colname)
+ print(f'computing similarities on mat. mat.shape:{mat.shape}')
+ print(f"size of mat is:{mat.data.nbytes}")
+ sims = simfunc(mat)
+ del mat
+
+ if issparse(sims):
+ sims = sims.todense()
+
+ print(f"shape of sims:{sims.shape}")
+ print(f"len(subreddit_names.subreddit.values):{len(subreddit_names.subreddit.values)}")
+ sims = pd.DataFrame(sims)
+ sims = sims.rename({i:sr for i, sr in enumerate(subreddit_names.subreddit.values)}, axis=1)
+ sims['subreddit'] = subreddit_names.subreddit.values
+
+ p = Path(outfile)
+
+ output_feather = Path(str(p).replace("".join(p.suffixes), ".feather"))
+ output_csv = Path(str(p).replace("".join(p.suffixes), ".csv"))
+ output_parquet = Path(str(p).replace("".join(p.suffixes), ".parquet"))
+
+ sims.to_feather(outfile)
+ tempdir.cleanup()
+
+def read_tfidf_matrix_weekly(path, term_colname, week, tfidf_colname='tf_idf'):
+ term = term_colname
+ term_id = term + '_id'
+ term_id_new = term + '_id_new'
+
+ dataset = ds.dataset(path,format='parquet')
+ entries = dataset.to_table(columns=[tfidf_colname,'subreddit_id_new', term_id_new],filter=ds.field('week')==week).to_pandas()
+ return(csr_matrix((entries[tfidf_colname], (entries[term_id_new]-1, entries.subreddit_id_new-1))))
+
+def read_tfidf_matrix(path, term_colname, tfidf_colname='tf_idf'):
+ term = term_colname
+ term_id = term + '_id'
+ term_id_new = term + '_id_new'