Homework 4: Loops & Conditionals

LIN 301: Computation for Linguists

Author

Dr. Andrew M. Byrd

Overview

Goal: Download a raw conlang wordlist and write Python code to:

  1. Strip whitespace and all edge punctuation marks.
  2. Make everything lowercase.
  3. Split into words.
  4. Build two lists:
    • words_v: tokens with at least one vowel
    • words_no_v: tokens with no vowels
  5. Print required summaries.

You’ll be making use of the skills learned in class: re.split(), .lower(), .append(), for loops, and conditionals.

You will submit one Quarto file named HW4_Lastname_Firstname.qmd.


Step 0 — Download the file (Terminal)

Open the Terminal pane in RStudio and download a file called conlang.txt using bash curl. File located at: https://winjapati.github.io/301_Fall_2025/5_loops_conditionals/conlang.txt.

Place conlang.txt in a new subfolder called hw4_files.

Step 1 - Load the text

Next, load your txt file within your qmd using the with open... command.

# 1) Load the raw text
with open("hw4_files/conlang.txt", "r", encoding="utf-8") as f:
    text = f.read()

Step 2 - Normalize + split

Next, using re.split(), eliminate all outer whitespace and non-word characters, and use the .lower() method while you’re at it to make all text lowercase.

Using a list comprehension, save these words in a list called words_all. When you do so, be sure to eliminate any members of the list that are blank.

words_all = [w for w in words_all if w]

Verify that you’ve successfully done so using by typing words_all[:15].

Step 3 - Filter out words by vowel

Next, place tokens that contain at least one vowel into a new list called words_v. Place tokens that do not have any vowels in a new list called words_no_V.

To do this, you will need to follow two steps:

  1. Create a set of all known vowels that occur in the conlang.
vowels = set("aei...")    # find all of the vowels in the conlang, and list them here.
  1. Use the any() command, which will return True if any of the checks inside are True.
any(ch in vowels for ch in wd)

You will be running this code inside of a conditional, nested inside a for loop.

Hint: Remember to create empty lists called words_v and words_no_v beforehand! Otherwise, you’ll have nothing to .append() to.

Step 4 - Verify

Run the following script to verify that everything is working properly.

print("First 30 words (with vowels):")
print(words_v[:30])
print()

print("Counts:")
print("  total tokens after edge-punct clean:", len(words_all))
print("  kept (has ≥1 vowel):", len(words_v))
print("  removed (no vowels):", len(words_no_v))

Step 5 - Upload to Canvas

Upload this qmd file to Canvas.