2021-05-03 06:39:55 +00:00
|
|
|
import pandas as pd
|
|
|
|
import fire
|
|
|
|
from pathlib import Path
|
2021-08-03 22:06:48 +00:00
|
|
|
from similarities_helper import *
|
|
|
|
#from similarities_helper import similarities, lsi_column_similarities
|
2021-05-03 06:39:55 +00:00
|
|
|
from functools import partial
|
|
|
|
|
2021-08-03 22:06:48 +00:00
|
|
|
inpath = "/gscratch/comdata/users/nathante/competitive_exclusion_reddit/data/tfidf/comment_terms_compex.parquet/"
|
|
|
|
term_colname='term'
|
|
|
|
outfile='/gscratch/comdata/users/nathante/competitive_exclusion_reddit/data/similarity/comment_terms_compex_LSI'
|
|
|
|
n_components=[10,50,100]
|
|
|
|
included_subreddits="/gscratch/comdata/users/nathante/competitive_exclusion_reddit/data/included_subreddits.txt"
|
|
|
|
n_iter=5
|
|
|
|
random_state=1968
|
|
|
|
algorithm='arpack'
|
|
|
|
topN = None
|
|
|
|
from_date=None
|
|
|
|
to_date=None
|
|
|
|
min_df=None
|
|
|
|
max_df=None
|
|
|
|
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):
|
2021-05-03 06:39:55 +00:00
|
|
|
print(n_components,flush=True)
|
|
|
|
|
2021-08-03 22:06:48 +00:00
|
|
|
|
|
|
|
if lsi_model is None:
|
|
|
|
if type(n_components) == list:
|
|
|
|
lsi_model = Path(outfile) / f'{max(n_components)}_{term_colname}_LSIMOD.pkl'
|
|
|
|
else:
|
|
|
|
lsi_model = Path(outfile) / f'{n_components}_{term_colname}_LSIMOD.pkl'
|
2021-05-03 06:39:55 +00:00
|
|
|
|
2021-08-03 22:06:48 +00:00
|
|
|
simfunc = partial(lsi_column_similarities,n_components=n_components,n_iter=n_iter,random_state=random_state,algorithm=algorithm,lsi_model_save=lsi_model)
|
|
|
|
|
|
|
|
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)
|
2021-05-03 06:39:55 +00:00
|
|
|
|
|
|
|
# change so that these take in an input as an optional argument (for speed, but also for idf).
|
2021-08-03 22:06:48 +00:00
|
|
|
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):
|
2021-05-03 06:39:55 +00:00
|
|
|
|
2021-08-03 22:06:48 +00:00
|
|
|
res = lsi_similarities(inpath,
|
2021-05-03 06:39:55 +00:00
|
|
|
'term',
|
|
|
|
outfile,
|
|
|
|
min_df,
|
|
|
|
max_df,
|
|
|
|
included_subreddits,
|
|
|
|
topN,
|
|
|
|
from_date,
|
|
|
|
to_date,
|
2021-08-03 22:06:48 +00:00
|
|
|
n_components=n_components,
|
|
|
|
algorithm = algorithm
|
2021-05-03 06:39:55 +00:00
|
|
|
)
|
2021-08-03 22:06:48 +00:00
|
|
|
return res
|
2021-05-03 06:39:55 +00:00
|
|
|
|
2021-08-03 22:06:48 +00:00
|
|
|
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):
|
|
|
|
return lsi_similarities(inpath,
|
2021-05-03 06:39:55 +00:00
|
|
|
'author',
|
|
|
|
outfile,
|
|
|
|
min_df,
|
|
|
|
max_df,
|
|
|
|
included_subreddits,
|
|
|
|
topN,
|
|
|
|
from_date=from_date,
|
|
|
|
to_date=to_date,
|
|
|
|
n_components=n_components
|
|
|
|
)
|
|
|
|
|
2021-08-03 22:06:48 +00:00
|
|
|
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):
|
|
|
|
return lsi_similarities(inpath,
|
2021-05-03 06:39:55 +00:00
|
|
|
'author',
|
|
|
|
outfile,
|
|
|
|
min_df,
|
|
|
|
max_df,
|
|
|
|
included_subreddits,
|
|
|
|
topN,
|
|
|
|
from_date=from_date,
|
|
|
|
to_date=to_date,
|
|
|
|
tfidf_colname='relative_tf',
|
2021-08-03 22:06:48 +00:00
|
|
|
n_components=n_components,
|
|
|
|
algorithm=algorithm
|
2021-05-03 06:39:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
fire.Fire({'term':term_lsi_similarities,
|
|
|
|
'author':author_lsi_similarities,
|
|
|
|
'author-tf':author_tf_similarities})
|
|
|
|
|