2017-09-11 8 views
4

私が言及した例文からbigramsとtrigramsを取得したいと思います。Gensimを使ってトリグラムを取得する際の問題

私のコードはバイグラムでうまく動作します。しかし、データ内のトリグラム(例えば、私の文章の5カ所に記載されている人間のコンピュータのやりとり)は捕捉されません。以下は、Gensimのフレーズを使用した私のコードです。

from gensim.models import Phrases 
documents = ["the mayor of new york was there", "human computer interaction and machine learning has now become a trending research area","human computer interaction is interesting","human computer interaction is a pretty interesting subject", "human computer interaction is a great and new subject", "machine learning can be useful sometimes","new york mayor was present", "I love machine learning because it is a new subject area", "human computer interaction helps people to get user friendly applications"] 
sentence_stream = [doc.split(" ") for doc in documents] 

bigram = Phrases(sentence_stream, min_count=1, threshold=1, delimiter=b' ') 
trigram = Phrases(bigram_phraser[sentence_stream]) 

for sent in sentence_stream: 
    bigrams_ = bigram_phraser[sent] 
    trigrams_ = trigram[bigrams_] 

    print(bigrams_) 
    print(trigrams_) 

アプローチ2私も両方Phraserやフレーズを使用しようとしましたが、それはうまくいきませんでした。

from gensim.models import Phrases 
from gensim.models.phrases import Phraser 
documents = ["the mayor of new york was there", "human computer interaction and machine learning has now become a trending research area","human computer interaction is interesting","human computer interaction is a pretty interesting subject", "human computer interaction is a great and new subject", "machine learning can be useful sometimes","new york mayor was present", "I love machine learning because it is a new subject area", "human computer interaction helps people to get user friendly applications"] 
sentence_stream = [doc.split(" ") for doc in documents] 

bigram = Phrases(sentence_stream, min_count=1, threshold=2, delimiter=b' ') 
bigram_phraser = Phraser(bigram) 
trigram = Phrases(bigram_phraser[sentence_stream]) 

for sent in sentence_stream: 
    bigrams_ = bigram_phraser[sent] 
    trigrams_ = trigram[bigrams_] 

    print(bigrams_) 
    print(trigrams_) 

トリグラムを取得するこの問題を解決するのを手伝ってください。

私はGensimのexample documentationに従っています。

答えて

1

私はバイグラムを取得することができたし、自分のコードにいくつかの変更を加えてトライグラム:それ以外の場合は、の構築を可能にする奇妙なdigramsを形成しているようですので、私はバイグラムPhrasesからthreshold = 1パラメータを削除

from gensim.models import Phrases 
documents = ["the mayor of new york was there", "human computer interaction and machine learning has now become a trending research area","human computer interaction is interesting","human computer interaction is a pretty interesting subject", "human computer interaction is a great and new subject", "machine learning can be useful sometimes","new york mayor was present", "I love machine learning because it is a new subject area", "human computer interaction helps people to get user friendly applications"] 
sentence_stream = [doc.split(" ") for doc in documents] 

bigram = Phrases(sentence_stream, min_count=1, delimiter=b' ') 
trigram = Phrases(bigram[sentence_stream], min_count=1, delimiter=b' ') 

for sent in sentence_stream: 
    bigrams_ = [b for b in bigram[sent] if b.count(' ') == 1] 
    trigrams_ = [t for t in trigram[bigram[sent]] if t.count(' ') == 2] 

    print(bigrams_) 
    print(trigrams_) 

奇妙なトリグラム(bigramはトリグラムPhrasesを構築するために使用されています)。このパラメータは、より多くのデータがある場合に役立つでしょう。トライグラムの場合は、min_countパラメータも指定する必要があります。これは、パラメータが指定されていない場合はデフォルトで5になるためです。

各ドキュメントのbigramsとtrigramsを取得するために、このリストの理解トリックを使用して、2つまたは3つの単語でそれぞれ形成されていない要素をフィルタリングすることができます。


編集 - thresholdパラメータに関するいくつかの詳細:

このパラメータは、二つの言葉Bフレーズを形成し、それが唯一であるかどうかを判断するために推定器によって使用されていますもし:

(count(a followed by b) - min_count) * N/(count(a) * count(b)) > threshold 

どこNは合計の語彙サイズです。デフォルトでは、パラメータ値は10です(docs参照)。したがって、thresholdが高いほど、フレーズを構成する単語の制約が厳しくなります。

たとえば、最初のアプローチではthreshold = 1を使用しようとしていたので、 "human computer interaction"で始まる5つの文のうち3つのdigramsとして['human computer','interaction is']が得られます。その奇妙な第二のdigramは、より緩和された閾値の結果である。

次に、デフォルトのthreshold = 10のトライグラムを取得しようとすると、これらの3つの文では['human computer interaction is']しか得られず、残りの2つのみ(しきい値でフィルタリングされたもの)では何も得られません。それはトライグラムの代わりに4グラムだったので、それもif t.count(' ') == 2でフィルタリングされます。たとえば、トリグラムのスレッシュホールドを1に下げる場合、残りの2つの文に対して[人間のコンピュータの相互作用]をトライグラムとして取得することができます。それはパラメータの良い組み合わせを得ることは容易ではないようです。here's詳細。

私はエキスパートではないので、この結論を塩の穀物で取ってください。まず、奇妙なディグラムが混乱を招く可能性があるので、最初に良いジグラムの結果を得てください( '相互作用'のようなものではない)さらにトリグラム、4グラム...

+0

あなたの非常に貴重な答えに感謝します。乾杯! :)ところで、私にはあまり明確ではないので、「しきい値」の値がどうなるか教えてください。 –

+1

あなたは大歓迎です!はい、私は答えを編集しました、うまくいけば今それは少し明確です。 – stjernaluiht

+0

ありがとう!非常に便利な答えが見つかりました:) –

関連する問題