Computation for Linguists

Other Visualizations

Dr. Andrew M. Byrd

2025-11-17

Review

  • What did you learn last time?

Recap from Last Time

  • Functions how do they work?
def function_name(arg)
  code
  return(output)

function_name(arg)

Recap from Last Time

def read_book(path):
    with open(path, mode="r", encoding="utf-8") as book_file:
        book_lines = book_file.readlines()
    clean_lines = [line.strip().lower() for line in book_lines]
    
    # Split each line into words and flatten the list
    words = [word for line in clean_lines for word in line.split() if word]
    return words

Activity: Together

  • Let’s write a function together that inserts the infix “-izz-” immediately a vowel in the word, if that vowel is preceded by a consonant
    • andrew –> andrizzew
    • bob –> bizzob
    • kentucky –> kizzentizzucky

Using Python to Make Graphics

Choosing the Right Chart

  • Bar (horizontal): rankable categories; easy comparison
  • Pie: proportion snapshots (few categories)
  • Line: order matters (rank, time)
  • Scatter: relationship between two numeric features
  • Word cloud: quick qualitative gist

Bar Charts

from collections import Counter
alice = read_book("alice.txt")

Bar Chart

  • Let’s write a function that will:
    • Load up a list of words
    • Count them
    • Create a bar chart of a predefined number of forms
    • Add a title that the user supplies

Bar Chart

  • Run this code, then experiment by deleting arguments when you run the function
import matplotlib.pyplot as plt
from collections import Counter

def bar(tokens, k=10, title="Top Words (Bar Chart)"):
    """
    Draws a simple bar chart of the k most frequent tokens.
    """
    if not tokens:
        print("No data.")
        return
    
    c = Counter(tokens)
    top_k = c.most_common(k)
    words, freqs = zip(*top_k)
    
    plt.figure(figsize=(8, 4))
    plt.bar(words, freqs)
    plt.title(title)
    plt.xlabel("Word")
    plt.ylabel("Frequency")
    plt.xticks(rotation=45, ha="right")
    plt.tight_layout()
    plt.show()

bar(alice, k=10, title="Top Words in Alice")

Pie Charts

  • Great for composition
    • POS proportions
    • Character shares - how the total text is divided among letters

Pie Charts

def pie(text, k=8, title="Character Shares (Pie Chart)", cmap_name="Set3", ignore_spaces=True):
    """
    Accepts either a string or a list of characters/words.
    """
    # If input is a list, join into one string
    if isinstance(text, list):
        text = " ".join(text)

    if not text:
        print("Empty text.")
        return

    if ignore_spaces:
        text = text.replace(" ", "")

    import matplotlib.pyplot as plt
    from collections import Counter

    c = Counter(text.lower())
    top_k = c.most_common(k)
    other_total = sum(v for _, v in list(c.items())[k:])

    labels = [ch for ch, _ in top_k]
    sizes = [v for _, v in top_k]
    if other_total > 0:
        labels.append("Other")
        sizes.append(other_total)

    cmap = plt.get_cmap(cmap_name)
    colors = cmap(range(len(labels)))

    plt.figure(figsize=(6, 6))
    plt.pie(sizes, labels=labels, autopct="%1.1f%%", startangle=90, colors=colors)
    plt.title(title)
    plt.tight_layout()
    plt.show()

Pie Charts

pie(alice, k = 5)

Line Charts

  • Good for ordered data, especially looking at the frequency of a large number of items
import matplotlib.pyplot as plt
from collections import Counter

def line(tokens, title="Line Chart: Rank vs Frequency", logy=False, marker="o"):
    """
    Draws a line chart showing word frequency by rank.
    - tokens: list of strings
    - logy: if True, uses log scale for frequency (useful for Zipf's Law)
    - marker: point style (e.g. 'o', '.', None)
    """
    if not tokens:
        print("No data.")
        return

    c = Counter(tokens)
    # Sort words by frequency
    freqs = sorted(c.values(), reverse=True)
    ranks = range(1, len(freqs) + 1)

    plt.figure(figsize=(8, 4))
    plt.plot(ranks, freqs, marker=marker)
    plt.xlabel("Rank (1 = most frequent)")
    plt.ylabel("Frequency")
    if logy:
        plt.yscale("log")
        plt.ylabel("Frequency (log scale)")
    plt.title(title)
    plt.tight_layout()
    plt.show()

Line Charts

# Regular scale
line(alice, title="Rank vs Frequency (Regular)")

Line Charts

# Log scale (shows Zipf-like pattern clearly)
line(alice, title="Rank vs Frequency (Log Scale)", logy=True)

Scatter Plots

  • Great for comparing two features per item (e.g., frequency vs length)
  • Our function here compares word length vs. frequency
import matplotlib.pyplot as plt
from collections import Counter
import pandas as pd

def scatter(tokens, title="Scatter Plot: Length vs Frequency", logy=False):
    """
    Draws a scatter plot showing the relationship between word length and frequency.
    - tokens: list of strings
    - logy: if True, uses a log scale for the frequency axis
    """
    if not tokens:
        print("No data.")
        return

    # Count how often each unique token appears
    c = Counter(tokens)
    df = pd.DataFrame({
        "token": list(c.keys()),
        "freq": list(c.values()),
        "length": [len(t) for t in c.keys()]
    })

    plt.figure(figsize=(8, 4))
    plt.scatter(df["length"], df["freq"])
    plt.xlabel("Word Length (characters)")
    plt.ylabel("Frequency")
    if logy:
        plt.yscale("log")
        plt.ylabel("Frequency (log scale)")
    plt.title(title)
    plt.tight_layout()
    plt.show()

    return df.sort_values("freq", ascending=False).reset_index(drop=True)

Scatter Plots

scatter(alice, title="Word Length vs Frequency (Normal Scale)")

Scatter Plots

# Same plot with log-scaled y-axis
scatter(alice, title="Word Length vs Frequency (Log Scale)", logy=True)

Word Clouds

  • Provides a nice visual to emphasize the most frequent tokens
from collections import Counter
import matplotlib.pyplot as plt

# Make sure to install the package once:
# pip install wordcloud

from wordcloud import WordCloud

def wordcloud_plot(tokens, title="Word Cloud", width=800, height=400, background_color="white"):
    """
    Draws a word cloud from a list of tokens.
    - tokens: list of strings
    - width, height: size of the generated image
    - background_color: 'white', 'black', etc.
    """
    if not tokens:
        print("No data.")
        return

    c = Counter(tokens)
    freqs = dict(c)

    wc = WordCloud(width=width, height=height, background_color=background_color)
    wc = wc.generate_from_frequencies(freqs)

    plt.figure(figsize=(10, 5))
    plt.imshow(wc, interpolation="bilinear")
    plt.axis("off")
    plt.title(title)
    plt.tight_layout()
    plt.show()

Word Clouds

wordcloud_plot(alice, title="Alice in Wonderland Word Cloud")

Final Activity

Final Activity

  • Load up a new book from the Gutenberg Project from past activities
  • Sketch out bar charts, pie charts, line charts, scatter plots, and word clouds using this data.