from math import sqrt def getMetrics (record, commons): # define variables lc, w
ID: 3791683 • Letter: F
Question
from math import sqrt
def getMetrics (record, commons): # define variables lc, wc, and cc and # initialize them (mostly to zero) lc = 0 # letter count
wc = 0
cc = 0
__________ = len (record) # word count
__________ = __________ # common-word count
# loop through every word in the record:
for word in __________: # count if the word is in commons
if word in __________:
cc = cc + __________ # accumulate the characters
lc = __________ + len (word) # return the two measurements
return [float (cc) / wc, float (lc) / wc]
def getMetricsList (paragraphs, commons):
metricsList = []
for record in paragraphs:
metricsList.append (__________ (record, commons))
return metricsList
def findCentroid (metricsList):
# we assume there is at least one record here
# n is the number of elements in a record
# and nn is the number of records
n = __________ (metricsList [0])
nn = len (__________) # create and initialize the accumulator
result = [0] * n
for record in metricsList:
for i in range (n):
result [i] = result [i] + record [i]
for i in range (n):
result [i] = result [i] / float (nn)
return result
def findDistance (rec1, rec2): # we assume the two records have the same length
dist = 0.0
n = len (rec1)
for i in range (n):
dist = dist + (rec1 [i] - rec2 [i]) ** 2
return sqrt (__________)
def findDistanceList (paragraphs, commons):
metricsList = __________ (paragraphs, commons)
centroid = __________ (metricsList)
distanceList = []
for metrics in metricsList:
distanceList.append (__________ (metrics, centroid))
return distanceList
def findOutLinerIndices (paragraphs, commons, howMany):
distanceList = findDistanceList (paragraphs, commons)
work = distanceList [:] # make a clone
work.__________ () # sort the list
work.__________ () # reverse the list
criterion = work [howMany-1]
result = []
for i in range (len (paragraphs)):
if distanceList [i] >= criterion:
result.append (i)
return result
def test (paragraphs, commons):
indices = findOutLinerIndices (paragraphs, commons, 5)
for i in indices:
score='%4.2f, %4.2f'%tuple(getMetrics(paragraphs[i],commons))
print (str (i) + ': ' + score + ' --- ' + str(paragraphs[i]))
def test1 ():
from pg1 import paragraphs, commons
test (paragraphs, commons) def main ():
test1 ()
def test2 ():
from pg1260 import paragraphs, commons
test (paragraphs, commons)def main ():
test2 ()
def main ():
print ('***********************************')
print ('Results for pg1.py:')
test1 ()
print ('***********************************')
print ('Results for pg1260.py:')
test2 ()
print ('***********************************')
main ()
Explanation / Answer
# define variables lc, wc, and cc and # initialize them (mostly to zero) lc = 0 # letter count
wc = 0
cc = 0
__________ = len (record) # word count
__________ = __________ # common-word count
# loop through every word in the record:
for word in __________: # count if the word is in commons
if word in __________:
cc = cc + __________ # accumulate the characters
lc = __________ + len (word) # return the two measurements
return [float (cc) / wc, float (lc) / wc]
def getMetricsList (paragraphs, commons):
metricsList = []
for record in paragraphs:
metricsList.append (__________ (record, commons))
return metricsList
def findCentroid (metricsList):
# we assume there is at least one record here
# n is the number of elements in a record
# and nn is the number of records
n = __________ (metricsList [0])
nn = len (__________) # create and initialize the accumulator
result = [0] * n
for record in metricsList:
for i in range (n):
result [i] = result [i] + record [i]
for i in range (n):
result [i] = result [i] / float (nn)
return result
def findDistance (rec1, rec2): # we assume the two records have the same length
dist = 0.0
n = len (rec1)
for i in range (n):
dist = dist + (rec1 [i] - rec2 [i]) ** 2
return sqrt (__________)
def findDistanceList (paragraphs, commons):
metricsList = __________ (paragraphs, commons)
centroid = __________ (metricsList)
distanceList = []
for metrics in metricsList:
distanceList.append (__________ (metrics, centroid))
return distanceList
def findOutLinerIndices (paragraphs, commons, howMany):
distanceList = findDistanceList (paragraphs, commons)
work = distanceList [:] # make a clone
work.__________ () # sort the list
work.__________ () # reverse the list
criterion = work [howMany-1]
result = []
for i in range (len (paragraphs)):
if distanceList [i] >= criterion:
result.append (i)
return result
def test (paragraphs, commons):
indices = findOutLinerIndices (paragraphs, commons, 5)
for i in indices:
score='%4.2f, %4.2f'%tuple(getMetrics(paragraphs[i],commons))
print (str (i) + ': ' + score + ' --- ' + str(paragraphs[i]))
def test1 ():
from pg1 import paragraphs, commons
test (paragraphs, commons) def main ():
test1 ()
def test2 ():
from pg1260 import paragraphs, commons
test (paragraphs, commons)def main ():
test2 ()
def main ():
print ('***********************************')
print ('Results for pg1.py:')
test1 ()
print ('***********************************')
print ('Results for pg1260.py:')
test2 ()
print ('***********************************')
main ()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.