Build a Key-word-in-context application

As we have seen, the sentiment results and the lexical dispersion plot only generate more questions about the text (which is a good thing). Of course these computational approaches should inevtibaly lead us back to closely reading the text, but even in that endeavour we can solicit the aid of R by creating a key-word-in-context search. Let’s undertake a key-word-in-context search of Moby-Dick.

setwd("~/Desktop/git-space/hacking-moby-dick/")
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.4.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
input.dir <- "texts/" #notice here that I migrated my moby-dick.txt into a sub-directory called "texts" for ease of processing and organization
file.v <- dir(input.dir, "moby-dick.txt")
file.v
## [1] "moby-dick.txt"
# Function takes a vector of file names and a directory path and
# returns a list in which each item in the list is an ordered
# vector of words from one of the files in the vector of file names 

make.file.word.v.l <- function(file.v, input.dir){
  text.word.vector.l <- list()
  # loop over the files
  for(i in 1:length(file.v)){
    # read the file in (notice that it is here that we need to know the input # directory
    text.v <- scan(paste(input.dir, file.v[i], sep="/"),
                   what="character", sep="\n") #convert to single string
    text.v <- paste(text.v, collapse=" ")
    #lowercase and split on non-word characters
    text.lower.v <- tolower(text.v)
    text.words.v <- strsplit(text.lower.v, "\\W")
    text.words.v <- unlist(text.words.v)
    #remove the blanks
    text.words.v <- text.words.v[which(text.words.v!="")]
    #use the index id from the files.v vector as the "name" in the list 
    text.word.vector.l[[file.v[i]]] <- text.words.v
  }
  return(text.word.vector.l)
}
# call the function with file.v and input.dir, put into new variable
my.corpus.l <- make.file.word.v.l(file.v, input.dir)
#show the first 100 words of the corpus
my.corpus.l[[1]][1:100]
##   [1] "etymology"    "supplied"     "by"           "a"           
##   [5] "late"         "consumptive"  "usher"        "to"          
##   [9] "a"            "grammar"      "school"       "the"         
##  [13] "pale"         "usher"        "threadbare"   "in"          
##  [17] "coat"         "heart"        "body"         "and"         
##  [21] "brain"        "i"            "see"          "him"         
##  [25] "now"          "he"           "was"          "ever"        
##  [29] "dusting"      "his"          "old"          "lexicons"    
##  [33] "and"          "grammars"     "with"         "a"           
##  [37] "queer"        "handkerchief" "mockingly"    "embellished" 
##  [41] "with"         "all"          "the"          "gay"         
##  [45] "flags"        "of"           "all"          "the"         
##  [49] "known"        "nations"      "of"           "the"         
##  [53] "world"        "he"           "loved"        "to"          
##  [57] "dust"         "his"          "old"          "grammars"    
##  [61] "it"           "somehow"      "mildly"       "reminded"    
##  [65] "him"          "of"           "his"          "mortality"   
##  [69] "while"        "you"          "take"         "in"          
##  [73] "hand"         "to"           "school"       "others"      
##  [77] "and"          "to"           "teach"        "them"        
##  [81] "by"           "what"         "name"         "a"           
##  [85] "whale"        "fish"         "is"           "to"          
##  [89] "be"           "called"       "in"           "our"         
##  [93] "tongue"       "leaving"      "out"          "through"     
##  [97] "ignorance"    "the"          "letter"       "h"

I have run a complicated for-loop that iterates over the words in the text file, separates them properly, and puts them in a vector (an ordered list). Now we can simply create new vectors that seek out a search term.

#search and find positions of word
great.positions.v <- which(my.corpus.l[[1]][]=="great")
great.positions.v
##   [1]    504    530    562    616    683    747    847    895   1124   1389
##  [11]   1789   1858   1977   2762   4313   5778   5921   6154   6716   7774
##  [21]   7816   8064   9756  10667  12920  13822  13927  14548  15100  15469
##  [31]  15631  16216  17724  17875  18977  20857  21055  21945  25408  25912
##  [41]  26055  27592  28587  28753  28790  29352  31800  36332  36844  36857
##  [51]  36924  36952  37091  37353  37557  37665  37910  39496  39824  40101
##  [61]  41504  41983  44734  44928  45183  45483  46458  46922  47037  47661
##  [71]  48190  48227  48801  49062  49443  51622  52405  52434  52914  52961
##  [81]  53105  53211  53260  54624  54665  54925  55245  55634  55831  56089
##  [91]  56590  56637  56854  57318  57827  60332  60406  60587  60836  61341
## [101]  61635  61713  62370  63781  64817  65112  65230  65259  66360  67000
## [111]  69764  69815  70493  70951  70982  72406  72582  73195  73522  73710
## [121]  73758  73847  74248  75376  76859  78042  78557  80005  80043  80245
## [131]  82165  82174  82229  82327  82577  85293  89143  89529  89978  91790
## [141]  94754  95653  97877 102793 103208 103621 103910 104487 104640 104827
## [151] 104860 105331 105541 106487 106932 107327 108223 108663 108922 109582
## [161] 110962 113666 114298 116721 116815 117036 118446 118607 118852 118998
## [171] 119748 120046 120209 120218 120419 120654 121036 122227 125001 125860
## [181] 127860 128400 129019 129117 129643 129805 129898 130438 130560 131655
## [191] 131872 131883 132297 132735 132812 132964 133525 133566 133584 134446
## [201] 134553 134711 134738 134837 135837 136325 136347 136355 136739 136769
## [211] 138327 138554 138743 139881 140358 140369 140758 140835 140924 141050
## [221] 141275 142110 142472 143108 143869 144867 145443 145779 146320 147505
## [231] 147991 148084 149529 151448 151886 152520 153825 154047 154483 155026
## [241] 158868 159152 162767 164716 164771 164975 165058 165090 165260 166254
## [251] 166426 166645 168580 168795 169158 169219 169331 169450 169990 170811
## [261] 171155 171244 171309 171488 171539 173365 173590 173750 173773 174041
## [271] 174259 174855 175284 175470 175811 175861 176082 176536 178199 178205
## [281] 178251 180403 181910 184072 185336 187799 191687 191754 195856 196097
## [291] 198882 202673 204044 206400 206725 207679 209278 210414 211092 213108
## [301] 215063 216700 218007 218325 218517 218521

This might look intimidating, but this shows you exactly in what position in the list these terms are occuring.

