2017-06-22 25 views
0

私は、gensimパッケージを使ってPythonでDoc2Vecテクニックを使ってNLPチャットアプリケーションを構築しています。私はすでにトークン化とステミングを行っています。私は訓練セットとユーザーが投げた質問の両方からストップワード(それがうまくいくかどうかをテストする)を取り除きたい。gensimのドキュメントからストップワードを削除するには?

ここに私のコードです。

import gensim 
import nltk 
from gensim import models 
from gensim import utils 
from gensim import corpora 
from nltk.stem import PorterStemmer 
ps = PorterStemmer() 

sentence0 = models.doc2vec.LabeledSentence(words=[u'sampl',u'what',u'is'],tags=["SENT_0"]) 
sentence1 = models.doc2vec.LabeledSentence(words=[u'sampl',u'tell',u'me',u'about'],tags=["SENT_1"]) 
sentence2 = models.doc2vec.LabeledSentence(words=[u'elig',u'what',u'is',u'my'],tags=["SENT_2"]) 
sentence3 = models.doc2vec.LabeledSentence(words=[u'limit', u'what',u'is',u'my'],tags=["SENT_3"]) 
sentence4 = models.doc2vec.LabeledSentence(words=[u'claim',u'how',u'much',u'can',u'I'],tags=["SENT_4"]) 
sentence5 = models.doc2vec.LabeledSentence(words=[u'retir',u'i',u'am',u'how',u'much',u'can',u'elig',u'claim'],tags=["SENT_5"]) 
sentence6 = models.doc2vec.LabeledSentence(words=[u'resign',u'i',u'have',u'how',u'much',u'can',u'i',u'claim',u'elig'],tags=["SENT_6"]) 
sentence7 = models.doc2vec.LabeledSentence(words=[u'promot',u'what',u'is',u'my',u'elig',u'post',u'my'],tags=["SENT_7"]) 
sentence8 = models.doc2vec.LabeledSentence(words=[u'claim',u'can,',u'i',u'for'],tags=["SENT_8"]) 
sentence9 = models.doc2vec.LabeledSentence(words=[u'product',u'coverag',u'cover',u'what',u'all',u'are'],tags=["SENT_9"]) 
sentence10 = models.doc2vec.LabeledSentence(words=[u'hotel',u'coverag',u'cover',u'what',u'all',u'are'],tags=["SENT_10"]) 
sentence11 = models.doc2vec.LabeledSentence(words=[u'onlin',u'product',u'can',u'i',u'for',u'bought',u'through',u'claim',u'sampl'],tags=["SENT_11"]) 
sentence12 = models.doc2vec.LabeledSentence(words=[u'reimburs',u'guidelin',u'where',u'do',u'i',u'apply',u'form',u'sampl'],tags=["SENT_12"]) 
sentence13 = models.doc2vec.LabeledSentence(words=[u'reimburs',u'procedur',u'rule',u'and',u'regul',u'what',u'is',u'the',u'for'],tags=["SENT_13"]) 
sentence14 = models.doc2vec.LabeledSentence(words=[u'can',u'i',u'submit',u'expenditur',u'on',u'behalf',u'of',u'my',u'friend',u'and',u'famili',u'claim',u'and',u'reimburs'],tags=["SENT_14"]) 
sentence15 = models.doc2vec.LabeledSentence(words=[u'invoic',u'bills',u'procedur',u'can',u'i',u'submit',u'from',u'shopper stop',u'claim'],tags=["SENT_15"]) 
sentence16 = models.doc2vec.LabeledSentence(words=[u'invoic',u'bills',u'can',u'i',u'submit',u'from',u'pantaloon',u'claim'],tags=["SENT_16"]) 
sentence17 = models.doc2vec.LabeledSentence(words=[u'invoic',u'procedur',u'can',u'i',u'submit',u'invoic',u'from',u'spencer',u'claim'],tags=["SENT_17"]) 

# User asks a question. 

