Week 6 · What words mean
Here, storm means Ororo, not the weather
Count the words on Storm's page and the comes first, 583 times. Weight each word by how few pages use it and storm rises to the top. We try eight ways of reading text on the same 303 Marvel pages, and they end with word vectors that put vision beside wanda here, and beside sense and image in everyday English. The alternative Week 6 post asks one question with the first half of these tools.
Part A · Documents, one row per page
IDF finds Marvel's own stopwords
The most frequent words on a page say the least about it, and A word's weight in one document. It's high when the word is frequent in that document and rare in the others. spots them using only the 303 pages themselves.
A word's Term frequency: how often a word appears in a document, divided by the number of words in that document. on a page is its count divided by the page's length. Its Document frequency: the number of documents a word appears in at least once. is how many of the 303 pages use it, and its Inverse document frequency: a weight that is high for a word few documents use and 0 for a word every document uses. is \ln \tfrac{303}{\text{DF}}. TF-IDF multiplies the two. He is on 291 pages, so its IDF is 0.04, while she is on 191, so \htmlClass{x-ln}{\ln} \tfrac{\htmlClass{x-n-docs}{303}}{\htmlClass{x-df}{191}} = \htmlClass{x-idf-value}{0.46}. Put together, they make a A table with one row per document and one column per word. Each cell says how much that word counts in that document. with one row per page and one column per word, where each cell holds a word's TF-IDF on that page.
A page's words by raw count and by TF-IDF
Twelve words each way for any of the 303 pages.
Method
A word is a run of letters, lowercased, with one inner apostrophe kept. The pages hold 727,261 words, 27,033 of them different.
Similarity only means something once the words are weighted
Under raw counts every page looks like every other. Under TF-IDF, most pairs share almost nothing.
Cosine similarity: how alike two lists of numbers are by their direction alone, ignoring their size. It's 1 when they point the same way and 0 when they share nothing. divides the dot product of two vectors by the product of their lengths. That gives the cosine of the angle between them, which is 1 when they point the same way and 0 when they share no word. Written out word by word, it's a sum with one term per word, so we can show which words carry it. Storm and the Human Torch have a raw-count cosine of 0.845, and the gives 0.396 of it. Under TF-IDF it's 0.297, and storm, Johnny Storm's surname, gives 0.234.
Two pages' cosine, word by word
24 pages a reader will know, which make 276 pairs. Each bar is one word's term in the sum.
Method
Tripling a page triples its raw counts but not its TF-IDF vector, because TF divides by the page's length. The analysis checks all 552 ordered pairs under both weightings, and no cosine moves by more than 10^{-15}.
Pronouns split women's pages from men's beyond chance
A tool that plots every word used by two groups of documents by how common it is in each group, so the words one group uses more stand out. places every word by how common it is in two groups of pages, and shuffling the groups shows how much of that is chance.
Wikidata labels 52 pages as women and 145 as men. The women's pages hold 197,637 words and the men's 397,709. Scattertext places each of the 3,188 words seen 20 times or more by its frequency rank in each group, and gives it a How far a value lies from what chance would give, measured in standard deviations, the typical size of chance's swings. The farther from 0, the less likely chance alone produced it. for how far it leans, in standard deviations. Anything beyond ±1.96 counts as a lean.
Every word's frequency rank on women's and men's pages
One dot per word seen 20 times or more. Large dots lean further than any shuffle of the labels gave. Her, she, his and he sit together in the top-right corner, common in both groups, so their lean shows in the lists below and not in where they sit. Distance from the diagonal hints at a lean, but many less common words sit far from it by chance. The z-score and the dot's colour decide which words lean.
Method
Scattertext's log-odds ratio with an informative Dirichlet prior (Monroe, Colaresi and Quinn 2008). The prior comes from the two groups' pooled counts, scaled to the class size. Gender is from Wikidata P21, and the 106 pages without a woman or man label are left out.
Topics move when the seed moves
Latent Dirichlet allocation: a topic model that treats each document as a mix of topics and each topic as a mix of words, and learns both from the text. finds recurring word groups, but which groups it finds depends on the number of topics and where it starts.
A A method that finds groups of words that tend to appear together across many documents and treats each group as a topic. looks for groups of words that recur together across documents, and LDA is one of them. It treats each topic as a distribution over words and each page as a mixture of topics. We fitted it with 5 to 12 topics, twice each, with seeds 0 and 1, on the 5,824 words left after removing names, stopwords, words under three letters, and words on fewer than 5 or more than 151 pages.
The topics of one fit, and one page's mixture
Top eight words per topic. Seed 1's topics are reordered to match seed 0's.
Method
scikit-learn LatentDirichletAllocation, batch learning, 50 iterations, words on 5 to 151 pages. We matched topics across seeds with the Hungarian algorithm on top-ten overlap.
Part B · Words, one row per word
Counting puts the first in almost every row
The The idea that a word's meaning can be read from the words that usually appear around it. says words that occur in similar contexts tend to have related meanings. But counted raw, the is the most common context of 24 of our 26 words.
A A table with one row per word and one column per neighbouring word. Each cell counts how often the two appear close together. has a row for each target word and a column for each context word. A cell counts how often the context word sits within the target's The context window: the words counted as a word's neighbours, a fixed number of words on either side of it. on one page. We count windows of 2, 5 and 10 words on either side.
One word's row of the word-context matrix
The fifteen largest cells of the row. The table is a corner of the matrix at window 5.
Method
We use the same tokens as section 1 and count them with scipy sparse matrices. The matrix is symmetric, so a row's total equals its column's.
Association, not frequency, finds the telling neighbours
Pointwise mutual information: how much more, or less, often two words appear together than they would if they had nothing to do with each other. asks whether two words meet more often than chance would have them meet, and Positive PMI: PMI with every negative score set to 0, so only pairs that meet more often than chance keep a score. keeps only the pairs where the answer is yes.
PMI is the natural log of how often a word and a context word meet, divided by how often they would meet if they were independent. A score above 0 means the pair meets more often than independence predicts. Below 0, it meets less often. PPMI sets the negatives to 0.
One word's neighbours by count, and by PMI or PPMI
Window 5. Plain PMI favours contexts seen once, and the PPMI list needs five sightings.
Method
The probabilities come from section 5's window-5 word-context matrix. P(w, c) is a cell over all cells, and P(w) is a row total over all cells.
Prediction packs a row into 100 numbers
A family of small neural networks that learn a short list of numbers for each word by predicting words from their neighbours. learns 100 numbers per word by predicting its neighbours. Retrained from another seed, A Word2Vec model that learns each word's numbers by predicting the words around it from the word itself. keeps 71% of each top ten, and symbiote's nearest are venom, brock, toxin and eddie.
Skip-gram predicts the context from the word, and Continuous bag of words: a Word2Vec model that learns each word's numbers by predicting the word from the words around it. predicts the word from its context. From Storm's opening words, skip-gram makes pairs such as (storm, is) and (storm, a), while CBOW makes one example, (is, a, superhero, appearing, in) → storm. Both train with A training shortcut. Instead of scoring every word in the vocabulary, the model learns to tell a real neighbour from a few randomly drawn words.. Each real pair is set against 5 random words, drawn more often the more common they are. Both are also A static word embedding gives each word one fixed list of numbers, learned from a corpus, whatever sentence the word appears in., with one vector per word whatever the sentence, so a word with two senses gets one vector for both. On these pages storm is the character, not the weather, and its nearest words are cyclops, panther, pryde and ororo. PPMI rows are a sparse static embedding of the same kind.
Nearest words under PPMI, and under skip-gram or CBOW
26 target words seen 20 times or more. Below the lists is one training step, with the real context set against sampled negatives.
Method
gensim 4.4 Word2Vec, 100 dimensions, window 5, words seen 20 times or more (3,746), 5 negatives, 20 epochs, one worker and a fixed hash so runs repeat. Each page counts as one sentence.
General English and Marvel English disagree
Global Vectors: a method that learns word vectors from how often every pair of words appears near each other across a whole corpus. learns static vectors from six billion words of news and Wikipedia, where the same words mean other things.
GloVe fits vectors to a whole corpus's word co-occurrence counts at once instead of predicting window by window. We took its 50-dimensional vectors, trained on Wikipedia 2014 and Gigaword 5, and compared each word's ten nearest with skip-gram's on the 303 pages, both ranked over the 3,645 words the two models know.
Nearest words in GloVe and in the Marvel skip-gram
Ten nearest words by cosine among the 3,645 words both models know. Highlighted words are in both lists.
Method
glove.6B, 50 dimensions (Pennington, Socher and Manning 2014), loaded with gensim. Only the neighbour lists ship with this page. Unrestricted, GloVe's nearest words for storm are hurricane, storms and winds, and none of them is in the Marvel vocabulary.
Play it as a timed game on the 303 Marvel pages
Cold Read
There are five levels, one per method. Each one is timed and scored, and the five scores add up to one total.
Clue Shop
TF-IDF
Flip word cards from a hidden Marvel page and name it. Rare words clear the board.
Whose Line
comparing groups
Two communities face off. Call which one uses a word more, or spot the one-page fluke.
Mix Desk
topic models
Read a page's words and spread ten chips over eight topics to guess its mix.
Tezgüino
context and PPMI
See only the words near a hidden word. Buy wider windows, PPMI or a peek, then name it.
Hot & Cold
word vectors
Guess a hidden word. Every guess lands on a radar by its cosine similarity.
Stuck on a level? Skip it for −500 points.
What the toolkit found
Weighting and association do the work. Counting alone turns up grammar, and a corpus decides what its words mean.
Takeaways
- IDF zeroes every word on all 303 pages, Marvel's own character, comics and marvel included, yet 151 of NLTK's 198 stopwords keep weight.
- Similarity needs weighting. Under raw counts the 99th-percentile pair of pages sits only 0.122 above the 0.754 median, and under TF-IDF it sits at eleven times the 0.0096 median.
- Only her, she, his and he split women's pages from men's further than any word in a label shuffle.
- At five topics, four of five keep at least half their top ten words when the seed changes. At eight, only four of eight do, so a topic's label is a reading, not a finding.
- By count, the is the top neighbour of 24 of 26 words, but its PMI is at most 0.70, so association finds the telling neighbours.
- 303 pages give stable Marvel neighbours. Retrained from another seed, skip-gram keeps 71% of each top ten, and symbiote's nearest are venom, brock, toxin and eddie.
- A static vector keeps its corpus's sense. Vision sits beside wiccan, wanda and witch on these pages and beside sense, concept and image in GloVe, and 11 of 26 words share no neighbour, even among words both models know.