Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import nltk
- from nltk import ngrams
- from nltk.tokenize import word_tokenize
- from collections import Counter
- #nltk.download('punkt')
- text = """
- Natural Language Processing with Python and NLTK is powerful.
- It helps in building models to understand and generate human language.
- """
- tokens = word_tokenize(text.lower())
- def generate_ngrams(tokens,n):
- n_grams = list(ngrams(tokens,n))
- return n_grams
- bigrams = generate_ngrams(tokens,2)
- trigrams = generate_ngrams(tokens,3)
- bigram_freq = Counter(bigrams)
- trigram_freq = Counter(trigrams)
- print("Bigrams:")
- for gram,freq in bigram_freq.items():
- print(f"{gram}: {freq}")
- print("\nTrigrams:")
- for gram,freq in trigram_freq.items():
- print(f"{gram}: {freq}")
- def predict_next_word(input_word, bigram_freq):
- candidates = {second: freq for (first, second), freq in bigram_freq.items() if first == input_word.lower()}
- if candidates:
- predicted = max(candidates, key=candidates.get)
- return predicted
- else:
- return None
- input_word = input("Enter word: ")
- next_word = predict_next_word(input_word, bigram_freq)
- if next_word:
- print(f"Predicted next word after '{input_word}' is: {next_word}")
- else:
- print(f"No prediction available for '{input_word}'")
- def predict_next_word_trigram(word1, word2, trigram_freq):
- candidates = {
- third: freq
- for (first, second, third), freq in trigram_freq.items()
- if first == word1.lower() and second == word2.lower()
- }
- if candidates:
- predicted = max(candidates, key=candidates.get)
- return predicted
- else:
- return None
- input_word1 = input("Enter word 1: ")
- input_word2 = input("Enter word 2: ")
- next_word = predict_next_word_trigram(input_word1, input_word2, trigram_freq)
- if next_word:
- print(f"Predicted next word after '{input_word1} {input_word2}' is: {next_word}")
- else:
- print(f"No prediction available for '{input_word1} {input_word2}'")
Advertisement
Add Comment
Please, Sign In to add comment