ASOIAF Motifs
Link to code
This page contains spoilers for all the books in A Song of Ice and
Fire, by George R. R. Martin. Do not continue unless you have
finished reading the series, or if you do not plan on ever reading
the series (which would be the wrong choice).
The purpose of this project is find textual motifs in ASOIAF by analyzing
the variations in word frequencies and bigram frequencies for each unique
point of view. Simple word frequencies will be used to find the likely
starts of motifs, and the bigrams following from those given starting
points. We will rank both word and bigram frequencies in each POV within
each book by:
(# occurrences in X POV chapters) / (# occurrences in all chapters)
Note that for testing purposes, we would expect "Where do whores go" to be
returned as a motif in Tyrion's chapters.
This page is my slightly edited stream of consciousness while working
towards this goal.
First, for the sake of making processing easier, we need to break down the
complete texts into separate files by POV, instead of by book. We also
want to remove the question marks, since those would confuse the bigram
finder (" "SomeWord " would be considered different from " SomeWord" ",
as well as every other variation of no quotes, quotes on one side, or even
quotes on both sides of a word, which is not the desired behavior.). We
also need to make all the text the same case (lowercase, here), so the
same phrase located at the beginning and the middle of a sentence will be
considered keys to the same count. Finally we need to clean up the white
space and remove all the unnecessary newlines, double spaces, etc. Here's
a sample of the cleaned text, from Arya's chapters in
A Game of Thrones:
who are you? arya asked. i am your dancing master. he tossed her one of
the wooden blades. she grabbed for it, missed, and heard it clatter to the
floor. tomorrow you will catch it. now pick it up. it was not just a
stick, but a true wooden sword complete with grip and guard and pommel.
arya picked it up and clutched it nervously with both hands, holding it
out in front of her.
The resulting files from this process can be accessed on github under the
povs/ directory (see the link to code at the top of this page).
Now, with the text all cleaned and sorted, we need to count the number
of times each word occurs in each POV. For each of these, I first cleaned
the text further to remove punctuation using the
re module. I used
the Counter object in the
collections module to get the actual word frequencies on the text
split on spaces. I kept a sum of the counts in each POV per book, the
total number of occurrences for each word in each book. This per book
count was stored using the
pickle module.
Next, I found the POV to book count ratios for each POV, per the formula
given above. These, I similarly stored using pickle. The first couple
results for each POV here tend to be, quite predictably, the names of the
narrator's companions which are either unknown or irrelevant to all the
other POVs. For example, Bran's top 4 results in A Storm of Swords,
all used exclusively by him (per the ratio being 1.0), are:
'osha': 1.0, 'frog': 1.0, "hodor's": 1.0, 'meera': 1.0
(As an aside, it's pretty entertaining looking at all the variations of
HOOODOOOOOR throughout the text. It might be interesting to graph the
correlation between frequency_of_hodors*average_hodor_length and some
sentiment analysis for Bran's chapters over time.)
Now, moving further down the list we see some more interesting results:
'summer': 0.6024096385542169, 'stories': 0.5357142857142857,
'causeway': 0.5333333333333333, 'lake': 0.5319148936170213,
'story': 0.5217391304347826, 'net': 0.5172413793103449, 'embers': 0.5,
'rat': 0.5, 'deer': 0.5, 'moonlight': 0.47368421052631576,
'stableboy': 0.46153846153846156, 'crypts': 0.45454545454545453,
'footsteps': 0.4444444444444444
It appears that between 0.25 and 0.8, we see a lot of environmental
descriptors and imagery. While this is not immediately helpful for our
motif finding goal, it's definitely worth taking another look at, perhaps
in another project. There is also evidence of a problem with our method
here. By making all the characters lowercase, we have unintentionally
combined the counts for 'summer' the season and 'Summer' the direwolf.
This would be a difficult problem to solve, however, since at the start
of a sentence, there would be no distinction between the two at all.
Given the small scale of this project, I'll just leave this as a 'won't
fix' known issue which we should keep in mind in our analysis, but really
isn't a big deal in the grand scheme of things.
The next step would be to calculate the bigram frequencies. I considered
using nltk's BigramCollocationFinder, but that wouldn't give me the counts
or the specific format I want the data to be in, so I decided to just do
this part by hand. I modeled a simple Bayesian network using a dictionary
where the top level keys are the first words in the bigrams, the second
level keys are the following words in the bigrams, and the value each
secondary key points to is the fraction of times the given secondary word
followed the given first word when the first word appeared, or:
dict['azor']['ahai'] = (# times 'azor ahai' occurs) / (# of times 'azor'
occurs)
The challenge here is determining whether punctuation should impact the
"motifiness" of a phrase containing a specific series of bigrams. For the
sake of this project, I decided to make a judgement call and say bigrams
where the first word ends in a period, i.e. this bigram crosses the end
of a sentence, do not count. Likewise, bigrams where the first word ends
in an exclamation point, question mark, etc., will not be counted.
However, commas will be allowed and simply ignored. That is to say,
occurrences of "x, y" will be counted toward the same total as "x y". I
chose to allow commas because they are so variable in allowed placements,
so should a character take an extra pause (I am equating commas to pauses,
for the sake of this exercise) while saying something that could be
considered a motif, that instance will still count toward said motif's
total count. That said, bear in mind my decision here is based off of
almost no data and severely impacted by my personal biases, so it is
definitely worth investigating bigram counts with a different
interpretation of the possible punctuations.
At this point, it's also worth noting that one of the main problems with
using bigram analysis to find motifs is that it is only possible to form
2-word assocations, and it is technically impossible to draw definite
continued phrases from that. Take, for example, the phrase:
"The sea was black and the moon was silver" (Victarion I, A Dance With
Dragons)
This phrase gets broken up into:
{'and': {'the': 1}, 'moon': {'was': 1}, 'black': {'and': 1}, 'sea':
{'was': 1}, 'the': {'sea': 1, 'moon': 1}, 'was': {'black': 1,
'silver': 1}}
From here, it is impossible to tell if the original phrase was "The sea
was black and the moon was silver" or "The sea was silver and the moon was
black." Therefore, we also need to crossreference each potential motif
with the original text to make sure it actually appears in the text.
Now would be a good time to revisit the example motif, "where do whores
go" for Tyrion in A Dance With Dragons. Broken into bigrams "where
do", "do whores", and "whores go", we can look at the number of
occurrences for each listed bigram divided by the total number of
occurrences for all bigrams begining with "where", "do", and "whores",
which we will call CTyrion for counts in that POV and
CAll for counts in all POVs:
Bigram |
CTyrion |
CAll |
CTyrion / CAll |
where -> do |
0.0409836065574 |
0.017825311943 |
2.29918032787 |
do -> whores |
0.0245398773006 |
0.00470035252644 |
5.22085889571 |
whores -> go |
0.179487179487 |
0.118644067797 |
1.51282051282 |
Judging from this, we should be able to find the bigrams with the highest
CPOV / CAll ratios and follow along the path of
bigrams, picking the most common next word and most common previous word,
until we hit a word that doesn't have a bigram with a ratio over, say,
1.5.
I ended up chosing to take the top two bigrams, rather than just the top
single bigram, for each iteration, mainly due to the fact that we are
looking for phrases greater than two words long, so we cannot guarentee
that every bigram in a motif will be the bigram with the highest ratio
given its first word. Two was an arbitrary cutoff, but for the purposes
of this project, seemed to serve well. I further verified each new
possible motif was still contained in the actual text of the books.
When we apply this algorithm to Tyrion's chapters in A Dance With
Dragons, we do, in fact, get our expected motif, "where do whores go"!
Interestingly, we even see a number of similar motifs to that one,
including "know where whores go," and "that be where whores go."
Overall, this algorithm did produce more false positives than desirable,
but the results were generally satisfactory. This number of false
positives could be expected, since not all POVs in all books contain
significant texual motifs. The results are listed, labeled by book and
POV, below.
Results
A Game of Thrones
Arya
- look with your eyes
- and arya felt the
- her wooden sword was
- the gold cloaks had
- they don't know me
- why not a second?
- the fat one said
- the fat man with
- if they caught her
- what are you talking
- the red cloak and
- was all her fault
- she might be able
- when they caught the
Bran
- a story about a
- the voice of robb the
- the big man said.
- no man can teach
- lord father took the
- face of robb the
- the long night and
- told him once that
- to watch over him
- grey wind and shaggydog
- the first men and
- if he didn't want
- you should be the
- and pulled himself up
- his hand on hodor's
Catelyn
- a stark in winterfell.
- on them when that
- no other way across
- have sent word of
- the steps were black
- i must go to
- a woman can be
Daenerys
- an arm around her
- i look back i
- with ten thousand dothraki
- the mother of mountains.
- the child inside her.
- keep me safe from harm.
- the free city of
- his long black hair
- we will go home
- she commanded her khas
- she commanded ser jorah.
- the knight looked at dany.
- hair until it shone
- her skin was cool
- her down off her
- the shadow lands beyond asshai
- he can have her
- your brother rhaegar was
- when she sent her
- yet even so dany
- that all things of
- she took him by
- the big house with
- her words in dothraki.
- she asked as irri
- she must not flinch
- stallion who mounts the
Eddard
- you win or you
- lords tyrell and redwyne
- her right leg to
- the king's voice and
- he commanded us to
- ser loras tyrell and
- ser loras tyrell was
- the city watch is
- when you play the
- and why was i
- you truly believe i
- of wine was i
Jon
- they led their horses
- watch needs all sorts
- just as your uncle was
- i didn't want him
- that this might be
- do we have to
- he found himself thinking
- his name is ghost.
- i don't care about
- the other boys were
- his throat was dry.
- it felt good to
- out from castle black
Prologue
- will had been a
- and it had been a
- it had been a few
Sansa
- the queen said so.
- i'll tell you what
- and told them to
- but this time sansa
- i'll be ever so
- he stood over her
- it please your grace
- the small council consents.
- she went on and
- the king has decreed.
- tell you what to
Tyrion
- it if we are
- that's what it is.
- come out through his
- you said they came at
- but this time tyrion
- he held out his
- was cold up here
- the moon brothers and
- his legs were cramping
- his legs were so
- the boy had said.
- no other men into
- must have looked as
- a big man i
- if truth be told
- the black on us
- i have three men
- out from under his
A Clash of Kings
Arya
- lord tywin had marched
- they don't know about the
- we could find a
- there's ghosts in harrenhal.
- i don't know about
- telling them how the
- the next morning he
- she wouldn't have let
- she grabbed for needle
- i'm not in chains
- wouldn't even know me
- they could find a
- to help me get those
- and who are you
- a man must have
- a bit her lip
- make certain she'd heard
Bran
- how much he hated
- to talk about the
- his hands around his
- the stark in winterfell
- you could see my
- but when i sleep
- that is coming to
- that is what the
- he could see them
- and pulled himself up
Catelyn
- her son had won
- been no easy thing
- my place is at
- is mine by rights.
- are no men like me.
- fell in beside her
- no need for battle
- the end was some
- they rode out in
- of men like you.
- i must do my duty.
- will not speak with
Daenerys
- the common tongue of
- it shall be as
- and die as blood
- the mother of dragons.
- and once for gold and
- a dead man with
- the first door on
- the first door to
- the first time she
- his eyes from her
- to keep me safe
- the great of qarth
- in this city of
- it is said in
- the red waste and
- to leave this city
- i have seen the
- i have given you
- dragon has three heads
Davos
- his sons to consider
- you have given me
- the red sword of
- a sword of fire!
- the first line was
- was on its way
- you do not love
- to bring down her
Jon
- to castle black they
- some men are like
- be back at castle black
- so far as jon
- you can see it
- that some men are
- i could tell you
- as far as jon
- looked up into a
Prologue
Sansa
- lady mother said i
- that will need to
- i don't want to.
- i don't want him
- lord stannis is no
- the gold cloaks of
- she reached out and
- he is no true knight
- not know what to
Theon
- lord balon had let
- that only made him
- and new with child.
- he should have kept
- so far as theon
- is dead may never die
- have come out on either
- he were still a
- to take my uncle
- and my uncle victarion
- he told himself to
- the boy will give
Tyrion
- until such time as
- we went down a
- i'll tell my sister.
- from me before you
- the high septon was
- the high septon to
- the gold cloaks had
- a word from me
- my own sweet sister
A Storm of Swords
Arya
- black iron coin that jaqen
- the outlaws had she
- his big yellow cloak
- some stupid little girl
- to care who she
- that lady smallwood gave
- is there gold hidden
- a stupid little girl
- he's just like they
- so she couldn't be
- sat down beside her.
- they looked more like
- stood on one leg
- but neither wolves nor
- but she couldn't be
- the three black dogs were
- it down beside her.
- you think we're going
- i don't even know
- you don't even know
- fell in love with
- it looked more like
- look so little to
- an arrow through his
Bran
- the thing that came in
- the green men had
- and who can say?
- the fat man was
- in one hand and
- the black brothers had
- had never heard this
- of them could find
Catelyn
- words can set it
- a table off its
- it does not always
- and too late for
- i took an arrow
- and how much i
- the table off its
- turned back to robb.
Daenerys
- the house of dust.
- had commanded the unsullied
- the dragon has three
- you told them i
- the city gates and
- the second sons were to
- he should never have kissed
- the seven kingdoms of
- and once for gold and
- i shall be betrayed
- i need the unsullied
- you must go back
- the day they are
- said that these eunuchs
- she found herself wondering
Davos
- of sea dragon tower
- one whose name may not
- the fiery heart on
- the fiery heart of
- the bastard of nightsong
- they will burn you
- she will burn you
- even now he prayed
- i tell him how
- had always been a
- he came up on
- the night that never ends.
Jaime
- my left hand i
- do something about that.
- in white sword tower
- want no trouble with
- my name is brienne
- my name is -
- his right hand that
- a lannister always pays
- get you safe to
- his left hand i
- bring you safe to
- want him dead as
- it fell to jaime
- her hands to jaime
- on your honor as
- had ever seen her
- of house lannister to
- about his brave companions
- to keep you safe
Jon
- king's tower where he
- the killing ground was
- that half so well
- i broke my vows with
- whatever is asked of
- with only one arm
- maester aemon sent a
- all men must die
- they should have stayed
- he walked past the
- all in black to
- no man knows that
- his sword hand opened and
- the two long blasts
- sun was high in
- had only one arm
- wall will stop them
- and kissed by fire
- you never found the
Prologue
- could be a dozen
- the old bear had the
Samwell
- the shadow tower men
- and give her a
- horse had died on
- we don't know that
- he'd told them all
- could get out his
- he reminded himself as
- i can't go on.
- it came out his
- he took another step.
- as we need more
- the first men with
- the first men and
- him but maester aemon
- but we don't know
Sansa
- the silver hair net
- the queen of thorns.
- him up to plant
- he got so drunk
- a moment she want
- not so drunk that
- was so drunk that
- his hand on her
- the thought made her
- he sat on her
Tyrion
- are won with quills
- the throne room for
- in red leather and
- a woman's hands are
- looked back over his
- you killed her children.
- of king joffrey's wedding
- the day before we
- his pale green eyes
- and your sister has
A Feast for Crows
Alayne
- him out of bed
- has a bastard daughter
- the way he is.
- and there will be
- you can have some
- it seemed as though
- her father had warned
- out of bed and
- well as her own.
Arya
- the house of black and
- and who are you?
- is no place for her.
- to him of many faces.
- he would find a
- the next day she
- the next day the
- you will find the
- you will find a
Brienne
- with blue eyes and
- a highborn maid of three-and-ten
- a maid of three-and-ten
- and broken men in
- i swear it by
- the big man laughed.
- are more like this
- she might well have
- one man told her
- should have used the
CatOfTheCanals
Cersei
- who called himself the
- the myrish woman was
- that our knight of
- the realm is going
- i love you too
- it could well be
- until such time as
- when my son was
- the gods were good
- you are here to
Jaime
- and osmund kettleblack and moon boy for
- send men after the
- the silent knight who
- it true that tywin was
- he made that clacking
- take up arms against
- and three hundred men
- of one last harvest.
- to make up for
Prologue
Samwell
- the wall with maester aemon
- tell them that aemon
- said something in braavosi.
- are no happy endings.
- he asked her what
- of men who can
- we might even have
- back at horn hill
- time they got the
- done with maester aemon.
- not know what to
Sansa
- he'd been at the
- that might have come
- has no need for
- not see him like
- but she never meant
- the big man in
- i never gave you
TheCaptainOfGuards
- the prince was still
- the children at their play.
TheDrownedMan
- on and on and on
- who shall be king over
- took up the cry
TheIronCaptain
ThePrincessInTheTower
TheProphet
- the priest had no
- the priest had been a
- and now he feasts
- the king is dead!
- had been born from
TheQueenmaker
TheReaver
- the sea and euron
- so he took her
TheSoiledKnight
- ser arys had a
- she will not be
- would never have seen
A Dance With Dragons
AGhostInWinterfell
Bran
- the heart tree in
- a thousand eyes and
- but in those days
- the big black birds
- into their eyes and
- he does not eat
- of them were gone
Cersei
- his grace king robert
- of your high holiness
- for all that she
- as his high holiness
- has been so long
- with them were the
- the men who had
- no one will tell
Daenerys
- done she sent her
- the green grace i
- a young girl and
- a young girl who
- must keep them safe
- the good masters had
- why did they give
- city that once was
- will soon be at
- would soon be at
- this one heard you
- all men must die
- the ships are yours
- she found herself thinking
- a tall man with
- she wondered how the
- her arms around her
- every day she sent
- it is known that
- i look back i
Davos
- that could be good
- had always been closed
- my friends of frey
- the three sisters had
- your head and hands
- man could see the
- to send you to
- to send him back
- send you to white
- and even the firth
- to your king stannis
- i have never had
- for your king stannis
- are you in sisterton
Epilogue
- an end to it
- but it would not
Jaime
Jon
- on days like this
- of castle black had
- help us hold the
- red woman might want
- the raven flapped its wings
- find another wet nurse
- for our common foe
- a grey girl on
- i fear you flatter
- had once told jon
- the bastard sword with
- to give up their
- i can only hope
- a broken sword with
- a few words of
Melisandre
Prologue
- i should have taken
- the flesh of man
Reek
- that other man had
- and remember who you
- your name is reek
- lord ramsay gave him
- lord ramsay gave the
- my name is reek
- he'd been before he
- will make you smell
- had to remember his
- will take another finger
- the way he smells
- but it had to
- could not let them
- to take another finger
- had come this way
Samwell
- and gave him a
- all that you were
- the man on the
TheBlindGirl
- the blind girl had learned
- of black and white
- not for his own
- not know that she
TheDiscardedKnight
TheDragontamer
- this will be the
- and he could hear
- of a brazen beast
TheGriffinReborn
TheIronSuitor
- who had seen the
- he had seen the
TheKingbreaker
TheLostLord
- they will not be
- do not have the
Theon
- he and her sisters
- we will be seen
- we will be a
ThePrinceOfWinterfell
- of white and grey
- he should have no
- i could be a
- and when he does
- grey and grey and grey
- would have no more
TheQueensguard
- hizdahr had not for
- it had been a
TheSacrifice
- but she could not
- the lord of light
- you have my word
TheSpurnedSuitor
- is what you want
- this is what i
TheTurncloak
- the snow was falling
- this is only a
- this is not my
- theon had left behind
- was no more than a
TheUglyLittleGirl
- the house of black and white
- the man she wanted
- is not for you
TheWatcher
- the white knight and
- the captain of guards
- him in king's landing
- i would not be
- back to king's landing
- of them in turn
TheWaywardBride
- the wolves have the
- die with an axe
- he might not have
- she could not have said
TheWindblown
- had it from old bill
- frog had it from
Tyrion
- our captain would prefer
- never reach her destination
- they set up for
- the nine free cities
- the fiery fingers were
- come up behind them
- with little bells on
- a lannister always pays
- would only serve to
- the serving men who
- and any other treasures
- are there any other
- know where whores go
- a game of cyvasse
- the fat man made
- the fat man began
- a woman's hands are warm
- where do whores go
- the common tongue as
- the common tongue of
- a very bad idea
- that be where whores go
- how did you sleep
- so hard that tyrion
- for another game of cyvasse
Victarion
And thanks to lordchair for
the raw texts of the books!