Posts Lab Session 09
Post
Cancel

Lab Session 09


Python Logo

Welcome to Lab Session 9: Python, Part 3

Today, we’re going to work with frequency lists, dictionaries and some simple plotting with the matplotlib library. Let me know if you need help with any of the exercises. I can also help explain concepts from the lectures if you need it.

Don’t forget that you can discuss the exercises with your fellow classmates on MittUiB, the UiB Studyfellowship Discord server (channel: #ling123) or right here in ling123labs.com’s comments section.

The lecture notes for lecture 9 can be found here.


Exercise 1: Character frequencies with Python

Compare the Python implementation of calculating character frequencies to the Awk implementation. What is simpler and what is more complicated?

For reference, here is an example of a shell scripting implementation of counting character frequencies with AWK:

1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env bash
awk '
  BEGIN { FS="" }

  { for (field=1; field <= NF; field++) {
  freq[$field]++ }
  }

  END { for (char in freq)
        { print freq[char], char } }' | sort -nr


Exercise 2: Using the Counter module

a) Write a function that takes a filename as an argument and prints the character frequencies as showed in the lecture notes on using the Counter module.

b) Reverse the left-to-right order in which the character and frequency are printed.

c) Print the character and its frequency only if the character is alphanumeric. Use the isalnum() method to check if a word is alphanumeric.


Exercise 3: Plotting frequencies

a) Recreate the plot from the lecture notes. Save the plot in a file.

b) Create a new function that takes a filename as argument and plots the character frequencies.

Exercise 4: Sorting dictionaries

Create a function that sorts a dictionary by value (as opposed to the keys). In addition to the input dictionary, the function should have a parameter reverse which is set to False by default. If the reverse parameter is False, the function should sort the dictionary in descending order. Otherwise, the function should sort the dictionary in ascending order.

Tip: You can use the built-in function sorted() to sort the dictionary. sorted has three useful parameters: iterable (the thing you want to sort), key (what to sort by) and reverse (a boolean value).


Exercise 5: Word frequencies in Python

a) Use the Counter module to implement word frequencies in a different way, as demonstrated in the chapter on character frequencies. Sort by frequency or alphabetically.

b) Given a counter with word frequencies, print out the vocabulary, i.e. all word types, alphabetically sorted, one word per line.

c) Compute the word frequencies for a slightly larger text and make a plot.

Exercise 6: Summing dictionaries

Write a Python program to sum all the values in a dictionary.

Exercise 7: Dictionary-based functions

Create a function dict_top_3 which takes a dictionary as input, and returns the 3 highest values.

This post is licensed under CC BY 4.0 by the author.