Answered step by step
Verified Expert Solution
Link Copied!

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 3 previous words using n-gram.
def generate_aya(start_phrase):
start = start_phrase
threshold = random.random()
aya =[]
while start not in ngrams.keys():
start_new = input(": ")
start = start_new
for i in range(1):
line = start
for j in range(3):
line_words = nltk.word_tokenize(line)
start =''.join(line_words[-n +3:])
candidates = ngrams.get(start,[])
if not candidates:
print(f"{start}")
break
probability =[candidates.count(word)/ len(candidates) for word in candidates]
accumulator =[sum(probability[:i +1]) for i in range(len(probability))]
for word, acc in zip(candidates, accumulator):
if acc >= threshold:
next_word = word
break
else:
next_word = random.choice(candidates)
line +=''+ next_word
aya.append(line)
start = random.choice(ngrams.get(start, list(ngrams.keys())))
return '
'.join(aya)
start_word = input(": ") # Prompt the user to enter a word to start the aya
#print('The ayah')
aya = generate_aya(start_word) # Generate the aya using the input word as the starting point
query =aya
search_results = retrieve_verse_information(query, dataset)
print(f"Text of the verse: {result['Document']}")
print(f"{result['Information']}")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced MySQL 8 Discover The Full Potential Of MySQL And Ensure High Performance Of Your Database

Authors: Eric Vanier ,Birju Shah ,Tejaswi Malepati

1st Edition

1788834445, 978-1788834445

More Books

Students also viewed these Databases questions

Question

What courses in college did you like most? Least? Why?

Answered: 1 week ago