Swizec Teller - a geek with a hatswizec.com

    Measuring vocabulary richness with python

    Pillowfights 51

    In preparation for a blogpost I'm going to make some time this week I found myself wanting to somehow parametrize vocabulary richness in a piece of text.

    btw, Code at bottom ;)

    It's an interesting problem because when you read something, it's pretty easy to see when an author is using rich vocabulary, but trying to reduce this observation to a simple number turns out to be a bit of a brainfuck. It's obviously somehow related to word frequencies and it seems obvious that what we want to measure is the distribution shape of word frequencies.

    Luckily googling around for about an hour turned up a clue. Way back in 1944 a statistician called G.U. Yule cracked this problem in a paper titled The statistical study of literary vocabulary. What he came up with is the so called Yule's K characteristic.

    Wikipedia is scarce on these things, so we know we're treading strange strange ground here. Persistent searching with google scholar turned up a version of the paper that wasn't paywalled to oblivion. However since it was on google books it was missing some key pages.

    Luckily what looks like a random homework for R explains perfectly how to implement Yule's K value:

    A complementary way of assessing the vocabulary difficulty of texts is to measure their lexical richness. Two indices one could use are Yule's K or Yule's I. These two are defined as follows: (1) Yule's K = 10,000⋅M 2−M 1 ÷ M 1⋅M 1 (2) Yule's I = M 1⋅M 1÷M 2−M 1  where M1 is the number of all word forms a text consists of and M2 is the sum of the products of each observed frequency to the power of two and the number of word types observed with that frequency (cf. Oakes 1998:204). For example, if one word occurs three times and four words occur five times, M2=(1*32)+(4*52)=109. The larger Yule's K, the smaller the diversity of the vocabulary (and thus, arguably, the easier the text). Since Yule's I is based on the reciprocal of Yule's K, the larger Yule's I, the larger the diversity of the vocabulary (and thus, arguably, the more difficult the text).

    Unfortunately I don't have the link anymore. It was a seriously random pdf I found online, the title seems to be "Quantitative corpus linguistics with R: a practical introduction"

    In hopes this blogpost saves somebody a few hours of googling when trying to measure vocabulary richness, here's my python implementation of Yule's K characteristic (or rather its inverse, Yule's I)

    from nltk.stem.porter import PorterStemmer
    from itertools import groupby
    def words(entry):
    return filter(lambda w: len(w) > 0,
    [w.strip("0123456789!:,.?(){}[]") for w in entry.split()])
    def yule(entry):
    # yule's I measure (the inverse of yule's K measure)
    # higher number is higher diversity - richer vocabulary
    d = {}
    stemmer = PorterStemmer()
    for w in words(entry):
    w = stemmer.stem(w).lower()
    d[w] += 1
    except KeyError:
    d[w] = 1
    M1 = float(len(d))
    M2 = sum([len(list(g))*(freq**2) for freq,g in groupby(sorted(d.values()))])
    return (M1*M1)/(M2-M1)
    except ZeroDivisionError:
    return 0

    For example the output of that function for this post is 21.6

    Just wish I knew how to make that middle part more functional-like. I don't like having weird for loops strewn about my code like that.

    Enhanced by Zemanta

    Did you enjoy this article?

    Published on September 28th, 2011 in Uncategorized

    Learned something new?
    Want to become an expert?

    Here's how it works 👇

    Leave your email and I'll send you thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

    Join Swizec's Newsletter

    And get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.

    "Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

    ~ Ashish Kumar

    Join over 14,000 engineers just like you already improving their careers with my letters, workshops, courses, and talks. ✌️

    Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

    Ready to Stop copy pasting D3 examples and create data visualizations of your own?  Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

    Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

    Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

    Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

    By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️

    Created bySwizecwith ❤️