document = input("Ask a question:") 
tokenized_document = list(gensim.utils.tokenize(document, lowercase = True, deacc = True)) 
#print(type(tokenized_document)) 
stemmed_document = [] 
for w in tokenized_document: 
    stemmed_document.append(ps.stem(w)) 
sentence19 = models.doc2vec.LabeledSentence(words= stemmed_document, tags=["SENT_19"]) 

# Building vocab. 
sentences = [sentence0,sentence1,sentence2,sentence3, sentence4, sentence5,sentence6, sentence7, sentence8, sentence9, sentence10, sentence11, sentence12, sentence13, sentence14, sentence15, sentence16, sentence17, sentence19] 

#I tried to remove the stop words but it didn't work out as LabeledSentence object has no attribute lower. 
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 sentences] 
.. 

私が直接sentencesからストップワードを削除して、ストップワードなしで単語の新しいセットを取得することができます方法はありますか?

答えて

1

sentencesオブジェクトはすでにLabeledSentenceオブジェクトのリストです。これらは上記のとおりです。文字列のリストはwordsに、リストの文字列はtagsに含まれています。

リスト内の各項目(document、あなたのリストの中にある)は、.lower()のような文字列メソッドを持つことができません。 (そのwordsは、すでに別のトークンですとしても、それは、.split()する必要があります。)

をクリーンなアプローチは、それらがLabeledSentenceオブジェクトを構築するために使用している前にリスト-の-wordsからストップワードを削除することです。たとえば、上部に定義された関数without_stopwords()を作成することができます。そして、LabeledSentenceオブジェクトを作成し、あなたのラインではなく、のように考えられます。

また
sentence0 = LabeledSentence(words=remove_stopwords([u'sampl', u'what', u'is']), 
          tags=["SENT_0"]) 

そのwordsの各属性は、現在ストップワードが不足しているように、あなたは既存LabeledSentenceオブジェクトを変異させることができます。あなたが要求していないが、知っておくべき

for doc in sentences: 
    doc.words = [word for word in doc.words if word not in stoplist] 
texts = sentences 

別に、物事:これは、より多くのようなもので、あなたの最後の行に代わる

  • TaggedDocument

    は今Doc2Vecテキストオブジェクトのための好ましい例 - クラス名です実際には、2つの必要なプロパティwordstagsを持つオブジェクトは正常に動作します。

  • Doc2Vecは、小さなおもちゃサイズのデータ​​セットに望ましいプロパティの多くを表示しません。何十もの文章に基づいて作成されたモデルが何も役に立たない場合、または前処理/パラメータオプションが最適です。 (何万語ものテキスト、少なくとも数十語のテキストは意味のある結果を得るのにはるかに良い)

  • 多くのWord2Vec/Doc2Vecの作業は、ステミングやストップワードの除去を気にしませんが、時には役に立つかもしれません。

+0

はい、正しくあります。それは正確ではなく、誤ったコサインの類似性をほとんどの時間与えますが、それは助けられません。それは私に提供されるデータです。その理由は、私が語幹除去や単語削除を止めるのが助けになるかどうかをチェックしようとしている理由です。サンプルの質問と回答のリストがあるときに質問応答システムを構築するためのよりよいアプローチを提案することもできますか?ありがとう。 – Kshitiz

+0

質問応答システムはかなり幅広い領域ですが、Doc2Vecよりも多くの機能が必要な場合は、あなたが持っているQAデータの量と種類に依存します。新しいQと以前のQとの単純な類似性のために、Doc2Vecは機能するかもしれませんが、*ロット*以上のトレーニングデータが必要です。また、「ワードムーバーの距離」と呼ばれる文の類似性のための興味深いテクニックもあります。これには、単語だけが必要です(ドキュメント単位のベクトルではありません)。最も類似した初期のQを見つけるためにはうまくいくかもしれません。 gensim Word2Vec/KeyedVectorsでは 'wmdistance()'として利用できますが、大きなデータセットではペアワイズを計算するのにはかなり時間がかかります。 – gojomo

関連する問題