他の回答がないので私の解決策を掲載します。我々は次のような状況にあるとしましょう:
import gensim
from gensim import models
from gensim import corpora
from gensim import similarities
from nltk.tokenize import word_tokenize
import pandas as pd
# routines:
text = "I work on natural language processing and I want to figure out how does gensim work"
text2 = "I love computer science and I code in Python"
dat = pd.Series([text,text2])
dat = dat.apply(lambda x: str(x).lower())
dat = dat.apply(lambda x: word_tokenize(x))
dictionary = corpora.Dictionary(dat)
corpus = [dictionary.doc2bow(doc) for doc in dat]
tfidf = models.TfidfModel(corpus)
corpus_tfidf = tfidf[corpus]
#Query:
query_text = "I love icecream and gensim"
query_text = query_text.lower()
query_text = word_tokenize(query_text)
vec_bow = dictionary.doc2bow(query_text)
vec_tfidf = tfidf[vec_bow]
我々が見れば:
print(vec_bow)
[(0, 1), (7, 1), (12, 1), (15, 1)]
と:FYI
print(tfidf[vec_bow])
[(12, 0.7071067811865475), (15, 0.7071067811865475)]
idとDOC:
print(dictionary.items())
[(0, u'and'),
(1, u'on'),
(8, u'processing'),
(3, u'natural'),
(4, u'figure'),
(5, u'language'),
(9, u'how'),
(7, u'i'),
(14, u'code'),
(19, u'in'),
(2, u'work'),
(16, u'python'),
(6, u'to'),
(10, u'does'),
(11, u'want'),
(17, u'science'),
(15, u'love'),
(18, u'computer'),
(12, u'gensim'),
(13, u'out')]
ルックスクエリのように既存の用語を集めてあらかじめ計算された重みを使用してtfidfスコアを与えます。したがって、私の回避策は、モデルを毎週または毎日再構築することです。
これは実際には機能しますか?私は、tfidfの性質のために、基本的に、新しい文書が入ってくるたびに、モデルに含まれるすべての関連する単語機能のIDF値を更新する必要があるため、モデルを段階的に更新できないと考えました(tfidf行列を更新します)。コーパス全体にわたる新しい文書。また、ドキュメントに新しい単語が含まれているとどうなりますか?長さの不一致はありませんか?私もこの問題を積極的に研究していますので教えてください。 – killerT2333
これは機能していますが、私はあなたの新しいモデルをあなたの既存のモデルを使って照会するだけであると信じています。私は答えを編集して作品を見せます。 – snowneji
うわー!これは本当にクールです - これを共有してくれてありがとう。だから、私が正しく理解したら、新しい照会文書が入ったとき、gensimは、前もって計算されたtfidf行列と新しい照会文書からtfidfスコアを計算しますか?または、それはあらかじめ計算されたtfidf行列から計算するだけですか?常にモデルを更新するのが高価な場合は、モデルを定期的に更新する方が意味があります。 – killerT2333