stop_words = get_stop_words('fr') stop_words.extend(ADDITIONAL_STOP_WORDS) # Un HashingVectorizer découpe un texte en une liste de mots, et renvoie une matrice où chaque ligne correspond à # un document et chaque colonne à un mot # doc : http://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.HashingVectorizer.html hasher = HashingVectorizer(strip_accents='unicode', stop_words=stop_words, norm=None) # Le HashingVectorizer ne permet pas la stemmisation des mots durant le processus de tokenisation. # On va donc lui dire de le faire quand même. # Pour cela, on récupère sa fonction de tokenisation, que l'on va améliorer, puis lui réinjecter: original_tokenizer = hasher.build_tokenizer( ) # recuperation de la fonction de tokenisation stemmer = SnowballStemmer("french", ignore_stopwords=True) def new_tokenizer(text): words = original_tokenizer(text) stemmed_words = [stemmer.stem(w) for w in words] return stemmed_words hasher = HashingVectorizer( tokenizer= new_tokenizer, # création d'un nouveau hasher avec injection de notre tokenizer amélioré strip_accents='unicode', stop_words=stop_words, norm=None) # Un pipeline est juste une liste dans laquelle on place différents processeurs.
yield doc ############################################################################### # Main # ---- # # Create the vectorizer and limit the number of features to a reasonable # maximum N_FEATURES = 2 ** 18 vectorizer = HashingVectorizer(decode_error='ignore', n_features=N_FEATURES, non_negative=True) tokenizer = vectorizer.build_tokenizer() preprocessor = vectorizer.build_preprocessor() stop_words = vectorizer.get_stop_words() def tokenize(text): return vectorizer._word_ngrams(tokenizer(preprocessor(vectorizer.decode(text))), stop_words) # Iterator over parsed Reuters SGML files. data_stream = stream_reuters_documents() # We learn a binary classification between the "acq" class and all the others. # "acq" was chosen as it is more or less evenly distributed in the Reuters # files. For other datasets, one should take care of creating a test set with # a realistic portion of positive instances.