So I am really confused how to implement this function because this python file
ID: 3571359 • Letter: S
Question
So I am really confused how to implement this function because this python file imports info from three other files which each have the function "trainingDataHasNGram" but it does a slightly different thing in each of the different files. So I am confused how I am supposed to use/call the functions.
Here are the directions for the function I am trying to implement:
selectNGramModel
This function takes a trained list of language models and a sentence, which is a list of strings. It first checks if the trigram model in the list can be used to pick the next word for the sentence; if so, it returns the trigram model. If not, it attempts to back off to a bigram model and returns the bigram model if possible. As a last resort, it returns the unigram model. Remember that you wrote a function that checks whether or not a particular NGramModel can be used to pick the next word for a sentence.
These were the directions for the function I need to call:
trainingDataHasNGram
This function takes a sentence, which is a list of strings, and returns True if a particular language model can be used to determine the next token to add to that sentence, given the n-grams that the language model has stored in self.nGramCounts. If not, it returns False.
For the unigram model, this function returns True if the unigram model knows about any words at all: in other words, its self.nGramCounts dictionary is not empty. This is because a unigram model only looks at the frequency of single words, regardless of their context.
For the bigram model, this function returns True if the model has seen the last word in the current sentence at the start of a bigram. Hint: which "dimension" of the bigram model's self.nGramCounts would contain this information?
For the trigram model, this function returns True if the model has seen the second-to-last and last words in the current sentence at the start of a trigram, in that order.
Here is my attempt (I am just so confused how to call the function in each of the three files-please help):
def selectNGramModel (models sentence) Requires: models is a list of NGramModel objects sorted by descending priority tri hen bi then unigrams Modifies: nothing Effects starting from the beginning of the models list returns the first possible model that can be used for the current sentence based on the n-grams that the models know (Remember that you wrote a function that checks if a model can be used to pick a word for a sentence for x in models: if training DataHasNGram (sentence) return.Explanation / Answer
As per my understanding to your problem, you need to know how to call a function which is having same name in three different files and you want to call all of them on need basis from other file. But if you want us to define the functions "selectNGramModel & trainingDataHasNGram", you should give an example explaining the behavior of those two functions. Because it is not clear for me to understand the directions of the function you have provided.
Here is my answer to the main problem you are facing, "How to call same function which is defined in different files".
As you said you have imported all the 3 files in the file.
Let's say "trainingDataHasNGram" function is defined in the following 3 files
trainingData1.py
trainingData2.py
trainingData3.py
Import and call these functions in the following manner.
import trainingData1 as t1
import trainingData2 as t2
import trainingData3 as t3
def selectNGramModel(models,sentence):
for x in models:
if t1.trainingDataHasNGram(sentence):
return x
elif t2.trainingDataHasNGram(sentence):
return x
elif t3.trainingDataHasNGram(sentence):
return x
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.