Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We begin with a feature extraction function. The features we are going to use ar

ID: 3588530 • Letter: W

Question

We begin with a feature extraction function. The features we are going to use are called trigrams. A trigram is simply a string of three contiguous characters. For example in the string"I love computing", there are lots of trigrams (L-2 to be precise, where L is the length of the string): ["I l"," lo","Lov","ove"] are the first four of them, in sequence. Write a function count trigrams (document) that takes a string and returns a default dictionary with the frequency counts of the trigrams within the string (noting that if you have N repeats of the same trigram in the string, the frequency will be N). Note that the output must be a default dictionary and not a standard dictionary, as it will be useful later. Note also that you should not modify the string in any way (e.g. remove punctuation, remove whitespace or convert to lower case) in calculating the frequencies. Your code should behave as follows: >>> count tr1grams ("hel") defaultdict (, ['hel' 1.0H) >>> counttrigrams ("aaaaa") defaultd1ct , f'aaa 3.0H) >>> count trigrams ("Boaty mcBoatFace.") defaultd1ct (, {'ty ": 1.0, Fac': 1.

Explanation / Answer

def count_trigrams(document): """ count_trigrams takes a string and returns a dictionary of the counts of trigrams within the document. """ count_dict = dd(float) i = 0 for i in range(len(document[:-2])): trig = document[i:i+3] count_dict[trig] += 1.0 return count_dict

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote