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

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