I attempted to write this python function based on the RME but I know it\'s wron
ID: 3780217 • Letter: I
Question
I attempted to write this python function based on the RME but I know it's wrong. Please help me fix it. I am confused how to make a copy. I know I need to make a deep copy but I'm not sure I did it right. Also, did I loop through the list of lists correctly? Do my insert and extend functions add the right symbols to the right locations? Do I need to use the dictionary "self" which contains all the member variables for a class?
Further details about this function:
prepData
The purpose of this function is to take input data in the form of a list of lists, and return a copy of that list with symbols added to both ends of each inner list.
For the core, these inner lists will be sentences, which are represented as lists of strings. The symbols added to the beginning of each sentence will be ^::^ followed by ^:::^, and the symbol added to the end of each sentence will be $:::$. These are arbitrary symbols, but make sure to use them exactly and in the correct order.
For example, if the function is passed this list of lists:
Then it would return a new list that looks like this:
The purpose of adding two symbols at the beginning of each sentence is so that you can look at a trigram containing only the first English word of that sentence. This captures information about which words are most likely to begin a sentence; without these symbols, you would not be able to use the trigam model at the beginning of sentences because there would be no trigrams to look at until the third word.
The purpose of adding a symbol to the end of each sentence is to be able to generate sentence endings. If you ever see $:::$ while generating a sentence in the generateSentence function, you know the sentence is complete.
MY ATTEMPT:
def prepData (self, text) Requires: text is a list of lists of strings Modifies: nothing Effects returns a copy of text where each inner list starts with the symbols A: A and and ends with the symbol 'S: S' For example if an inner list in text were hello', 'goodbye 1, that list would become I' hello goodbye 'S S in the returned copy Make sure you are not modifying the original text parameter in this function text copy import copy textCopy copy.deepcopy (text) for list in text copy: textCopy insert (0, T A A T text copy. insert (1 T A A T text copy .extend return text CopyExplanation / Answer
You did deep copy correctly.
Also, you did iterate through the list correctly but you had to use list.insert instead of textCopy.insert.
Also, there is a typo, it is textCopy but you have written text_copy in one line.
Also, use append instead of extend to add an element at the end of the list. The operation is much cheaper.
Do I need to use the dictionary "self" which contains all the member variables for a class?
You only need to use self if you want to define the input list of lists as a member of the object. self in python is equivalent to this in other languages. So, to answer the question, self is only required if you design your class that way.
Code
import copy
class Data:
def prepData(self, text):
textCopy = []
textCopy = copy.deepcopy(text)
for list in textCopy:
list.insert(0, '^::^')
list.insert(1, '^:::^')
list.append('$:::$')
return textCopy
data = Data()
print data.prepData([['hety', 'gfucb'], ['jfbwd', '3bf']])
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.