Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python Please help me to finish the line 19 and line 31 by dict comprehension and dict.get big-segmented link: https://drive.google.com/open?id=1Dyg78SPLXpN276sVautBbonZhiAFRxYs -*- coding: utf-8 -* 2
Python
Please help me to finish the line 19 and line 31 by dict comprehension and dict.get
big-segmented link:
https://drive.google.com/open?id=1Dyg78SPLXpN276sVautBbonZhiAFRxYs
-*- coding: utf-8 -* 2 import operator 3 from collections import Counter 4 from functools import reduce 7 # multiply all elements in a list 8 def product(iterable): return reduce (operator.mul, iterable, 1) 12 class LanguageModel(): 13 def init (self, filename-'big.segmented.txt: with open(filename, encoding-'UTF-8") as f: self.total sum(word-count.values()) # compute probability of each word: count/total (as dict) word-count = Counter(word for line in f for word in line.split()) 15 16 self.word_prob - 20 def getitem (self, word): return self.get_word_prob(word) 23 def get(self, word): return self [word] 25 26 27 28 29 30 def get_word_prob(self, word): # return the probability of a word # if the word is not in the 'self.word-prob' dictionary # return 10/10**len(word)/total as the default probability return 32 def get_words_prob(self, tokens) return product (map (self.get, tokens)) 34 35 36 37 if namemain 38 39 40 Im- LanguageModel() # easy 9.074170876729021e-95 print('easy',1m[ easy']) # apple 7.755701604034296e-96 print('apple', 1m.get('apple')) # juice 3.877850802017103e-06 print( juice', 1m.get_word prob( juice')) 42 46 # apple juice is good 9.885965853143219e-17 rint( 'apple juice is good', 1m.get words prob( 'apple juice is good".splitStep 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