Keyword analysis¶
Keyword analysis asks which words are distinctive of one collection of texts. Two corpora are compared: the target, whose keywords we want, and the reference it is measured against. A word is a keyword when it is more frequent in the target than the reference frequency leads us to expect. The reference corpus establishes a baseline of comparison that we use to evaluate the words used in the target corpus.
Here we compare how men and women talk in the spoken part of the BNC, first with the chi-sq statistic (Rayson, et al. 1997), then with the t-test of Lijffijt et al. (2016), and finally we ask how much the two rankings agree.
References
- Rayson, P., Leech, G., and Hodges, M. 1997. Social differentiation in the use of English vocabulary: some analyses of the conversational component of the British National Corpus. International Journal of Corpus Linguistics. 2(1): 133-152.
- Lijffijt, J., T. Nevalainen, T. Säily, P. Papapetrou, K. Puolamäki, and H. Mannila. 2016. Significance testing of word frequencies in corpora. Digital Scholarship in the Humanities 31(2): 374-397.
import polars as pl
import polars_corpus as plc
from scipy.stats import spearmanr
import matplotlib.pyplot as plt
import seaborn as sns
pl.Config.set_tbl_rows(20)
pl.Config.set_float_precision(3)
pl.Config.set_thousands_separator(",")
polars.config.Config
pl.Config.set_tbl_rows(25)
pl.Config.set_float_precision(3)
polars.config.Config
The corpus¶
We read the BNC, restrict it to face-to-face conversation (CONVRSN), and keep the tokens whose speaker has a recorded sex -- the speaker metadata is a column on every token, so this is a filter rather than a join, and sex is null both for written texts and for speakers the BNC did not record. That column is what the target and reference corpora are split on below.
bnc = pl.scan_parquet("../../data/bnc.parquet").set_sorted("file_id")
bnc = bnc.filter(pl.col("text_type") == "CONVRSN", pl.col("sex").is_not_null()) \
.with_columns(pl.col("token").str.to_lowercase().alias("norm"))
Keyness by log-likelihood¶
The classic approach counts each word in the target and in the reference and tests the resulting 2x2 table for independence. With method='ll' that test is
the log-likelihood ratio $G^2$, which measures how far the observed counts fall from the counts we would expect if the word were spread across the two corpora
in proportion to their sizes. It is reported in the LogLik column, with the words most specific to the target first.
Swapping target and reference gives the other side of the comparison: the words distinctive of women's speech rather than men's. The two calls are separate analyses, not two halves of one, so each is ranked on its own.
mens_keywords = (
plc.keywords(
target=bnc.filter(pl.col("sex") == "Male"),
reference=bnc.filter(pl.col("sex") == "Female"),
method="ll",
expr=pl.col("token").str.to_lowercase(),
)
.filter(plc.is_letters("token"))
.head(100)
.select("token", "target_range", "LogLik")
.collect(engine="streaming")
)
mens_keywords.head(10)
| token | target_range | LogLik |
|---|---|---|
| str | u32 | f64 |
| "fucking" | 47 | 1,237.921 |
| "er" | 130 | 900.794 |
| "the" | 134 | 574.377 |
| "yeah" | 131 | 305.647 |
| "minus" | 10 | 299.750 |
| "aye" | 66 | 258.852 |
| "hundred" | 96 | 249.382 |
| "right" | 126 | 249.237 |
| "fuck" | 39 | 238.648 |
| "is" | 132 | 201.699 |
womens_keywords = (
plc.keywords(
target=bnc.filter(pl.col("sex") == "Female"),
reference=bnc.filter(pl.col("sex") == "Male"),
method="ll",
expr=pl.col("token").str.to_lowercase(),
)
.filter(plc.is_letters("token"))
.select("token", "target_range", "LogLik")
.collect(engine="streaming")
)
womens_keywords.head(10)
| token | target_range | LogLik |
|---|---|---|
| str | u32 | f64 |
| "she" | 130 | 3,373.992 |
| "her" | 129 | 1,017.036 |
| "said" | 131 | 915.451 |
| "n't" | 135 | 494.184 |
| "i" | 137 | 369.237 |
| "and" | 136 | 271.296 |
| "to" | 135 | 207.086 |
| "cos" | 129 | 204.473 |
| "christmas" | 86 | 175.169 |
| "oh" | 134 | 174.568 |
keyword_plot draws a ranked list as a lollipop chart, one stem per word,
positioned by its keyness score.
fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
plc.keyword_plot(
womens_keywords, "token", "LogLik", ax=ax1, top_k=20, descending=True, reverse=True
)
ax1.set_title("Women's keywords")
plc.keyword_plot(mens_keywords, "token", "LogLik", ax=ax2, top_k=20, descending=True)
ax2.set_title("Men's keywords")
plt.tight_layout()
Keyness as a difference in rates¶
$G^2$ pools each corpus into a single bag of words, so a word one talkative speaker uses frequently has the same impact as one used occasionally by many different speakers.
Lijffijt et al. (2016) argue for treating corpus samples as the unit of observation instead: compute the relative frequency of the word in each file and compare the two sets of rates with Welch's t-test. Files where the word never occurs count as a rate of zero, so a word confined to a few speakers gets a low mean and a large variance, and lands well down the list.
That is method='ttest', which returns only the words more frequent in the target, ranked by p ascending. t and p measure the evidence that the rates
differ and df is Welch's adjusted degrees of freedom; g is Hedges' $g$, the effect size, which is what to rank by when the question is how big the
difference is rather than how sure we are of it.
tt_mens_keywords = plc.keywords(
target=bnc.filter(pl.col("sex") == "Male"),
reference=bnc.filter(pl.col("sex") == "Female"),
method="ttest",
expr=pl.col("token").str.to_lowercase(),
).collect()
tt_mens_keywords
| token | target_freq | target_range | t | p | df | g |
|---|---|---|---|---|---|---|
| str | u32 | u32 | f64 | f64 | f64 | f64 |
| "er" | 9,415 | 130 | 3.979 | 0.000 | 207.298 | 0.480 |
| "which" | 1,417 | 106 | 3.687 | 0.000 | 192.910 | 0.445 |
| "c" | 515 | 74 | 3.657 | 0.000 | 201.534 | 0.442 |
| "quid" | 476 | 73 | 3.537 | 0.000 | 231.138 | 0.427 |
| "ah" | 2,374 | 122 | 3.418 | 0.001 | 206.286 | 0.413 |
| "hundred" | 1,473 | 96 | 3.346 | 0.001 | 240.972 | 0.404 |
| "against" | 175 | 64 | 3.331 | 0.001 | 197.162 | 0.402 |
| "chap" | 155 | 38 | 3.153 | 0.002 | 159.891 | 0.380 |
| "mate" | 266 | 44 | 3.151 | 0.002 | 163.768 | 0.380 |
| "a" | 28,415 | 133 | 3.096 | 0.002 | 255.185 | 0.374 |
| "simple" | 57 | 39 | 3.102 | 0.002 | 150.255 | 0.374 |
| "old" | 1,207 | 105 | 3.083 | 0.002 | 227.136 | 0.372 |
| "grand" | 110 | 37 | 3.028 | 0.003 | 202.687 | 0.366 |
| … | … | … | … | … | … | … |
| "compact" | 16 | 9 | 0.001 | 0.999 | 270.299 | 0.000 |
| "girly" | 1 | 1 | 0.001 | 0.999 | 249.216 | 0.000 |
| "tapes" | 133 | 45 | 0.001 | 0.999 | 266.692 | 0.000 |
| "jilly" | 1 | 1 | 0.000 | 1.000 | 270.995 | 0.000 |
| "kirby" | 3 | 2 | 0.000 | 1.000 | 270.995 | 0.000 |
| "speeches" | 2 | 2 | 0.000 | 1.000 | 255.414 | 0.000 |
| "lockerbie" | 1 | 1 | 0.000 | 1.000 | 270.996 | 0.000 |
| "bummer" | 1 | 1 | 0.000 | 1.000 | 222.108 | 0.000 |
| "ginny" | 2 | 2 | 0.000 | 1.000 | 262.675 | 0.000 |
| "coverage" | 2 | 2 | 0.000 | 1.000 | 266.671 | 0.000 |
| "tinker" | 5 | 3 | 0.000 | 1.000 | 247.782 | 0.000 |
| "trafalgar" | 2 | 2 | 0.000 | 1.000 | 270.989 | 0.000 |
tt_womens_keywords = plc.keywords(
target=bnc.filter(pl.col("sex") == "Female"),
reference=bnc.filter(pl.col("sex") == "Male"),
method="ttest",
expr=pl.col("token").str.to_lowercase(),
).collect()
tt_womens_keywords.sort(by="g", descending=True)
| token | target_freq | target_range | t | p | df | g |
|---|---|---|---|---|---|---|
| str | u32 | u32 | f64 | f64 | f64 | f64 |
| "she" | 22,807 | 130 | 7.102 | 0.000 | 214.777 | 0.856 |
| "her" | 7,306 | 129 | 4.983 | 0.000 | 270.939 | 0.602 |
| "to" | 40,934 | 135 | 4.964 | 0.000 | 270.949 | 0.599 |
| "lovely" | 1,217 | 99 | 4.706 | 0.000 | 203.745 | 0.567 |
| "children" | 571 | 78 | 3.961 | 0.000 | 241.274 | 0.478 |
| "did" | 11,556 | 132 | 3.932 | 0.000 | 262.944 | 0.474 |
| "shopping" | 305 | 71 | 3.923 | 0.000 | 183.692 | 0.472 |
| "likes" | 281 | 71 | 3.842 | 0.000 | 195.918 | 0.463 |
| "really" | 5,048 | 124 | 3.837 | 0.000 | 228.945 | 0.463 |
| "dress" | 133 | 46 | 3.775 | 0.000 | 179.104 | 0.454 |
| "cooking" | 139 | 46 | 3.686 | 0.000 | 146.055 | 0.443 |
| "husband" | 225 | 60 | 3.662 | 0.000 | 156.779 | 0.440 |
| "liked" | 198 | 70 | 3.637 | 0.000 | 204.759 | 0.438 |
| … | … | … | … | … | … | … |
| "apologising" | 1 | 1 | 0.001 | 0.999 | 270.999 | 0.000 |
| "sonar" | 1 | 1 | 0.001 | 0.999 | 270.999 | 0.000 |
| "clang" | 1 | 1 | 0.001 | 0.999 | 270.999 | 0.000 |
| "patio" | 19 | 15 | 0.001 | 0.999 | 265.075 | 0.000 |
| "contributed" | 1 | 1 | 0.001 | 0.999 | 270.999 | 0.000 |
| "acclimatized" | 1 | 1 | 0.001 | 0.999 | 270.999 | 0.000 |
| "boxing" | 28 | 12 | 0.000 | 1.000 | 245.949 | 0.000 |
| "plot" | 10 | 8 | 0.000 | 1.000 | 269.084 | 0.000 |
| "penchant" | 1 | 1 | 0.000 | 1.000 | 270.997 | 0.000 |
| "confused" | 16 | 13 | 0.000 | 1.000 | 265.464 | 0.000 |
| "classic" | 11 | 11 | 0.000 | 1.000 | 263.006 | 0.000 |
| "ye" | 57 | 29 | 0.000 | 1.000 | 270.317 | 0.000 |
The same back-to-back plot, now positioned by effect size rather than $G^2$. Note how much shorter the lists' shared vocabulary is: ranking by $g$ promotes words that most speakers of one group use a little more often over words that a few of them use a great deal.
fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
plc.keyword_plot(
tt_womens_keywords, "token", "g", ax=ax1, top_k=20, descending=True, reverse=True
)
ax1.set_title("Women's keywords")
plc.keyword_plot(tt_mens_keywords, "token", "g", ax=ax2, top_k=20, descending=True)
ax2.set_title("Men's keywords")
plt.tight_layout()
Do the two measures agree?¶
Joining the two women's keyword tables on token puts each word's LogLik and its t score side by side. The join is inner and the t-test table was cut to
its top 500, so this compares the two measures over the words the t-test ranks highest.
Spearman's rank correlation asks whether the two measures order those words the same way, and the scatter plot -- with $G^2$ on a log scale, since it spans orders of magnitude -- shows where they diverge.
both = tt_womens_keywords.head(500).join(womens_keywords, how="inner", on="token")
spearmanr(both["LogLik"], both["t"])
SignificanceResult(statistic=np.float64(0.5016223388449035), pvalue=np.float64(3.205435702046297e-33))
sns.regplot(both, x="t", y="LogLik", robust=True, scatter_kws=dict(alpha=0.1))
plt.xlabel("t")
plt.ylabel("G$^2$")
plt.semilogy()
[]