Question

How to create a random sentence generator from unigrams, bigrams, trigrams from a given number of...

How to create a random sentence generator from unigrams, bigrams, trigrams from a given number of text files in python code without using NLTK?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

we can use some natural language processing libraries such as the Natural Language Toolkit (NLTK). In particular, nltk has the ngrams function that returns a generator of n-grams given a tokenized sentence.
sample code:
import re
from nltk.util import ngrams

s = s.lower()
s = re.sub(r'[^a-zA-Z0-9\s]', ' ', s)
tokens = [token for token in s.split(" ") if token != ""]
output = list(ngrams(tokens, 5))
The above block of code will generate the same output as the function generate_ngrams() as shown above. It is for n-grams in place of n by keepin 1,2,3 e get unigram,bigram,trigram.

Add a comment
Know the answer?
Add Answer to:
How to create a random sentence generator from unigrams, bigrams, trigrams from a given number of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT