]> code.communitydata.science - cdsc_reddit.git/blob - similarities/lsi_similarities.py
update pushshift dumps.
[cdsc_reddit.git] / similarities / lsi_similarities.py
1 import pandas as pd
2 import fire
3 from pathlib import Path
4 from similarities_helper import *
5 #from similarities_helper import similarities, lsi_column_similarities
6 from functools import partial
7
8 inpath = "/gscratch/comdata/users/nathante/competitive_exclusion_reddit/data/tfidf/comment_terms_compex.parquet/"
9 term_colname='term'
10 outfile='/gscratch/comdata/users/nathante/competitive_exclusion_reddit/data/similarity/comment_terms_compex_LSI'
11 n_components=[10,50,100]
12 included_subreddits="/gscratch/comdata/users/nathante/competitive_exclusion_reddit/data/included_subreddits.txt"
13 n_iter=5
14 random_state=1968
15 algorithm='arpack'
16 topN = None
17 from_date=None
18 to_date=None
19 min_df=None
20 max_df=None
21 def lsi_similarities(inpath, term_colname, outfile, min_df=None, max_df=None, included_subreddits=None, topN=None, from_date=None, to_date=None, tfidf_colname='tf_idf',n_components=100,n_iter=5,random_state=1968,algorithm='arpack',lsi_model=None):
22     print(n_components,flush=True)
23
24         
25     if lsi_model is None:
26         if type(n_components) == list:
27             lsi_model = Path(outfile) / f'{max(n_components)}_{term_colname}_LSIMOD.pkl'
28         else:
29             lsi_model = Path(outfile) / f'{n_components}_{term_colname}_LSIMOD.pkl'
30
31     simfunc = partial(lsi_column_similarities,n_components=n_components,n_iter=n_iter,random_state=random_state,algorithm=algorithm,lsi_model_save=lsi_model)
32
33     return similarities(inpath=inpath, simfunc=simfunc, term_colname=term_colname, outfile=outfile, min_df=min_df, max_df=max_df, included_subreddits=included_subreddits, topN=topN, from_date=from_date, to_date=to_date, tfidf_colname=tfidf_colname)
34
35 # change so that these take in an input as an optional argument (for speed, but also for idf).
36 def term_lsi_similarities(inpath='/gscratch/comdata/output/reddit_similarity/tfidf/comment_terms_100k.parquet',outfile=None, min_df=None, max_df=None, included_subreddits=None, topN=None, from_date=None, to_date=None, algorithm='arpack', n_components=300,n_iter=5,random_state=1968):
37
38     res =  lsi_similarities(inpath,
39                             'term',
40                             outfile,
41                             min_df,
42                             max_df,
43                             included_subreddits,
44                             topN,
45                             from_date,
46                             to_date,
47                             n_components=n_components,
48                             algorithm = algorithm
49                             )
50     return res
51
52 def author_lsi_similarities(inpath='/gscratch/comdata/output/reddit_similarity/tfidf/comment_authors_100k.parquet',outfile=None, min_df=2, max_df=None, included_subreddits=None, topN=None, from_date=None, to_date=None,algorithm='arpack',n_components=300,n_iter=5,random_state=1968):
53     return lsi_similarities(inpath,
54                             'author',
55                             outfile,
56                             min_df,
57                             max_df,
58                             included_subreddits,
59                             topN,
60                             from_date=from_date,
61                             to_date=to_date,
62                             n_components=n_components
63                                )
64
65 def author_tf_similarities(inpath='/gscratch/comdata/output/reddit_similarity/tfidf/comment_authors_100k.parquet',outfile=None, min_df=2, max_df=None, included_subreddits=None, topN=None, from_date=None, to_date=None,n_components=300,n_iter=5,random_state=1968):
66     return lsi_similarities(inpath,
67                             'author',
68                             outfile,
69                             min_df,
70                             max_df,
71                             included_subreddits,
72                             topN,
73                             from_date=from_date,
74                             to_date=to_date,
75                             tfidf_colname='relative_tf',
76                             n_components=n_components,
77                             algorithm=algorithm
78                             )
79
80
81 if __name__ == "__main__":
82     fire.Fire({'term':term_lsi_similarities,
83                'author':author_lsi_similarities,
84                'author-tf':author_tf_similarities})
85

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