GamerBhai02

NLP Exp 5

Oct 9th, 2025
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | Source Code | 0 0
  1. import nltk
  2. from nltk import ngrams
  3. from nltk.tokenize import word_tokenize
  4. from collections import Counter
  5. #nltk.download('punkt')
  6.  
  7. text = """
  8. Natural Language Processing with Python and NLTK is powerful.
  9. It helps in building models to understand and generate human language.
  10. """
  11. tokens = word_tokenize(text.lower())
  12.  
  13. def generate_ngrams(tokens,n):
  14.     n_grams = list(ngrams(tokens,n))
  15.     return n_grams
  16.  
  17. bigrams = generate_ngrams(tokens,2)
  18. trigrams = generate_ngrams(tokens,3)
  19.  
  20. bigram_freq = Counter(bigrams)
  21. trigram_freq = Counter(trigrams)
  22.  
  23. print("Bigrams:")
  24. for gram,freq in bigram_freq.items():
  25.     print(f"{gram}: {freq}")
  26.  
  27. print("\nTrigrams:")
  28. for gram,freq in trigram_freq.items():
  29.     print(f"{gram}: {freq}")
  30.  
  31.  
  32.  
  33. def predict_next_word(input_word, bigram_freq):
  34.     candidates = {second: freq for (first, second), freq in bigram_freq.items() if first == input_word.lower()}
  35.     if candidates:
  36.         predicted = max(candidates, key=candidates.get)
  37.         return predicted
  38.     else:
  39.         return None
  40.  
  41. input_word = input("Enter word: ")
  42. next_word = predict_next_word(input_word, bigram_freq)
  43.  
  44. if next_word:
  45.     print(f"Predicted next word after '{input_word}' is: {next_word}")
  46. else:
  47.     print(f"No prediction available for '{input_word}'")
  48.  
  49.  
  50. def predict_next_word_trigram(word1, word2, trigram_freq):
  51.     candidates = {
  52.         third: freq
  53.         for (first, second, third), freq in trigram_freq.items()
  54.         if first == word1.lower() and second == word2.lower()
  55.     }
  56.     if candidates:
  57.         predicted = max(candidates, key=candidates.get)
  58.         return predicted
  59.     else:
  60.         return None
  61.  
  62. input_word1 = input("Enter word 1: ")
  63. input_word2 = input("Enter word 2: ")
  64. next_word = predict_next_word_trigram(input_word1, input_word2, trigram_freq)
  65.  
  66. if next_word:
  67.     print(f"Predicted next word after '{input_word1} {input_word2}' is: {next_word}")
  68. else:
  69.     print(f"No prediction available for '{input_word1} {input_word2}'")
Tags: nlp
Advertisement
Add Comment
Please, Sign In to add comment