great.context <- great.positions.v
context <- 4
results <- for(i in 1:length(great.context)){
  start <- great.context[i]-context
  end <- great.context[i]+context
  before <- my.corpus.l[[1]][start:(start+context-1)]
  after <- my.corpus.l[[1]][(start+context+1) :end]
  keyword <- my.corpus.l[[1]][start+context]
  cat("----------", i, "----------", "\n")
  cat(before,"[",keyword, "]", after, "\n")
}
## ---------- 1 ---------- 
## extracts and god created [ great ] whales genesis leviathan maketh 
## ---------- 2 ---------- 
## lord had prepared a [ great ] fish to swallow up 
## ---------- 3 ---------- 
## with his sore and [ great ] and strong sword shall 
## ---------- 4 ---------- 
## all incontinently that foul [ great ] swallow of his and 
## ---------- 5 ---------- 
## when about sunrise a [ great ] many whales and other 
## ---------- 6 ---------- 
## had bones of very [ great ] value for their teeth 
## ---------- 7 ---------- 
## retires into it in [ great ] security and there sleeps 
## ---------- 8 ---------- 
## stowe s annals the [ great ] leviathan that maketh the 
## ---------- 9 ---------- 
## art is created that [ great ] leviathan called a commonwealth 
## ---------- 10 ---------- 
## to proceed with a [ great ] deal of caution for 
## ---------- 11 ---------- 
## make them speak like [ great ] wales goldsmith to johnson 
## ---------- 12 ---------- 
## they stand in so [ great ] dread of some of 
## ---------- 13 ---------- 
## whale fishery spain a [ great ] whale stranded on the 
## ---------- 14 ---------- 
## is a matter of [ great ] astonishment that the consideration 
## ---------- 15 ---------- 
## be athirst in the [ great ] american desert try this 
## ---------- 16 ---------- 
## overwhelming idea of the [ great ] whale himself such a 
## ---------- 17 ---------- 
## voyage was welcome the [ great ] flood gates of the 
## ---------- 18 ---------- 
## yet nantucket was her [ great ] original the tyre of 
## ---------- 19 ---------- 
## door it seemed the [ great ] black parliament sitting in 
## ---------- 20 ---------- 
## gigantic fish even the [ great ] leviathan himself in fact 
## ---------- 21 ---------- 
## cape horner in a [ great ] hurricane the half foundered 
## ---------- 22 ---------- 
## must have been a [ great ] central chimney with fireplaces 
## ---------- 23 ---------- 
## throwing them into the [ great ] stove in the middle 
## ---------- 24 ---------- 
## balmed new zealand heads [ great ] curios you know and 
## ---------- 25 ---------- 
## handle he puffed out [ great ] clouds of tobacco smoke 
## ---------- 26 ---------- 
## the window and a [ great ] rattling of coaches in 
## ---------- 27 ---------- 
## broad awake feeling a [ great ] deal worse than i 
## ---------- 28 ---------- 
## i was guilty of [ great ] rudeness staring at him 
## ---------- 29 ---------- 
## wrapped up in his [ great ] pilot monkey jacket and 
## ---------- 30 ---------- 
## always though ledyard the [ great ] new england traveller and 
## ---------- 31 ---------- 
## slightest bashfulness had boarded [ great ] whales on the high 
## ---------- 32 ---------- 
## reputation and joins the [ great ] whale fishery you should 
## ---------- 33 ---------- 
## he was a very [ great ] favourite he had been 
## ---------- 34 ---------- 
## melting sleet and his [ great ] pilot cloth jacket seemed 
## ---------- 35 ---------- 
## god had prepared a [ great ] fish to swallow up 
## ---------- 36 ---------- 
## for whose cause this [ great ] tempest was upon them 
## ---------- 37 ---------- 
## for his sake this [ great ] tempest was upon them 
## ---------- 38 ---------- 
## him who as the [ great ] pilot paul has it 
## ---------- 39 ---------- 
## fail to be of [ great ] usefulness to one who 
## ---------- 40 ---------- 
## punchbowl always forms the [ great ] central ornament on the 
## ---------- 41 ---------- 
## glance upwards to the [ great ] giver of all feasts 
## ---------- 42 ---------- 
## launching a navy of [ great ] ships on the sea 
## ---------- 43 ---------- 
## but being in a [ great ] hurry to resume scolding 
## ---------- 44 ---------- 
## we despatched it with [ great ] expedition when leaning back 
## ---------- 45 ---------- 
## the word cod with [ great ] emphasis and resumed my 
## ---------- 46 ---------- 
## many things queequeg placed [ great ] confidence in the excellence 
## ---------- 47 ---------- 
## for all men tragically [ great ] are made so through 
## ---------- 48 ---------- 
## no doubt before a [ great ] while he would follow 
## ---------- 49 ---------- 
## it was after a [ great ] feast given by his 
## ---------- 50 ---------- 
## the gaining of a [ great ] battle wherein fifty of 
## ---------- 51 ---------- 
## the custom when a [ great ] battle had been gained 
## ---------- 52 ---------- 
## they were placed in [ great ] wooden trenchers and garnished 
## ---------- 53 ---------- 
## he thought it a [ great ] pity that such a 
## ---------- 54 ---------- 
## rubbed them with his [ great ] yellow bandana handkerchief and 
## ---------- 55 ---------- 
## of us belong the [ great ] and everlasting first congregation 
## ---------- 56 ---------- 
## step along by the [ great ] anchor what a harpoon 
## ---------- 57 ---------- 
## cabin and to my [ great ] joy queequeg was soon 
## ---------- 58 ---------- 
## if he had a [ great ] secret in him morning 
## ---------- 59 ---------- 
## passed and there was [ great ] activity aboard the pequod 
## ---------- 60 ---------- 
## whalemen for besides the [ great ] length of the whaling 
## ---------- 61 ---------- 
## the king chiefs and [ great ] people generally were in 
## ---------- 62 ---------- 
## of profane words however [ great ] the hurry peleg said 
## ---------- 63 ---------- 
## nantucket that were as [ great ] and greater than your 
## ---------- 64 ---------- 
## in those parts that [ great ] america on the other 
## ---------- 65 ---------- 
## prince than alfred the [ great ] who with his own 
## ---------- 66 ---------- 
## more honourable than that [ great ] captain of antiquity who 
## ---------- 67 ---------- 
## was one of the [ great ] staple outfits of the 
## ---------- 68 ---------- 
## from god himself the [ great ] god absolute the centre 
## ---------- 69 ---------- 
## out in it thou [ great ] democratic god who didst 
## ---------- 70 ---------- 
## to think that the [ great ] leviathans had personally and 
## ---------- 71 ---------- 
## in quest of the [ great ] new england moose had 
## ---------- 72 ---------- 
## the wake of the [ great ] whales of the sea 
## ---------- 73 ---------- 
## sent for to the [ great ] quarter deck on high 
## ---------- 74 ---------- 
## the harpooneers with the [ great ] body of the crew 
## ---------- 75 ---------- 
## lofty trunk of a [ great ] tree when the upper 
## ---------- 76 ---------- 
## the sea and a [ great ] lord of leviathans was 
## ---------- 77 ---------- 
## were kicked by a [ great ] man and with a 
## ---------- 78 ---------- 
## greatest lords think it [ great ] glory to be slapped 
## ---------- 79 ---------- 
## of the whale the [ great ] cuvier and john hunter 
## ---------- 80 ---------- 
## the men small and [ great ] old and new landsmen 
## ---------- 81 ---------- 
## says nothing of the [ great ] sperm whale compared with 
## ---------- 82 ---------- 
## leviathanic allusions in the [ great ] poets of past days 
## ---------- 83 ---------- 
## whale is deposed the [ great ] sperm whale now reigneth 
## ---------- 84 ---------- 
## the black whale the [ great ] whale the true whale 
## ---------- 85 ---------- 
## folios it is the [ great ] mysticetus of the english 
## ---------- 86 ---------- 
## approaching to olive his [ great ] lips present a cable 
## ---------- 87 ---------- 
## whales it is of [ great ] importance to mention that 
## ---------- 88 ---------- 
## harbor he has a [ great ] pack on him like 
## ---------- 89 ---------- 
## then always at too [ great ] a distance to study 
## ---------- 90 ---------- 
## the advance of the [ great ] sperm whale book ii 
## ---------- 91 ---------- 
## days regarded as the [ great ] antidote against poison and 
## ---------- 92 ---------- 
## accounted an object of [ great ] curiosity black letter tells 
## ---------- 93 ---------- 
## he sometimes takes the [ great ] folio whales by the 
## ---------- 94 ---------- 
## will then see the [ great ] sperm whale himself in 
## ---------- 95 ---------- 
## unfinished even as the [ great ] cathedral of cologne was 
## ---------- 96 ---------- 
## to bring on a [ great ] baron of salt junk 
## ---------- 97 ---------- 
## his head into a [ great ] empty wooden trencher while 
## ---------- 98 ---------- 
## for all this the [ great ] negro was wonderfully abstemious 
## ---------- 99 ---------- 
## time though to his [ great ] delight the three salt 
## ---------- 100 ---------- 
## to it as that [ great ] stone mast of theirs 
## ---------- 101 ---------- 
## or louis the devil [ great ] washington too stands high 
## ---------- 102 ---------- 
## be fire but neither [ great ] washington nor napoleon nor 
## ---------- 103 ---------- 
## of it without running [ great ] risk of perishing like 
## ---------- 104 ---------- 
## it was not a [ great ] while after the affair 
## ---------- 105 ---------- 
## nantucket wool after the [ great ] annual sheep shearing aye 
## ---------- 106 ---------- 
## steward go draw the [ great ] measure of grog but 
## ---------- 107 ---------- 
## accountants have computed their [ great ] counting house the globe 
## ---------- 108 ---------- 
## vengeance will fetch a [ great ] premium here he smites 
## ---------- 109 ---------- 
## task what when the [ great ] pope washes the feet 
## ---------- 110 ---------- 
## more than ye ye [ great ] gods ever were i 
## ---------- 111 ---------- 
## which whale after doing [ great ] mischief to his assailants 
## ---------- 112 ---------- 
## not unfrequent instances of [ great ] ferocity cunning and malice 
## ---------- 113 ---------- 
## eminent tremendousness of the [ great ] sperm whale anywhere more 
## ---------- 114 ---------- 
## the surface remain in [ great ] part unaccountable to his 
## ---------- 115 ---------- 
## after sounding to a [ great ] depth he transports himself 
## ---------- 116 ---------- 
## one jot of his [ great ] natural intellect had perished 
## ---------- 117 ---------- 
## a broken throne the [ great ] gods mock that captive 
## ---------- 118 ---------- 
## have seemed the gliding [ great ] demon of the seas 
## ---------- 119 ---------- 
## white charger and the [ great ] austrian empire caesarian heir 
## ---------- 120 ---------- 
## in the greek mythologies [ great ] jove himself being made 
## ---------- 121 ---------- 
## could send to the [ great ] spirit with the annual 
## ---------- 122 ---------- 
## in white before the [ great ] white throne and the 
## ---------- 123 ---------- 
## spell but god s [ great ] unflattering laureate nature i 
## ---------- 124 ---------- 
## either wholly or in [ great ] part stripped of all 
## ---------- 125 ---------- 
## of her hues the [ great ] principle of light for 
## ---------- 126 ---------- 
## migrating whales may with [ great ] confidence be looked for 
## ---------- 127 ---------- 
## commander to make the [ great ] passage southwards double cape 
## ---------- 128 ---------- 
## to know an irascible [ great ] man they make distant 
## ---------- 129 ---------- 
## these famous whales enjoy [ great ] individual celebrity nay you 
## ---------- 130 ---------- 
## at various times creating [ great ] havoc among the boats 
## ---------- 131 ---------- 
## by stating that a [ great ] earthquake somewhere about that 
## ---------- 132 ---------- 
## time did actually do [ great ] mischief along the spanish 
## ---------- 133 ---------- 
## to me of the [ great ] power and malice at 
## ---------- 134 ---------- 
## the whale towing her [ great ] hull through the water 
## ---------- 135 ---------- 
## prefecture at constantinople a [ great ] sea monster was captured 
## ---------- 136 ---------- 
## post sweeping round his [ great ] steering oar lay back 
## ---------- 137 ---------- 
## the pequod and the [ great ] white whale its object 
## ---------- 138 ---------- 
## was famous for his [ great ] heedfulness in the fishery 
## ---------- 139 ---------- 
## whales is always under [ great ] and extraordinary difficulties that 
## ---------- 140 ---------- 
## a conscience and the [ great ] mundane soul were in 
## ---------- 141 ---------- 
## four corners of a [ great ] highway where you meet 
## ---------- 142 ---------- 
## are shored by two [ great ] contrasting nations as the 
## ---------- 143 ---------- 
## holy of holies of [ great ] forests on roman arches 
## ---------- 144 ---------- 
## in substance and its [ great ] items true i know 
## ---------- 145 ---------- 
## look now at a [ great ] christian painter s portrait 
## ---------- 146 ---------- 
## plates the whales like [ great ] rafts of logs are 
## ---------- 147 ---------- 
## count de lacepede a [ great ] naturalist published a scientific 
## ---------- 148 ---------- 
## in fact as the [ great ] hunter says the mere 
## ---------- 149 ---------- 
## needs conclude that the [ great ] leviathan is that one 
## ---------- 150 ---------- 
## published outlines of the [ great ] sperm whale colnett s 
## ---------- 151 ---------- 
## than theirs but by [ great ] odds beale s is 
## ---------- 152 ---------- 
## supper cooking in the [ great ] bowels below sea fowls 
## ---------- 153 ---------- 
## mell through the consecutive [ great ] battles of france where 
## ---------- 154 ---------- 
## of carving is as [ great ] a trophy of human 
## ---------- 155 ---------- 
## fail to trace out [ great ] whales in the starry 
## ---------- 156 ---------- 
## and as in the [ great ] hunting countries of india 
## ---------- 157 ---------- 
## in the distance a [ great ] white mass lazily rose 
## ---------- 158 ---------- 
## sir said flask the [ great ] live squid which they 
## ---------- 159 ---------- 
## to imagine that the [ great ] kraken of bishop pontoppodan 
## ---------- 160 ---------- 
## off with a prodigious [ great ] wedding cake to present 
## ---------- 161 ---------- 
## accustomed cry as the [ great ] fish slowly and regularly 
## ---------- 162 ---------- 
## moved for upon the [ great ] canal of hang ho 
## ---------- 163 ---------- 
## and according to the [ great ] military maxim make the 
## ---------- 164 ---------- 
## whale was esteemed a [ great ] delicacy in france and 
## ---------- 165 ---------- 
## them they had a [ great ] porpoise grant from the 
## ---------- 166 ---------- 
## richness he is the [ great ] prize ox of the 
## ---------- 167 ---------- 
## to this block the [ great ] blubber hook weighing some 
## ---------- 168 ---------- 
## is heard with a [ great ] swash the ship rolls 
## ---------- 169 ---------- 
## of the second alternating [ great ] tackle is then hooked 
## ---------- 170 ---------- 
## if it were a [ great ] live mass of plaited 
## ---------- 171 ---------- 
## his flanks effaced in [ great ] part of the regular 
## ---------- 172 ---------- 
## after explanation that this [ great ] monster to whom corporeal 
## ---------- 173 ---------- 
## the pole like the [ great ] dome of st peter 
## ---------- 174 ---------- 
## s and like the [ great ] whale retain o man 
## ---------- 175 ---------- 
## the joyous breezes that [ great ] mass of death floats 
## ---------- 176 ---------- 
## while in life the [ great ] whale s body may 
## ---------- 177 ---------- 
## it might yet in [ great ] part be buoyed up 
## ---------- 178 ---------- 
## he had been a [ great ] prophet in their cracked 
## ---------- 179 ---------- 
## gasping there with that [ great ] iron hook poor queequeg 
## ---------- 180 ---------- 
## distance they saw a [ great ] heap of tumultuous white 
## ---------- 181 ---------- 
## here now are two [ great ] whales laying their heads 
## ---------- 182 ---------- 
## between them like a [ great ] mountain separating two lakes 
## ---------- 183 ---------- 
## lens of herschel s [ great ] telescope and his ears 
## ---------- 184 ---------- 
## might descend into the [ great ] kentucky mammoth cave of 
## ---------- 185 ---------- 
## come nearer to this [ great ] head it begins to 
## ---------- 186 ---------- 
## case you will take [ great ] interest in thinking how 
## ---------- 187 ---------- 
## oil and more a [ great ] pity now that this 
## ---------- 188 ---------- 
## were inside of the [ great ] haarlem organ and gazing 
## ---------- 189 ---------- 
## s there is no [ great ] well of sperm no 
## ---------- 190 ---------- 
## lais chapter 77 the [ great ] heidelburgh tun now comes 
## ---------- 191 ---------- 
## be regarded as the [ great ] heidelburgh tun of the 
## ---------- 192 ---------- 
## and as that famous [ great ] tierce is mystically carved 
## ---------- 193 ---------- 
## the sperm whale s [ great ] heidelburgh tun is tapped 
## ---------- 194 ---------- 
## handed hold on the [ great ] cabled tackles suspending the 
## ---------- 195 ---------- 
## foremost down into this [ great ] tun of heidelburgh and 
## ---------- 196 ---------- 
## got foul of the [ great ] cutting tackles a sharp 
## ---------- 197 ---------- 
## be and might occasion [ great ] trouble he had thrust 
## ---------- 198 ---------- 
## foremost as for the [ great ] head itself that was 
## ---------- 199 ---------- 
## through the courage and [ great ] skill in obstetrics of 
## ---------- 200 ---------- 
## brow is as that [ great ] golden seal affixed by 
## ---------- 201 ---------- 
## deer but in the [ great ] sperm whale this high 
## ---------- 202 ---------- 
## a speech no his [ great ] genius is declared in 
## ---------- 203 ---------- 
## me that had the [ great ] sperm whale been known 
## ---------- 204 ---------- 
## s high seat the [ great ] sperm whale shall lord 
## ---------- 205 ---------- 
## whale and that the [ great ] monster is indomitable you 
## ---------- 206 ---------- 
## going all abreast with [ great ] speed straight before the 
## ---------- 207 ---------- 
## harness they left a [ great ] wide wake as though 
## ---------- 208 ---------- 
## though continually unrolling a [ great ] wide parchment upon the 
## ---------- 209 ---------- 
## were going with such [ great ] velocity moreover as almost 
## ---------- 210 ---------- 
## lowered but from the [ great ] start he had had 
## ---------- 211 ---------- 
## such thin threads the [ great ] leviathan was suspended like 
## ---------- 212 ---------- 
## next moment relieved in [ great ] part from the downward 
## ---------- 213 ---------- 
## of water at a [ great ] distance below the surface 
## ---------- 214 ---------- 
## sperm whale floats with [ great ] buoyancy with its side 
## ---------- 215 ---------- 
## i impressed with its [ great ] honourableness and antiquity and 
## ---------- 216 ---------- 
## i find so many [ great ] demi gods and heroes 
## ---------- 217 ---------- 
## doing battle with the [ great ] monster of the deep 
## ---------- 218 ---------- 
## horseback yet considering the [ great ] ignorance of those times 
## ---------- 219 ---------- 
## no other than the [ great ] leviathan himself in fact 
## ---------- 220 ---------- 
## a whale like their [ great ] patron let them never 
## ---------- 221 ---------- 
## nothing short of the [ great ] gods themselves that wondrous 
## ---------- 222 ---------- 
## the discovery of that [ great ] headland from bartholomew diaz 
## ---------- 223 ---------- 
## s was foremost by [ great ] exertion tashtego at last 
## ---------- 224 ---------- 
## of ages before the [ great ] whales should have been 
## ---------- 225 ---------- 
## o hunter as the [ great ] necessities that strike the 
## ---------- 226 ---------- 
## by considerations touching the [ great ] inherent dignity and sublimity 
## ---------- 227 ---------- 
## so much to the [ great ] strength of the masonry 
## ---------- 228 ---------- 
## can transcend it five [ great ] motions are peculiar to 
## ---------- 229 ---------- 
## would almost think a [ great ] gun had been discharged 
## ---------- 230 ---------- 
## in time for the [ great ] whaling season there by 
## ---------- 231 ---------- 
## herds sometimes embracing so [ great ] a multitude that it 
## ---------- 232 ---------- 
## miles and forming a [ great ] semicircle embracing one half 
## ---------- 233 ---------- 
## cried one to a [ great ] dromedary that of a 
## ---------- 234 ---------- 
## block ice when the [ great ] river hudson breaks up 
## ---------- 235 ---------- 
## aggregations now though such [ great ] bodies are at times 
## ---------- 236 ---------- 
## and so being a [ great ] traveller he leaves his 
## ---------- 237 ---------- 
## by reason of the [ great ] stress of her plunging 
## ---------- 238 ---------- 
## the matter the two [ great ] principles laid down in 
## ---------- 239 ---------- 
## fish what is the [ great ] globe itself but a 
## ---------- 240 ---------- 
## we have been at [ great ] trouble and peril and 
## ---------- 241 ---------- 
## von slack in his [ great ] work on smells a 
## ---------- 242 ---------- 
## honour to alexander the [ great ] chapter 93 the castaway 
## ---------- 243 ---------- 
## hatch we expose the [ great ] try pots two in 
## ---------- 244 ---------- 
## been related how the [ great ] leviathan is afar off 
## ---------- 245 ---------- 
## beheaded was killed his [ great ] padded surtout becomes the 
## ---------- 246 ---------- 
## is cool then the [ great ] hatchways are unsealed the 
## ---------- 247 ---------- 
## head are profanely piled [ great ] rusty casks lie about 
## ---------- 248 ---------- 
## the entire ship seems [ great ] leviathan himself while on 
## ---------- 249 ---------- 
## and put away the [ great ] hatch is scrubbed and 
## ---------- 250 ---------- 
## world and beneath the [ great ] equator and named after 
## ---------- 251 ---------- 
## his own mysterious self [ great ] pains small gains for 
## ---------- 252 ---------- 
## cheer yet oh the [ great ] sun is no fixture 
## ---------- 253 ---------- 
## open sea for the [ great ] swells now lift the 
## ---------- 254 ---------- 
## two previous and the [ great ] tackles were still aloft 
## ---------- 255 ---------- 
## the sea a bouncing [ great ] whale with a milky 
## ---------- 256 ---------- 
## humoredly well this old [ great ] grandfather with the white 
## ---------- 257 ---------- 
## and what a noble [ great ] whale it was the 
## ---------- 258 ---------- 
## harpoon let this old [ great ] grandfather have it but 
## ---------- 259 ---------- 
## my diet oh a [ great ] watcher and very dietetically 
## ---------- 260 ---------- 
## me there would be [ great ] glory in killing him 
## ---------- 261 ---------- 
## our lord 1775 this [ great ] whaling house was in 
## ---------- 262 ---------- 
## with civilized steel the [ great ] sperm whale and that 
## ---------- 263 ---------- 
## any sort in the [ great ] south sea the voyage 
## ---------- 264 ---------- 
## was thus that the [ great ] japanese whaling ground first 
## ---------- 265 ---------- 
## his cable for the [ great ] south sea of the 
## ---------- 266 ---------- 
## these latter was a [ great ] sperm whale which after 
## ---------- 267 ---------- 
## of the leaves the [ great ] sun seemed a flying 
## ---------- 268 ---------- 
## this din of the [ great ] world s loom thy 
## ---------- 269 ---------- 
## that arsacidean wood the [ great ] white worshipped skeleton lay 
## ---------- 270 ---------- 
## their yard sticks the [ great ] skull echoed and seizing 
## ---------- 271 ---------- 
## so that like a [ great ] chest of drawers you 
## ---------- 272 ---------- 
## the hull of a [ great ] ship new laid upon 
## ---------- 273 ---------- 
## mostly lie like the [ great ] knobbed blocks on a 
## ---------- 274 ---------- 
## lie in him like [ great ] cables and hawsers coiled 
## ---------- 275 ---------- 
## a mighty theme no [ great ] and enduring volume can 
## ---------- 276 ---------- 
## mason and also a [ great ] digger of ditches canals 
## ---------- 277 ---------- 
## disinterred in excavating the [ great ] docks of antwerp in 
## ---------- 278 ---------- 
## an apartment of the [ great ] temple of denderah some 
## ---------- 279 ---------- 
## still survive there in [ great ] numbers much more may 
## ---------- 280 ---------- 
## much more may the [ great ] whale outlast all hunting 
## ---------- 281 ---------- 
## that from the presumed [ great ] longevity of whales their 
## ---------- 282 ---------- 
## that kept him a [ great ] part of the time 
## ---------- 283 ---------- 
## and there s a [ great ] cry for life boats 
## ---------- 284 ---------- 
## queequeg s measure with [ great ] accuracy regularly chalking queequeg 
## ---------- 285 ---------- 
## at last upon the [ great ] south sea were it 
## ---------- 286 ---------- 
## and stretched to a [ great ] tension pressing his foot 
## ---------- 287 ---------- 
## did not escape a [ great ] rolling sea dashing high 
## ---------- 288 ---------- 
## wave has such a [ great ] long start before it 
## ---------- 289 ---------- 
## of us are ahabs [ great ] god forbid but is 
## ---------- 290 ---------- 
## with all the crew [ great ] god where art thou 
## ---------- 291 ---------- 
## were the resort of [ great ] numbers of seals and 
## ---------- 292 ---------- 
## their black seventy fours [ great ] admirals sometimes sit at 
## ---------- 293 ---------- 
## circle commanded at so [ great ] a height when in 
## ---------- 294 ---------- 
## arm but if the [ great ] sun move not of 
## ---------- 295 ---------- 
## odor sometimes to a [ great ] distance given forth by 
## ---------- 296 ---------- 
## not jove not that [ great ] majesty supreme did surpass 
## ---------- 297 ---------- 
## an instant s compass [ great ] hearts sometimes condense to 
## ---------- 298 ---------- 
## confidence acquired by some [ great ] natural geniuses among the 
## ---------- 299 ---------- 
## the wind that made [ great ] bellies of their sails 
## ---------- 300 ---------- 
## ll slay him yet [ great ] god but for one 
## ---------- 301 ---------- 
## man that weeps how [ great ] the agony of the 
## ---------- 302 ---------- 
## spout curled round his [ great ] monadnock hump he was 
## ---------- 303 ---------- 
## then turned the ship [ great ] god where is the 
## ---------- 304 ---------- 
## all collapsed and the [ great ] shroud of the sea 
## ---------- 305 ---------- 
## and owing to its [ great ] buoyancy rising with great 
## ---------- 306 ---------- 
## great buoyancy rising with [ great ] force the coffin life

Suppose we want to combine terms with great—say, with some other negative terms from the sentiment analysis.

death.positions.v <- which(my.corpus.l[[1]][]=="death")
dead.positions.v <- which(my.corpus.l[[1]][]=="dead")
dark.positions.v <- which(my.corpus.l[[1]][]=="dark")
# now combine the three search-placement vectors into one wit the 'c' combine function
dark.words.md <- (c(death.positions.v, dead.positions.v, dark.positions.v))

dark.words.context <- dark.words.md
context <- 4 # here you can adjust the context
results <- for(i in 1:length(dark.words.context)){
  start <- dark.words.context[i]-context
  end <- dark.words.context[i]+context
  before <- my.corpus.l[[1]][start:(start+context-1)]
  after <- my.corpus.l[[1]][(start+context+1) :end]
  keyword <- my.corpus.l[[1]][start+context]
  cat("----------", i, "----------", "\n")
  cat(before,"[",keyword, "]", after, "\n")
}
## ---------- 1 ---------- 
## history of life and [ death ] the sovereignest thing on 
## ---------- 2 ---------- 
## to the sport of [ death ] the crews repair rodmond 
## ---------- 3 ---------- 
## of which the wight [ death ] is the only glazier 
## ---------- 4 ---------- 
## ever have gone a [ death ] harvesting with such a 
## ---------- 5 ---------- 
## the sailors deliriums and [ death ] abominable are the tumblers 
## ---------- 6 ---------- 
## as though naught but [ death ] should part us twain 
## ---------- 7 ---------- 
## life insurance companies pay [ death ] forfeitures upon immortals in 
## ---------- 8 ---------- 
## brevet yes there is [ death ] in this business of 
## ---------- 9 ---------- 
## matter of life and [ death ] methinks that what they 
## ---------- 10 ---------- 
## man who bleeds to [ death ] for conscience is the 
## ---------- 11 ---------- 
## have the fear of [ death ] how then can st 
## ---------- 12 ---------- 
## thou not think of [ death ] and the judgment then 
## ---------- 13 ---------- 
## the ship would sink [ death ] and the judgment then 
## ---------- 14 ---------- 
## and aft think of [ death ] and the judgment then 
## ---------- 15 ---------- 
## time to think about [ death ] then life was what 
## ---------- 16 ---------- 
## undone if at my [ death ] my executors or more 
## ---------- 17 ---------- 
## whale in the very [ death ] lock of the fight 
## ---------- 18 ---------- 
## converted the jaws of [ death ] into an easy chair 
## ---------- 19 ---------- 
## what he thought of [ death ] itself there is no 
## ---------- 20 ---------- 
## aught that looks like [ death ] among sea commanders the 
## ---------- 21 ---------- 
## brute is worried to [ death ] the killer is never 
## ---------- 22 ---------- 
## jib in a squall [ death ] and devils men it 
## ---------- 23 ---------- 
## for the jaws of [ death ] too captain ahab if 
## ---------- 24 ---------- 
## deathful whaleboat s bow [ death ] to moby dick god 
## ---------- 25 ---------- 
## moby dick to his [ death ] the long barbed steel 
## ---------- 26 ---------- 
## as to cause instantaneous [ death ] and however the general 
## ---------- 27 ---------- 
## that every dismembering or [ death ] that he caused was 
## ---------- 28 ---------- 
## white silent stillness of [ death ] in this shark and 
## ---------- 29 ---------- 
## in forecastle stories after [ death ] but he was admitted 
## ---------- 30 ---------- 
## which had had a [ death ] by a whale some 
## ---------- 31 ---------- 
## began to prepare for [ death ] and indeed the shock 
## ---------- 32 ---------- 
## facing the life and [ death ] peril so close to 
## ---------- 33 ---------- 
## in these jaws of [ death ] in vain we hailed 
## ---------- 34 ---------- 
## limb all these and [ death ] itself seem to him 
## ---------- 35 ---------- 
## himself back foremost into [ death ] s jaws can t 
## ---------- 36 ---------- 
## i survived myself my [ death ] and burial were locked 
## ---------- 37 ---------- 
## cool collected dive at [ death ] and destruction and the 
## ---------- 38 ---------- 
## tap on life and [ death ] this old man walked 
## ---------- 39 ---------- 
## on there like grim [ death ] chapter 54 the town 
## ---------- 40 ---------- 
## that his steelkilt s [ death ] would be the signal 
## ---------- 41 ---------- 
## seek to gag in [ death ] the vital jaw of 
## ---------- 42 ---------- 
## with steelkilt since the [ death ] of radney chapter 55 
## ---------- 43 ---------- 
## into the jaws of [ death ] with a halter around 
## ---------- 44 ---------- 
## swift sudden turn of [ death ] that mortals realize the 
## ---------- 45 ---------- 
## start her like grim [ death ] and grinning devils and 
## ---------- 46 ---------- 
## its flexibility even in [ death ] causes it to sink 
## ---------- 47 ---------- 
## that great mass of [ death ] floats on and on 
## ---------- 48 ---------- 
## his foes in his [ death ] his ghost becomes a 
## ---------- 49 ---------- 
## of such a letter [ death ] himself might well have 
## ---------- 50 ---------- 
## into unmerited disaster and [ death ] therefore i saw that 
## ---------- 51 ---------- 
## speculative indifference as to [ death ] but mark the other 
## ---------- 52 ---------- 
## practical resolution in facing [ death ] this right whale i 
## ---------- 53 ---------- 
## to the air after [ death ] it soon begins to 
## ---------- 54 ---------- 
## be recalled the delicious [ death ] of an ohio honey 
## ---------- 55 ---------- 
## wires the life and [ death ] throbs of the whale 
## ---------- 56 ---------- 
## he must die the [ death ] and be murdered in 
## ---------- 57 ---------- 
## bows it was his [ death ] stroke for by this 
## ---------- 58 ---------- 
## dashing each other to [ death ] best therefore withhold any 
## ---------- 59 ---------- 
## bewildered feeling as of [ death ] came over me convulsively 
## ---------- 60 ---------- 
## in this vale of [ death ] god girds us round 
## ---------- 61 ---------- 
## them and a jolly [ death ] and that fine gam 
## ---------- 62 ---------- 
## a skeleton life folded [ death ] death trellised life the 
## ---------- 63 ---------- 
## skeleton life folded death [ death ] trellised life the grim 
## ---------- 64 ---------- 
## pass it without immediate [ death ] but the truth of 
## ---------- 65 ---------- 
## driven one leg to [ death ] and spavined the other 
## ---------- 66 ---------- 
## of the door of [ death ] how he wasted and 
## ---------- 67 ---------- 
## the drawing near of [ death ] which alike levels all 
## ---------- 68 ---------- 
## something vile to the [ death ] devouring sharks no he 
## ---------- 69 ---------- 
## made every preparation for [ death ] now that his coffin 
## ---------- 70 ---------- 
## woe on woe oh [ death ] why canst thou not 
## ---------- 71 ---------- 
## care killing competency but [ death ] plucked down some virtuous 
## ---------- 72 ---------- 
## scorn to flaxen curls [ death ] seems the only desirable 
## ---------- 73 ---------- 
## career like this but [ death ] is only a launching 
## ---------- 74 ---------- 
## unshored therefore to the [ death ] longing eyes of such 
## ---------- 75 ---------- 
## the guilt of intermediate [ death ] here are wonders supernatural 
## ---------- 76 ---------- 
## is more oblivious than [ death ] come hither put up 
## ---------- 77 ---------- 
## it of the true [ death ] temper ahoy there tashtego 
## ---------- 78 ---------- 
## no sooner dead than [ death ] whirls round the corpse 
## ---------- 79 ---------- 
## how fair fair for [ death ] and doom that s 
## ---------- 80 ---------- 
## wake thee not to [ death ] old man who can 
## ---------- 81 ---------- 
## door he placed the [ death ] tube in its rack 
## ---------- 82 ---------- 
## dreaded symbol of grim [ death ] by a mere hap 
## ---------- 83 ---------- 
## hand i hold his [ death ] tempered in blood and 
## ---------- 84 ---------- 
## the onset a pale [ death ] glimmer lit up fedallah 
## ---------- 85 ---------- 
## that little word what [ death ] knell rings in it 
## ---------- 86 ---------- 
## mouldy and over salted [ death ] though cherries cherries cherries 
## ---------- 87 ---------- 
## and pole pointed prow [ death ] glorious ship must ye 
## ---------- 88 ---------- 
## shipwrecked captains oh lonely [ death ] on lonely life oh 
## ---------- 89 ---------- 
## piled comber of my [ death ] towards thee i roll 
## ---------- 90 ---------- 
## savage beneath in his [ death ] gasp kept his hammer 
## ---------- 91 ---------- 
## found to be a [ dead ] whale which some asiatics 
## ---------- 92 ---------- 
## place where the first [ dead ] american whale was stranded 
## ---------- 93 ---------- 
## the nightmare to a [ dead ] sartainty landlord i whispered 
## ---------- 94 ---------- 
## selling the heads of [ dead ] idolators depend upon it 
## ---------- 95 ---------- 
## my room at the [ dead ] of night in fact 
## ---------- 96 ---------- 
## a sportsman bagging a [ dead ] woodcock all these queer 
## ---------- 97 ---------- 
## them and duelled them [ dead ] without winking and yet 
## ---------- 98 ---------- 
## afresh oh ye whose [ dead ] lie buried beneath the 
## ---------- 99 ---------- 
## of living creatures the [ dead ] of mankind are included 
## ---------- 100 ---------- 
## to hush all the [ dead ] wherefore but the rumor 
## ---------- 101 ---------- 
## and even from these [ dead ] doubts she gathers her 
## ---------- 102 ---------- 
## and shrieks in his [ dead ] ear what meanest thou 
## ---------- 103 ---------- 
## now consider his father [ dead ] and gone he being 
## ---------- 104 ---------- 
## of _ile_ was found [ dead ] in my first floor 
## ---------- 105 ---------- 
## eye why dad whale [ dead ] quick bildad said peleg 
## ---------- 106 ---------- 
## when he lay like [ dead ] for three days and 
## ---------- 107 ---------- 
## mystic ways and so [ dead ] to anything like an 
## ---------- 108 ---------- 
## last office for the [ dead ] would find a birth 
## ---------- 109 ---------- 
## home nothing but the [ dead ] wintry bleakness of the 
## ---------- 110 ---------- 
## living thump and a [ dead ] thump that s what 
## ---------- 111 ---------- 
## undoubted superiority over the [ dead ] level of the mass 
## ---------- 112 ---------- 
## pull to men a [ dead ] whale or a stove 
## ---------- 113 ---------- 
## brought me to this [ dead ] stump i stand on 
## ---------- 114 ---------- 
## would have dropped ye [ dead ] perchance ye need it 
## ---------- 115 ---------- 
## cabin builded over the [ dead ] water of the wake 
## ---------- 116 ---------- 
## romish mass for the [ dead ] begins with requiem eternam 
## ---------- 117 ---------- 
## the aspect of the [ dead ] which most appals the 
## ---------- 118 ---------- 
## that pallor of the [ dead ] we borrow the expressive 
## ---------- 119 ---------- 
## afterwards taken from the [ dead ] fish in the three 
## ---------- 120 ---------- 
## first battle not the [ dead ] man s ghost encountering 
## ---------- 121 ---------- 
## to three whales running [ dead ] to leeward our sail 
## ---------- 122 ---------- 
## every stroke of his [ dead ] limb sounded like a 
## ---------- 123 ---------- 
## hours would stand gazing [ dead ] to windward while an 
## ---------- 124 ---------- 
## along the deck like [ dead ] cattle and side by 
## ---------- 125 ---------- 
## to give up its [ dead ] still in dreams sees 
## ---------- 126 ---------- 
## inert mass of a [ dead ] whale a conquered fortress 
## ---------- 127 ---------- 
## and raise the buried [ dead ] perpendicular out of their 
## ---------- 128 ---------- 
## had burst he s [ dead ] mr stubb said daggoo 
## ---------- 129 ---------- 
## mouth stubb scattered the [ dead ] ashes over the water 
## ---------- 130 ---------- 
## that the creature was [ dead ] some vague dissatisfaction or 
## ---------- 131 ---------- 
## the sight of that [ dead ] body reminded him that 
## ---------- 132 ---------- 
## sharks swarming round the [ dead ] leviathan smackingly feasted on 
## ---------- 133 ---------- 
## the table at the [ dead ] meat and though were 
## ---------- 134 ---------- 
## carried anywhere or a [ dead ] slave to be decently 
## ---------- 135 ---------- 
## spirits than around a [ dead ] sperm whale moored by 
## ---------- 136 ---------- 
## command came to a [ dead ] stop on the opposite 
## ---------- 137 ---------- 
## cook when you are [ dead ] but don t you 
## ---------- 138 ---------- 
## the long rows of [ dead ] quadrupeds does not that 
## ---------- 139 ---------- 
## to shut down the [ dead ] lid of his murderous 
## ---------- 140 ---------- 
## true from the unmarred [ dead ] body of the whale 
## ---------- 141 ---------- 
## the man being stark [ dead ] the whole calamity with 
## ---------- 142 ---------- 
## think of the blasphemer [ dead ] and down there beware 
## ---------- 143 ---------- 
## macey and he s [ dead ] poor fellow poor fellow 
## ---------- 144 ---------- 
## scrabble scramble upon the [ dead ] whale s back you 
## ---------- 145 ---------- 
## such prey as a [ dead ] whale the otherwise miscellaneously 
## ---------- 146 ---------- 
## that the devil was [ dead ] did you ever see 
## ---------- 147 ---------- 
## this whale is not [ dead ] he is only dispirited 
## ---------- 148 ---------- 
## s head is a [ dead ] blind wall without a 
## ---------- 149 ---------- 
## mark unerringly impelling this [ dead ] impregnable uninjurable wall and 
## ---------- 150 ---------- 
## frequently found in the [ dead ] bodies of captured whales 
## ---------- 151 ---------- 
## for it usually the [ dead ] sperm whale floats with 
## ---------- 152 ---------- 
## floating body of a [ dead ] whale even as the 
## ---------- 153 ---------- 
## russian campaign turned their [ dead ] horses into tents and 
## ---------- 154 ---------- 
## floating body of a [ dead ] whale both to mark 
## ---------- 155 ---------- 
## fast fish alive or [ dead ] a fish is technically 
## ---------- 156 ---------- 
## is that living or [ dead ] if but decently treated 
## ---------- 157 ---------- 
## when sailors in a [ dead ] calm bathe in the 
## ---------- 158 ---------- 
## a grenadier carrying a [ dead ] comrade from the field 
## ---------- 159 ---------- 
## the congregation of the [ dead ] give not thyself up 
## ---------- 160 ---------- 
## gale had been found [ dead ] and stranded with his 
## ---------- 161 ---------- 
## merely poring over his [ dead ] attenuated skeleton stretched in 
## ---------- 162 ---------- 
## are oftentimes cast up [ dead ] upon that shore the 
## ---------- 163 ---------- 
## the condition of that [ dead ] bone upon which he 
## ---------- 164 ---------- 
## marble senate of the [ dead ] captain peleg s bruited 
## ---------- 165 ---------- 
## now for working in [ dead ] lumber saw a live 
## ---------- 166 ---------- 
## and when thou art [ dead ] never bury thyself under 
## ---------- 167 ---------- 
## elsewhere seen mount his [ dead ] back in a rolling 
## ---------- 168 ---------- 
## an author from the [ dead ] could adequately tell so 
## ---------- 169 ---------- 
## who after embalming a [ dead ] warrior stretched him out 
## ---------- 170 ---------- 
## long parched by the [ dead ] drought of the earthy 
## ---------- 171 ---------- 
## but see no sooner [ dead ] than death whirls round 
## ---------- 172 ---------- 
## thrust upright into the [ dead ] whale s spout hole 
## ---------- 173 ---------- 
## and ere noon the [ dead ] whale was brought to 
## ---------- 174 ---------- 
## with his live and [ dead ] feet a sneering triumph 
## ---------- 175 ---------- 
## his way by mere [ dead ] reckoning of the error 
## ---------- 176 ---------- 
## with the reel the [ dead ] blind wall butts all 
## ---------- 177 ---------- 
## only yesterday but were [ dead ] ere night only that 
## ---------- 178 ---------- 
## the white whale is [ dead ] and then whosoever of 
## ---------- 179 ---------- 
## more me than this [ dead ] one that s lost 
## ---------- 180 ---------- 
## aloft there which way [ dead ] to leeward sir up 
## ---------- 181 ---------- 
## all by heaven this [ dead ] wood has the better 
## ---------- 182 ---------- 
## some ships made of [ dead ] trees outlast the lives 
## ---------- 183 ---------- 
## looking nay a very [ dark ] and dismal night bitingly 
## ---------- 184 ---------- 
## the room stands a [ dark ] looking den the bar 
## ---------- 185 ---------- 
## the harpooneer is a [ dark ] complexioned chap he never 
## ---------- 186 ---------- 
## feel suspicious of this [ dark ] complexioned harpooneer at any 
## ---------- 187 ---------- 
## it was of a [ dark ] purplish yellow colour here 
## ---------- 188 ---------- 
## all over the same [ dark ] squares he seemed to 
## ---------- 189 ---------- 
## if a parcel of [ dark ] green frogs were running 
## ---------- 190 ---------- 
## about me in the [ dark ] landlord for god s 
## ---------- 191 ---------- 
## the flying scud and [ dark ] rolling clouds there floated 
## ---------- 192 ---------- 
## a little in the [ dark ] and now and then 
## ---------- 193 ---------- 
## that for the present [ dark ] ahab slipped my mind 
## ---------- 194 ---------- 
## in the cold and [ dark ] this made me really 
## ---------- 195 ---------- 
## ascribe high qualities though [ dark ] weave round them tragic 
## ---------- 196 ---------- 
## on thou deep and [ dark ] blue ocean roll ten 
## ---------- 197 ---------- 
## but only to drag [ dark ] ahab after it where 
## ---------- 198 ---------- 
## race is the undeniable [ dark ] side of mankind devilish 
## ---------- 199 ---------- 
## side of mankind devilish [ dark ] at that no offence 
## ---------- 200 ---------- 
## high noon through a [ dark ] blue sea leaving a 
## ---------- 201 ---------- 
## came forth from his [ dark ] den into the blessed 
## ---------- 202 ---------- 
## on account of such [ dark ] symptoms the calculating people 
## ---------- 203 ---------- 
## any hospitable shore the [ dark ] ocean and swelling waters 
## ---------- 204 ---------- 
## start all glared at [ dark ] ahab who was surrounded 
## ---------- 205 ---------- 
## trowsers of the same [ dark ] stuff but strangely crowning 
## ---------- 206 ---------- 
## wild conjectures as to [ dark ] ahab s precise agency 
## ---------- 207 ---------- 
## blast and gored the [ dark ] waves in her madness 
## ---------- 208 ---------- 
## him down into their [ dark ] den growlingly disappearing like 
## ---------- 209 ---------- 
## and smelling in the [ dark ] for the blood he 
## ---------- 210 ---------- 
## as before just after [ dark ] that day when one 
## ---------- 211 ---------- 
## out of the small [ dark ] slabs of the noble 
## ---------- 212 ---------- 
## hemp is a dusky [ dark ] fellow a sort of 
## ---------- 213 ---------- 
## declared these things the [ dark ] daring play of his 
## ---------- 214 ---------- 
## captain mayhew began a [ dark ] story concerning moby dick 
## ---------- 215 ---------- 
## being kept in a [ dark ] locker of the cabin 
## ---------- 216 ---------- 
## he came with a [ dark ] flask in one hand 
## ---------- 217 ---------- 
## a chance of a [ dark ] night and he standing 
## ---------- 218 ---------- 
## was a small and [ dark ] but rather delicate looking 
## ---------- 219 ---------- 
## fishermen it designates the [ dark ] glutinous substance which is 
## ---------- 220 ---------- 
## cylindrically to remove its [ dark ] pelt as an african 
## ---------- 221 ---------- 
## ocean which is the [ dark ] side of this earth 
## ---------- 222 ---------- 
## to dress in the [ dark ] and eat in the 
## ---------- 223 ---------- 
## and eat in the [ dark ] and stumble in darkness 
## ---------- 224 ---------- 
## let me read a [ dark ] valley between three mighty 
## ---------- 225 ---------- 
## down our eyes the [ dark ] vale shows her mouldy 
## ---------- 226 ---------- 
## step over into a [ dark ] corner when you ate 
## ---------- 227 ---------- 
## certain little canoes of [ dark ] wood like the rich 
## ---------- 228 ---------- 
## laid in those same [ dark ] canoes and that the 
## ---------- 229 ---------- 
## islands and from these [ dark ] planks the coffin was 
## ---------- 230 ---------- 
## up a cluster of [ dark ] nods replied yes three 
## ---------- 231 ---------- 
## other way oh thou [ dark ] hindoo half of nature 
## ---------- 232 ---------- 
## see better of a [ dark ] night than anybody else 
## ---------- 233 ---------- 
## but it s too [ dark ] to look hear me 
## ---------- 234 ---------- 
## not unaccompanied with hinted [ dark ] meanings he hollowly laughed 
## ---------- 235 ---------- 
## am i in the [ dark ] side of earth that 
## ---------- 236 ---------- 
## the ship during the [ dark ] vicissitudes of the chase 
## ---------- 237 ---------- 
## and thither at every [ dark ] spot however small on 
## ---------- 238 ---------- 
## soon it was almost [ dark ] but the look out 
## ---------- 239 ---------- 
## spout now sir too [ dark ] cried a voice from 
## ---------- 240 ---------- 
## rising from out the [ dark ] waters beneath the hull

What if you want to identify the first instance of “death”?

first.death <- death.positions.v[1]
my.corpus.l[[1]][first.death]
## [1] "death"
#grab words before and after 1st 'death' instances and use cat to pretty print
cat(my.corpus.l[[1]] [(first.death-2):(first.death+2)])
## life and death the sovereignest
# What about the last 'death'?
last.death <- last(death.positions.v)
my.corpus.l[[1]][last.death]
## [1] "death"
cat(my.corpus.l[[1]] [(last.death-2):(last.death+2)])
## in his death gasp kept

Now for the fun part. Copy and paste this code block into RStudio. You should be able to run your own key-word-search-in-context application.

# This creates a function to take a list containing word vector # from text files and allows for interactive user input to produce KWIC lists 
doitKwic <- function(named.text.word.vector.l){
show.files(names(named.text.word.vector.l)) 
  # ask the user for three bits of information 
  file.id <- as.numeric(readline(
"Which file would you like to examine? Enter a file number: \n")) 
  context <- as.numeric(readline(
  "How much context do you want to see? Enter a number: \n")) 
  keyword <- tolower((readline("Enter a keyword: \n")))
  hits.v <- which(named.text.word.vector.l[[file.id]] == keyword) 
  if(length(hits.v)>0){
    result <- NULL
    for(h in 1:length(hits.v)){
      start <- hits.v[h]-context
      if(start < 1){
        start <- 1 }
      end <- hits.v[h]+context
      cat("\n-----------------------", h, "-------------------------\n") 
      cat(named.text.word.vector.l[[file.id]][start:(hits.v[h]-1)], sep=" ") 
      cat(" [", named.text.word.vector.l[[file.id]][hits.v[h]],"] ", sep="") 
      cat(named.text.word.vector.l[[file.id]][(hits.v[h]+1):end], sep=" ")
      myrow <- cbind(hits.v[h], 
          paste(named.text.word.vector.l[[file.id]][start:(hits.v[h]-1)],
                                      collapse=" "), 
          paste(named.text.word.vector.l[[file.id]][hits.v[h]],
                                      collapse=" "), 
          paste(named.text.word.vector.l[[file.id]][(hits.v[h]+1):end],
                                      collapse=" "))
      result <- rbind(result,myrow)
    }
    colnames(result) <- c("position", "left", "keyword", "right") 
    toprint <- as.numeric((
      readline("Would you like to save this result to a file: enter 1=yes or 0=no \n")))
    if(toprint==1){
      write.csv(result,
                paste("search-results/", keyword,"_In_",
                      context, names(named.text.word.vector.l)[file.id], ".csv", sep="")) 
      }
  } else {
    cat("YOUR KEYWORD WAS NOT FOUND\n")
  }
}

