time_slices=times, **dtm_defaults) mod.save(os.path.join(PKLDIR, key)) return mod # DTM Analysis nips = NipsData() dat = nips.load_data(sample_frac=1) sections = ['title', 'abstract'] ncomps = dtm_defaults['num_topics'] dat = nips.combined_sections(sections, data=dat) yrs, cnts = papers_per_year(nips.raw) sname = get_save_name(sections) docs = lda_get_corpus(dat, name=sname, save=True) d, bow = lda_get_dictionary(dat, name=sname, save=True) ## mod = dtm_run(dat, cnts, dtm_path, name=sname, save=True) mod = DtmModel.load(os.path.join(PKLDIR, 'lda_dtm_15_title_abstract')) def print_dtm_top_words_for_year(model, years, n_topics, n_words): """Print top n_words from top n_topics for year in years.""" print(f"Top {n_words} from top {n_topics} for year(s) {years}:") yrs = enumerate(range(1987, 2018)) inds = [(i,yr) for i, yr in yrs if yr in years] for i, yr in inds: print(f"Year {yr}:") for topic, words in enumerate(model.dtm_coherence(i, n_words)[:n_topics]): print(f" Topic #{topic}: " + ', '.join(words)) def dtm_coherence(model, corpus, d, year): """Get coherence for DTM model at year."""
def load_model(self): # Load model self.model = DtmModelClass.load(self.output_file_path) print(f"Model loaded from {self.output_file_path}")
def loadDTM(amounttopics): modelAmountTopics = DtmModel.load(f'./Models/model{amounttopics}Topics') return modelAmountTopics