<s> import sys <EOL> sys . path . append ( "<STR_LIT>" ) <EOL> from sklearn . feature_extraction . text import CountVectorizer <EOL> from sklearn . feature_extraction . text import TfidfTransformer <EOL> from sklearn import decomposition <EOL> import jieba <EOL> import time <EOL> import glob <EOL> import sys <EOL> import os <EOL> import random <EOL> if len ( sys . argv ) < <NUM_LIT:2> : <EOL> print ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> n_topic = <NUM_LIT:10> <EOL> n_top_words = <NUM_LIT> <EOL> if len ( sys . argv ) > <NUM_LIT:2> : <EOL> n_topic = int ( sys . argv [ <NUM_LIT:2> ] ) <EOL> if len ( sys . argv ) > <NUM_LIT:3> : <EOL> n_top_words = int ( sys . argv [ <NUM_LIT:3> ] ) <EOL> count_vect = CountVectorizer ( ) <EOL> docs = [ ] <EOL> pattern = os . path . join ( sys . argv [ <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" + pattern ) <EOL> for f_name in glob . glob ( pattern ) : <EOL> with open ( f_name ) as f : <EOL> print ( "<STR_LIT>" , f_name ) <EOL> for line in f : <EOL> words = "<STR_LIT:U+0020>" . join ( jieba . cut ( line ) ) <EOL> docs . append ( words ) <EOL> random . shuffle ( docs ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> counts = count_vect . fit_transform ( docs ) <EOL> tfidf = TfidfTransformer ( ) . fit_transform ( counts ) <EOL> print ( tfidf . shape ) <EOL> t0 = time . time ( ) <EOL> print ( "<STR_LIT>" ) <EOL> nmf = decomposition . NMF ( n_components = n_topic ) . fit ( tfidf ) <EOL> print ( "<STR_LIT>" % ( time . time ( ) - t0 ) ) <EOL> feature_names = count_vect . get_feature_names ( ) <EOL> for topic_idx , topic in enumerate ( nmf . components_ ) : <EOL> print ( "<STR_LIT>" % topic_idx ) <EOL> print ( "<STR_LIT:U+0020>" . join ( [ feature_names [ i ] <EOL> for i in topic . argsort ( ) [ : - n_top_words - <NUM_LIT:1> : - <NUM_LIT:1> ] ] ) ) <EOL> print ( "<STR_LIT>" ) </s>