doitKwic(my.corpus.l)

It will first ask for a file number. Since there is only one file number, enter ‘1’.

It will then ask for how much context you would like. I enter ‘4’.

Then, if we enter a term from our sentiment list, ‘grand’, the results are:

———————– 1 ————————- a sea ibid the [mighty] whales which swim in

———————– 2 ————————- whale boat suddenly a [mighty] mass emerged from the

———————– 3 ————————- euroclydon nevertheless is a [mighty] pleasant zephyr to any

———————– 4 ————————- good laugh is a [mighty] good thing and rather

———————– 5 ————————- smallest strands in the [mighty] cable of the scriptures

———————– 6 ————————- far rush of the [mighty] whale which even now

———————– 7 ————————- this struck me as [mighty] singular yet upon second

———————– 8 ————————- nation s census a [mighty] pageant creature formed for

———————– 9 ————————- bildad might have a [mighty] deal to say about

———————– 10 ————————- than the high and [mighty] business of whaling one

———————– 11 ————————- mother of that now [mighty] colony moreover in the

———————– 12 ————————- our leviathan who but [mighty] job and who composed

———————– 13 ————————- in one of the [mighty] triumphs given to a

———————– 14 ————————- of an enraged and [mighty] man but were the

———————– 15 ————————- who in all thy [mighty] earthly marchings ever cullest

