基本的に入力テキストファイルを受け取り、異なるドキュメントと比較して類似性を得るアルゴリズムを書き直そうとしています。
ここで、一致しない単語の出力を印刷し、一致しない単語を含む新しいテキスタイルを出力したいと考えています。
このコードから、「hello force」が入力であり、raw_documents に対してチェックされ、一致したドキュメントのランクが 0 から 1 の間で出力されます (単語「force」は 2 番目のドキュメントと一致し、出力は 2 番目のドキュメントにより高いランクを与えますが、「hello」はどの raw_document にもありません 一致しない単語「hello」を一致しないものとして出力したい )、しかし、私が望むのは、どの raw_document とも一致しなかった一致しない入力単語を出力することです
import gensim
import nltk
from nltk.tokenize import word_tokenize
raw_documents = ["I'm taking the show on the road",
"My socks are a force multiplier.",
"I am the barber who cuts everyone's hair who doesn't cut their own.",
"Legend has it that the mind is a mad monkey.",
"I make my own fun."]
gen_docs = [[w.lower() for w in word_tokenize(text)]
for text in raw_documents]
dictionary = gensim.corpora.Dictionary(gen_docs)
corpus = [dictionary.doc2bow(gen_doc) for gen_doc in gen_docs]
tf_idf = gensim.models.TfidfModel(corpus)
s = 0
for i in corpus:
s += len(i)
sims = gensim.similarities.Similarity('/usr/workdir/',tf_idf[corpus],
num_features=len(dictionary))
query_doc = [w.lower() for w in word_tokenize("hello force")]
query_doc_bow = dictionary.doc2bow(query_doc)
query_doc_tf_idf = tf_idf[query_doc_bow]
result = sims[query_doc_tf_idf]
print result