I want to get bigrams and trigrams from the example sentences I have mentioned.
My code works fine for bigrams. However, it does not capture trigrams in the data (e.g., human computer interaction, which is mentioned in 5 places of my sentences)
Approach 1 Mentioned below is my code using Phrases in 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_)
Approach 2 I even tried to use Phraser and Phrases both, but it didn't work.
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_)
Please help me to fix this issue of getting trigrams.
I am following the example documentation of Gensim.