I have a list of sentences:
text = ['cant railway station','citadel hotel',' police stn'].
I need to form bigram pairs and store them in a variable. The problem is that when I do that, I get a pair of sentences instead of words. Here is what I did:
text2 = [[word for word in line.split()] for line in text] bigrams = nltk.bigrams(text2) print(bigrams)
which yields
[(['cant', 'railway', 'station'], ['citadel', 'hotel']), (['citadel', 'hotel'], ['police', 'stn'])
Can't railway station and citadel hotel form one bigram. What I want is
[([cant],[railway]),([railway],[station]),([citadel,hotel]), and so on...
The last word of the first sentence should not merge with the first word of second sentence. What should I do to make it work?
First, we need to generate such word pairs from the existing sentence maintain their current sequences. Such pairs are called bigrams. Python has a bigram function as part of NLTK library which helps us generate these pairs.
nltk.bigrams() returns an iterator (a generator specifically) of bigrams. If you want a list, pass the iterator to list() .
In natural language processing, an n-gram is an arrangement of n words. For example “Python” is a unigram (n = 1), “Data Science” is a bigram (n = 2), “Natural language preparing” is a trigram (n = 3) etc. Here our focus will be on implementing the unigrams(single words) models in python.
Using list comprehensions and zip:
>>> text = ["this is a sentence", "so is this one"] >>> bigrams = [b for l in text for b in zip(l.split(" ")[:-1], l.split(" ")[1:])] >>> print(bigrams) [('this', 'is'), ('is', 'a'), ('a', 'sentence'), ('so', 'is'), ('is', 'this'), ('this', 'one')]
from nltk import word_tokenize from nltk.util import ngrams text = ['cant railway station', 'citadel hotel', 'police stn'] for line in text: token = nltk.word_tokenize(line) bigram = list(ngrams(token, 2)) # the '2' represents bigram...you can change it to get ngrams with different size
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With