About Lesson
In this Python NLP lesson we are going to learn about Python NLP Trigrams, so trigrams are three consecutive words that occurred in a text.
Now let’s create an example, you can see that we have used trigrams, also we have used grail.txt file from nltk, at the end we have just printed the 10 most common words.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
from nltk.corpus import webtext, stopwords from nltk import trigrams from nltk.probability import FreqDist text_data = webtext.words('grail.txt') stop_words = set(stopwords.words('english')) filtered_words = [] for word in text_data: if word not in stop_words: if len(word) > 3: filtered_words.append(word) #now we are going to use trigrams for this trgram = trigrams(filtered_words) freq_dist = FreqDist(trgram) print(freq_dist.most_common(10)) |
Run the code and this will be the result.
1 2 3 4 5 6 7 8 9 10 |
[(('Hello', 'Hello', 'Hello'), 21), (('squeak', 'squeak', 'squeak'), 15), (('clop', 'clop', 'clop'), 13), (('Burn', 'Burn', 'Burn'), 13), (('witch', 'witch', 'witch'), 11), (('mumble', 'mumble', 'mumble'), 10), (('clang', 'Bring', 'dead'), 9), (('King', 'Arthur', 'music'), 9), (('mumble', 'mumble', 'boom'), 9), (('Bring', 'dead', 'clang'), 8)] |
Also you can plot the frequency distribution.
1 |
freq_dist.plot(10) |
This is the result.
![Python NLP Trigrams](https://geekscoders.com/wp-content/uploads/2021/02/python-nlp-trigrams-frequency-distribution.jpg)