Question
Write a function select_scores(langs, document,lang_counts=default_lang_counts) which takes a list of languages and returns the same list of languages, together with another list of the corresponding
Write a function select_scores(langs, document,lang_counts=default_lang_counts) which takes a list of languages and returns the same list of languages, together with another list of the corresponding scores of the string document for those languages. Use your implementation of score_document(document,lang_counts=default_lang_counts).
We have provided a function plot_scores(languages, scores)which takes a list of languages and their corresponding scores and plots them on a histogram using the matplotlib module. Try this out with the documents provided in the tabs at right. Are the scores highest for the languages the documents are written in?
It is interesting to see which languages have similar scores; perhaps this says something about how similar the languages are, and whether they are related in some way (e.g. perhaps they belong to neighbouring countries or evolved from the same language some time long ago).
Your function should behave as follows (results may differ in the final decimal points):
>>> select_scores(['English', 'Indonesian', 'Malay'],open('Indonesian1.txt').read())
(['English', 'Indonesian', 'Malay'], [2.3500733676922594, 12.076336401029348, 12.096929312152872])
>>> select_scores(['English','Indonesian','Malay'],open('English1.txt').read())
(['English', 'Indonesian', 'Malay'], [21.428216914833023, 7.553282625461611, 5.766103898176064])
Make sure you comment out the plotting code before grading.
INcomplete coding:
import matplotlib.pyplot as plt import numpy as np
def plot_scores(languages, scores): """plot_scores takes a list of languages langs and their corresponding scores and plots them in a bar chart.""" N = len(languages) # a nice way of generating the x locations for the groups ind = np.arange(N) # the width of the bars width = 0.4 ax = plt.axes()
rects1 = ax.bar(ind, scores, width, color='b')
# add some text for labels, title and axes ticks ax.set_ylabel('Score') ax.set_title('Score per language for document') # note that we add width/2 so the labels are centred ax.set_xticks(ind + width/2) ax.set_xticklabels(languages) plt.ylim(0, 1.1*max(scores))
# a function that adds labels atop the bars to undicate their height def autolabel(rects): for rect in rects: height = rect.get_height() ax.text(rect.get_x() + rect.get_width()/2., 1.05*height, '%2.2f' % height, ha='center', va='bottom')
autolabel(rects1) plt.show()
I dont really get the output
Step 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