-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
73 lines (52 loc) · 1.88 KB
/
Copy pathmain.py
File metadata and controls
73 lines (52 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# coding: utf-8
import numpy as np
import pandas as pd
from tqdm import tqdm
from sklearn.decomposition import NMF, LatentDirichletAllocation
from bokeh.io import output_file, show, output_notebook
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, HoverTool, TapTool, OpenURL, Title, WheelZoomTool
from bokeh.layouts import row,column, widgetbox
from bokeh.models import FactorRange
from bokeh.models.widgets import Select
from bokeh.embed import components
from google_topic_utilities import *
from corpus_building import *
from utils import *
# -------------- Create corpus (Web scraping Google results) --------------
indir = ''
out_dir = ''
## Load query data
search_data = load_search_jsons(indir)
## Build corpus
search_data = search_data.drop_duplicates()
queries = search_data['query_text'].tolist()
print len(queries)
build_corpus(10, queries,outdir)
# -------------- Load, clean and prepare corpus data --------------
## Load data
indir = "Recherches/NLP/"
corpus_dict = load_corpus(indir)
## Process data
doc_clean = clean_text_data(corpus_dict)
## Create Document-Term Dataframe
tf_thresh = 10
tfidf_thresh = 0
doc_term_df = make_doc_term_df(doc_clean,'tfidf', tf_thresh, tfidf_thresh)
print "{} words from {} queries are included in the analysis".format(doc_term_df.shape[1],doc_term_df.shape[0])
# -------------- Apply Topic Detection Algorithm --------------
# Set Params
no_topics = 20
top_words = 10
# Run NMF
nmf = NMF(n_components=no_topics, random_state=42)
nmf.fit_transform(doc_term_df.values)
# Get all related matrices
V_df, W_df, H_df = get_NMF_df(doc_term_df,nmf)
# -------------- Display results --------------
# List
display_topics(nmf, doc_term_df.columns.tolist(), top_words)
# Word clouds
word_cloud_words = 100
dims = (1200,800) # Attention modif values in color_func
topics_wordcloud(nmf, doc_term_df.columns.tolist(), word_cloud_words, dims)