J'ai essayé le chapitre From Strings to Vectors.
La partie stoplist exclut les mots inutiles.
Qu'est-ce qu'un mot d'arrêt Mots qui doivent être exclus de la cible de recherche afin d'améliorer la précision de la recherche car cela nécessite trop de recherches. Les mots fonctionnels tels que les mots auxiliaires et les verbes auxiliaires (comme "ha", "no", "desu", "masu" en japonais, "the", "of", "is" en anglais) sont presque toujours applicables. ..
sample.py
import logging
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
from gensim import corpora, models, similarities
documents = ["Human machine interface for lab abc computer applications",
"A survey of user opinion of computer system response time",
"The EPS user interface management system",
"System and human system engineering testing of EPS",
"Relation of user perceived response time to error measurement",
"The generation of random binary unordered trees",
"The intersection graph of paths in trees",
"Graph minors IV Widths of trees and well quasi ordering",
"Graph minors A survey"]
# remove common words and tokenize
stoplist = set('for a of the and to in'.split())
texts = [[word for word in document.lower().split() if word not in stoplist]
for document in documents]
# remove words that appear only once
from collections import defaultdict
frequency = defaultdict(int)
# print(texts)
for text in texts:
for token in text:
frequency[token] += 1
texts = [[token for token in text if frequency[token] > 1]
for text in texts]
# from pprint import pprint # pretty-printer
# pprint(texts)
dictionary = corpora.Dictionary(texts)
# print(dictionary)
#Sortie avec identifiant
# print(dictionary.token2id)
#Convertir en vecteur de phrase
corpus = [dictionary.doc2bow(text) for text in texts]
print(corpus)
Tutoriel officiel https://radimrehurek.com/gensim/tut1.html
Recommended Posts