Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I want to calculate the accuracy of this model that predicts the next word based on 3 previous words using n - gram. def generate
I want to calculate the accuracy of this model that predicts the next word based on previous words using ngram.
def generateayastartphrase:
start startphrase
threshold random.random
aya
while start not in ngrams.keys:
startnew input:
start startnew
for i in range:
line start
for j in range:
linewords nltkwordtokenizeline
start joinlinewordsn :
candidates ngrams.getstart
if not candidates:
printfstart
break
probability candidatescountword lencandidates for word in candidates
accumulator sumprobability:i for i in rangelenprobability
for word, acc in zipcandidates accumulator:
if acc threshold:
nextword word
break
else:
nextword random.choicecandidates
line nextword
aya.appendline
start random.choicengramsgetstart listngramskeys
return
joinaya
startword input: # Prompt the user to enter a word to start the aya
#printThe ayah'
aya generateayastartword # Generate the aya using the input word as the starting point
query aya
searchresults retrieveverseinformationquery dataset
printfText of the verse: resultDocument
printfresultInformation
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started