I tried the Chapter From Strings to Vectors.
The stoplist part excludes unnecessary words.
What is a stop word Words that have to be excluded from the search target in order to improve the search accuracy because it takes too many searches. Function words such as particles and auxiliary verbs (such as "ha", "no", "desu", "masu" in Japanese, and "the", "of", "is" in English) are almost always applicable. ..
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)
#Output with id
# print(dictionary.token2id)
#Convert to sentence vector
corpus = [dictionary.doc2bow(text) for text in texts]
print(corpus)
Official tutorial https://radimrehurek.com/gensim/tut1.html
Recommended Posts