]> code.communitydata.science - cdsc_reddit.git/blob - author_cosine_similarity.py
7b2a766d24461269061a244f0350725ef8a15936
[cdsc_reddit.git] / author_cosine_similarity.py
1 from pyspark.sql import functions as f
2 from pyspark.sql import SparkSession
3 from pyspark.sql import Window
4 import numpy as np
5 import pyarrow
6 import pandas as pd
7 import fire
8 from itertools import islice
9 from pathlib import Path
10 from similarities_helper import cosine_similarities
11
12 spark = SparkSession.builder.getOrCreate()
13 conf = spark.sparkContext.getConf()
14
15 # outfile = '/gscratch/comdata/users/nathante/test_similarities_500.feather'; min_df = None; included_subreddits=None; similarity_threshold=0;
16 def author_cosine_similarities(outfile, min_df = None, included_subreddits=None, similarity_threshold=0, topN=500, exclude_phrases=True):
17     '''
18     Compute similarities between subreddits based on tfi-idf vectors of author comments
19     
20     included_subreddits : string
21         Text file containing a list of subreddits to include (one per line) if included_subreddits is None then do the top 500 subreddits
22
23     similarity_threshold : double (default = 0)
24         set > 0 for large numbers of subreddits to get an approximate solution using the DIMSUM algorithm
25 https://stanford.edu/~rezab/papers/dimsum.pdf. If similarity_threshold=0 we get an exact solution using an O(N^2) algorithm.
26
27     min_df : int (default = 0.1 * (number of included_subreddits)
28          exclude terms that appear in fewer than this number of documents.
29
30     outfile: string
31          where to output csv and feather outputs
32 '''
33
34     print(outfile)
35     print(exclude_phrases)
36
37     tfidf = spark.read.parquet('/gscratch/comdata/users/nathante/subreddit_tfidf_authors.parquet_test1/part-00000-107cee94-92d8-4265-b804-40f1e7f1aaf2-c000.snappy.parquet')
38
39     if included_subreddits is None:
40         included_subreddits = list(islice(open("/gscratch/comdata/users/nathante/cdsc-reddit/top_25000_subs_by_comments.txt"),topN))
41         included_subreddits = {s.strip('\n') for s in included_subreddits}
42
43     else:
44         included_subreddits = set(open(included_subreddits))
45
46     sim_dist, tfidf = cosine_similarities(tfidf, 'author', min_df, included_subreddits, similarity_threshold)
47
48     p = Path(outfile)
49
50     output_feather =  Path(str(p).replace("".join(p.suffixes), ".feather"))
51     output_csv =  Path(str(p).replace("".join(p.suffixes), ".csv"))
52     output_parquet =  Path(str(p).replace("".join(p.suffixes), ".parquet"))
53     sim_dist = sim_dist.entries.toDF()
54
55     sim_dist = sim_dist.repartition(1)
56     sim_dist.write.parquet(str(output_parquet),mode='overwrite',compression='snappy')
57     
58     spark.stop()
59
60     #instead of toLocalMatrix() why not read as entries and put strait into numpy
61     sim_entries = pd.read_parquet(output_parquet)
62
63     df = tfidf.select('subreddit','subreddit_id_new').distinct().toPandas()
64     df['subreddit_id_new'] = df['subreddit_id_new'] - 1
65     df = df.sort_values('subreddit_id_new').reset_index(drop=True)
66     df = df.set_index('subreddit_id_new')
67
68     similarities = sim_entries.join(df, on='i')
69     similarities = similarities.rename(columns={'subreddit':"subreddit_i"})
70     similarities = similarities.join(df, on='j')
71     similarities = similarities.rename(columns={'subreddit':"subreddit_j"})
72
73     similarities.write_feather(output_feather)
74     similarities.write_csv(output_csv)
75     return similarities
76     
77 if __name__ == '__main__':
78     fire.Fire(term_cosine_similarities)

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