———————– 16 ————————- overbearing dignity of some [mighty] woe ere long from

———————– 17 ————————- and there s a [mighty] difference between a living

———————– 18 ————————- a leech till the [mighty] brute is worried to

———————– 19 ————————- of the high and [mighty] cabin in strange contrast

———————– 20 ————————- for a parmacetty and [mighty] quick captain ahab and

———————– 21 ————————- pregnant with many a [mighty] birth no wonder then

———————– 22 ————————- and fearless as this [mighty] steed whether marching amid

———————– 23 ————————- to preserve all his [mighty] swells and undulations and

———————– 24 ————————- the assault yes a [mighty] change had come over

———————– 25 ————————- hoary with mosses speak [mighty] head and tell us

———————– 26 ————————- in thinking how this [mighty] monster is actually a

———————– 27 ————————- leviathan is of so [mighty] a magnitude all his

———————– 28 ————————- whale this high and [mighty] god like dignity inherent

———————– 29 ————————- all things that are [mighty] wears a false brow

———————– 30 ————————- his men in a [mighty] rage that was a

———————– 31 ————————- drawn off from some [mighty] fountain and with half

———————– 32 ————————- our conceit of the [mighty] misty monster to behold

———————– 33 ————————- more i consider this [mighty] tail the more do

———————– 34 ————————- dark valley between three [mighty] heaven abiding peaks that

———————– 35 ————————- hummed around him the [mighty] idler seemed the cunning

———————– 36 ————————- its length was the [mighty] circular basket of ribs

———————– 37 ————————- fossil whale from his [mighty] bulk the whale affords

———————– 38 ————————- bulk to produce a [mighty] book you must choose

———————– 39 ————————- you must choose a [mighty] theme no great and

———————– 40 ————————- i stand among these [mighty] leviathan skeletons skulls tusks

———————– 41 ————————- mark thou high and [mighty] pilot thou tellest me

———————– 42 ————————- listen what s the [mighty] difference between holding a

———————– 43 ————————- long slow billows of [mighty] bulk and striving in

———————– 44 ————————- the bottomless blue rushed [mighty] leviathans sword fish and

———————– 45 ————————- a gentle joyousness a [mighty] mildness of repose in

———————– 46 ————————- land and as the [mighty] iron leviathan of the

———————– 47 ————————- almost simultaneously with a [mighty] volition of ungraduated instantaneous


This is adapted from Matthew Jockers’s Text Analysis in R for Students of Literature (2014).

Return to Week 8