-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseCode
More file actions
277 lines (233 loc) · 8.42 KB
/
Copy pathBaseCode
File metadata and controls
277 lines (233 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Catullus.R
# Contains the majority of the code used in this project
setwd("~/OneDrive/OD_Documents/Hacking the Humanist/Final Paper")
# Import the libraries that I will need
library(rvest)
library(magrittr)
# Get full english version of the poems:
cateng_xml<-read_html("CatullusEnglish.xml")
eng.v<-html_nodes(cateng_xml,"div1") %>%
html_text(trim = TRUE)
for (i in 1:3){
eng.v<-eng.v[-19]
}
View(eng.v)
# Have all as one file for searching purposes:
catall_eng<-paste(1:117, eng.v, sep=" ", collapse = "\n") %>%
write(.,"catall_eng.txt")
# Write them to files:
for (i in 1:length(eng.v)) {
num<-i
num<-i
if (i==15){
num<-"14b"
}
if (i>15){
num<-i-1
}
if (num== 2||num==3||num==5||num==6||num==8||num==11||num==36
||num==37||num==51||num==58||num==68||num==70||num==72||num==75
||num==76||num==79||num==83||num==85||num==86||num==87||num==91
||num==92||num==104||num==107||num==109) {
setwd("~/OneDrive/OD_Documents/Hacking the Humanist/Final Paper/Lesbia")
lesbia<-paste(num,".txt", sep="")
write(eng.v[i],lesbia)
}
setwd("~/OneDrive/OD_Documents/Hacking the Humanist/Final Paper/corpus")
curr<-paste(num, ".txt", sep="")
write(eng.v[i],curr)
}
setwd("~/OneDrive/OD_Documents/Hacking the Humanist/Final Paper")
# Get latin version of the poems
catlat_xml<-read_html("CatullusLatin.xml")
latin.v<-html_nodes(catlat_xml,"div2") %>%
html_text(trim = TRUE)
View(latin.v)
# Have all as one file for searching purposes:
catall_lat<-paste(1:117, eng.v, sep=" ", collapse = "\n") %>%
write(.,"catall_lat.txt")
# Write them to files:
setwd("~/OneDrive/OD_Documents/Hacking the Humanist/Final Paper/Latin Poems")
for (i in 1:length(latin.v)) {
num<-i
if (i==15){
num<-"14b"
}
if (i>15){
num<-i-1
}
curr<-paste(num, ".txt", sep="")
write(latin.v[i],curr)
}
setwd("~/OneDrive/OD_Documents/Hacking the Humanist/Final Paper")
#-----------------------------------------------------
##### MANIPULATION:
# word frequencies
library("tm")
library(magrittr)
catcorpus<-Corpus(DirSource("corpus"))
# Remove "filler" works
catremov<-tm_map(catcorpus, removeWords, stopwords("SMART"))
catcorpus<-tm_map(catcorpus, removePunctuation)
catcorpus<-tm_map(catcorpus, content_transformer(tolower))
catullus.dtm <- DocumentTermMatrix(catremov)
rownames(catullus.dtm)<-c(1,10,100:109,11,110:119,12,120,13:19,2,20:29,
3,30:39,4,40:49,5,50:59,6,60:69,7,70:79,8,
80:89,9,90:99)
# Frequent terms:
catfreqterms.eng<-findFreqTerms(catullus.dtm, lowfreq=15, highfreq= Inf)
freqs<-c()
for (i in 1:length(catfreqterms.eng)){
currword<-catfreqterms.eng[i]
col<-which(colnames(catullus.dtm)==currword)
currval<-sum(catullus.dtm[,col])
freqs<-c(freqs,currval)
}
catfreqterms.eng<-cbind(catfreqterms.eng,freqs)
#-----------------------------------------------------
##### CLUSTERING:
# Works Cited:
# http://www.statmethods.net/advstats/cluster.html
# Colors from: http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf
source('Rtools.R') # our tools: using getcorpus() below
library(cluster) #for clusplot()
library(fpc) #for entroid plot [plotcluster()] & comparing
colors<-c("darkolivegreen1","darkorchid","aquamarine4","deeppink","mediumorchid4")
#-----------
## ENGLISH:
eng<-getcorpus("corpus")
# counts and freqs
eng.counts.l<-lapply(eng,table) # raw counts
eng.relfreq.l<-lapply(eng.counts.l,prop.table) # relative freqs
# convert list of frequency tables to a list of dataframes
eng.df.l<-mapply(data.frame,
ID=names(eng),
eng.relfreq.l, SIMPLIFY=FALSE,
MoreArgs=list(stringsAsFactors=FALSE)) # I still get factors when I run this mapply
# convert to matrix
# first bind all dataframes in the lists into a single dataframe
songfreqs.df<-do.call(rbind,eng.df.l)
rownames(songfreqs.df)<-NULL
# convert to a wide-form table
result<-xtabs(Freq ~ ID+Var1,data=songfreqs.df)
# convert xtabs table to a matrix
eng.m<-apply(result,2,as.numeric)
engreduced.m<-eng.m[,apply(eng.m,2,mean)>=.0025]
### Clustering!
dm<-dist(engreduced.m)
cluster<-hclust(dm)
cluster$labels<-names(eng.relfreq.l)
plot(cluster,cex=.8)
groups<-cutree(cluster,k=11) #cut tree into clusters
rect.hclust(cluster,k=11, border=colors[2])
# K-Means Clustering with 5 clusters
fit <- kmeans(engreduced.m, 10)
# Cluster Plot against 1st 2 principal components
# vary parameters for most readable graph
clusplot(engreduced.m, fit$cluster,
col.p=colors[4],
col.clus=colors[2:5],
color=T, shade=T,
labels=2, lines=0)
## zoom in :
rect(0,-1, 2,1, border = "orange", lwd=2)
clusplot(engreduced.m, fit$cluster, color = T,
xlim = c(-1,1), ylim = c(-1,1), labels=2)
box(col="orange",lwd=2); mtext("Eng sub region", font = 3, cex = 2)
# ## or zoom out :
# clusplot(iris.x, cl3, color = TRUE, xlim = c(-4,4), ylim = c(-4,4))
# mtext("`super' region", font = 4, cex = 2)
# rect(U[1],U[3], U[2],U[4], lwd=2, lty = 3)
# Centroid Plot against 1st 2 discriminant functions
plotcluster(engreduced.m, fit$cluster)
title("English Centroid Plot")
## A different type of clustering using stylo:
# stylo package
library("stylo")
engstylo<-stylo(gui=FALSE)
#----------------
## JUST LESBIA:
les<-getcorpus("Lesbia")
# counts and freqs
les.counts.l<-lapply(les,table) # raw counts
les.relfreq.l<-lapply(les.counts.l,prop.table) # relative freqs
# convert list of frequency tables to a list of dataframes
les.df.l<-mapply(data.frame,
ID=names(les),
les.relfreq.l, SIMPLIFY=FALSE,
MoreArgs=list(stringsAsFactors=FALSE)) # I still get factors when I run this mapply
# convert to matrix
# first bind all dataframes in the lists into a single dataframe
songfreqs.df<-do.call(rbind,les.df.l)
rownames(songfreqs.df)<-NULL
# convert to a wide-form table
result<-xtabs(Freq ~ ID+Var1,data=songfreqs.df)
# convert xtabs table to a matrix
les.m<-apply(result,2,as.numeric)
lesreduced.m<-les.m[,apply(les.m,2,mean)>=.0025]
### Clustering!
dm<-dist(lesreduced.m)
cluster2<-hclust(dm)
cluster2$labels<-names(les.relfreq.l)
plot(cluster2,cex=.8)
groups<-cutree(cluster2,k=5) #cut tree into clusters
rect.hclust(cluster2,k=5, border="purple")
# # K-Means Clustering with clusters
# fit2 <- kmeans(lesreduced.m, 2)
# # Cluster Plot against 1st 2 principal components
# # vary parameters for most readable graph
# clusplot(lesreduced.m, fit2$cluster,
# col.p=colors[4],
# color=T, shade=T,
# labels=2, lines=0)
# # Centroid Plot against 1st 2 discriminant functions
# plotcluster(lesreduced.m, fit2$cluster)
# title("Lesbia Poems Centroid Plot")
#----------
## LATIN:
latin<-getcorpus("Latin Poems")
# counts and freqs
latin.counts.l<-lapply(latin,table) # raw counts
latin.relfreq.l<-lapply(latin.counts.l,prop.table) # relative freqs
# convert list of frequency tables to a list of dataframes
latin.df.l<-mapply(data.frame,
ID=names(latin),
latin.relfreq.l, SIMPLIFY=FALSE,
MoreArgs=list(stringsAsFactors=FALSE)) # I still get factors when I run this mapply
# convert to matrix
# first bind all dataframes in the lists into a single dataframe
songfreqs.df<-do.call(rbind,latin.df.l)
rownames(songfreqs.df)<-NULL
# convert to a wide-form table
result<-xtabs(Freq ~ ID+Var1,data=songfreqs.df)
# convert xtabs table to a matrix
latin.m<-apply(result,2,as.numeric)
latinreduced.m<-latin.m[,apply(latin.m,2,mean)>=.0025]
### Clustering!
dm<-dist(latinreduced.m)
cluster3<-hclust(dm)
cluster3$labels<-names(latin.relfreq.l)
plot(cluster3,cex=.8)
groups<-cutree(cluster3,k=11) #cut tree into clusters
rect.hclust(cluster3,k=11, border=colors[3])
# K-Means Clustering with 5 clusters
fit3 <- kmeans(latinreduced.m, 10)
# Cluster Plot against 1st 2 principal components
# vary parameters for most readable graph
clusplot(latinreduced.m, fit3$cluster,
col.p=colors[4],
col.clus=colors[2:5],
color=T, shade=T,
labels=2, lines=0)
## zoom in :
rect(0,-1, 2,1, border = "orange", lwd=2)
clusplot(latinreduced.m, fit3$cluster, color = T,
xlim = c(-1,1), ylim = c(-1,1), labels=2)
box(col="orange",lwd=2); mtext("Latin sub region", font = 3, cex = 2)
# ## or zoom out :
# clusplot(iris.x, cl3, color = TRUE, xlim = c(-4,4), ylim = c(-4,4))
# mtext("`super' region", font = 4, cex = 2)
# rect(U[1],U[3], U[2],U[4], lwd=2, lty = 3)
# Centroid Plot against 1st 2 discriminant functions
plotcluster(latinreduced.m, fit3$cluster)
title("Latin Centroid Plot")