2013-08-25 14 views
7

私はtf-idfでテキストの類似性を計算するためにこのコードを持っています。pythonのtfidfアルゴリズム

from sklearn.feature_extraction.text import TfidfVectorizer 

documents = [doc1,doc2] 
tfidf = TfidfVectorizer().fit_transform(documents) 
pairwise_similarity = tfidf * tfidf.T 
print pairwise_similarity.A 

問題は、このコードが入力プレーン文字列として取得し、私は、ストップワードを削除語幹とtokkenizeで書類を作成したいということです。したがって、入力はリストになります。私はtokkenized文書をdocuments = [doc1,doc2]を呼び出した場合、エラーは次のとおりです。

Traceback (most recent call last): 
    File "C:\Users\tasos\Desktop\my thesis\beta\similarity.py", line 18, in <module> 
    tfidf = TfidfVectorizer().fit_transform(documents) 
    File "C:\Python27\lib\site-packages\scikit_learn-0.14.1-py2.7-win32.egg\sklearn\feature_extraction\text.py", line 1219, in fit_transform 
    X = super(TfidfVectorizer, self).fit_transform(raw_documents) 
    File "C:\Python27\lib\site-packages\scikit_learn-0.14.1-py2.7-win32.egg\sklearn\feature_extraction\text.py", line 780, in fit_transform 
    vocabulary, X = self._count_vocab(raw_documents, self.fixed_vocabulary) 
    File "C:\Python27\lib\site-packages\scikit_learn-0.14.1-py2.7-win32.egg\sklearn\feature_extraction\text.py", line 715, in _count_vocab 
    for feature in analyze(doc): 
    File "C:\Python27\lib\site-packages\scikit_learn-0.14.1-py2.7-win32.egg\sklearn\feature_extraction\text.py", line 229, in <lambda> 
    tokenize(preprocess(self.decode(doc))), stop_words) 
    File "C:\Python27\lib\site-packages\scikit_learn-0.14.1-py2.7-win32.egg\sklearn\feature_extraction\text.py", line 195, in <lambda> 
    return lambda x: strip_accents(x.lower()) 
AttributeError: 'unicode' object has no attribute 'apply_freq_filter' 

は、コードを変更して、リストを受け入れるか、私は再び文字列にtokkenized文書を変更する必要がありますようにする方法はありますか?

+0

実際のエラーメッセージが表示されていないようです(トレースバックは含まれていますが、発生したエラーは含まれていません)。 –

+0

私はそれを編集する。 – Tasos

+0

@Tasos私の答えはうまくいったのですか、それでも問題はありますか?私の解決策がうまくいかなかったら、 'doc1' /' doc2'の最小限の例を挙げることができますか? – chlunde

答えて

5

小文字と独自の「NOP」トークナイザを供給するための前処理をスキップ試してみてください:あなたは、あなたの前処理の重複を避けるためにstop_wordsのような他のパラメータをチェックアウトする必要があります

tfidf = TfidfVectorizer(tokenizer=lambda doc: doc, lowercase=False).fit_transform(documents)