-
Notifications
You must be signed in to change notification settings - Fork 0
/
Workflow.R
294 lines (269 loc) · 13.5 KB
/
Workflow.R
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#Libraries
library(stylo)#for stylometry and for pre-processing of corpora
library(plotly)#for interactive plots
library(tidyverse)#for data manipulation and ggplot2
# To launch the GUI
stylo()
# it is better to save the outcome directly to a variable
# for re-usage and for more info
res = stylo()
sc_HCA_1gram = stylo()
# the following should create the same as above
# Analysis type CA = Hierarchical cluster analysis,
# MDS = multidimensional scaling
# PCV = Principal component analysis (covariance)
saga_corpus_cluster_1gram=stylo(gui=F,
corpus.format = "plain",
corpus.lang = "Other",
corpus.dir="corpus",
analyzed.features = "w",
ngram.size = 1,
preserve.case = FALSE,
mfw.min = 500,
mfw.max = 500,
mfw.incr = 100,
start.at = 1,
culling.min = 0,
culling.max = 0,
culling.incr = 20,
mfw.list.cutoff = 5000,
delete.pronouns = FALSE,
use.existing.freq.tables = FALSE,
use.existing.wordlist = FALSE,
use.custom.list.of.files = FALSE,
analysis.type = "CA",
distance.measure = "delta",
sampling = "no.sampling",
display.on.screen = TRUE,
write.pdf.file = FALSE,
write.jpg.file = FALSE,
write.svg.file = FALSE,
write.png.file = FALSE,
plot.custom.height = 15,
plot.custom.width = 15,
plot.font.size = 2,
plot.line.thickness = 1,
text.id.on.graphs = "both",
colors.on.graphs = "colors",
titles.on.graphs = TRUE,
label.offset = 3,
add.to.margins = 6,
dendrogram.layout.horizontal = TRUE,
pca.visual.flavour = "loadings",
save.distance.tables = FALSE,
save.analyzed.features = FALSE,
save.analyzed.freqs = FALSE,
dump.samples = FALSE
)
# re-use the frequency list, makes the process a lot faster!
# use.existing.freq.tables = TRUE,
saga_corpus_PCV_1gram=stylo(gui=F,
corpus.format = "plain",
corpus.lang = "Other",
corpus.dir="corpus",
analyzed.features = "w",
ngram.size = 1,
preserve.case = FALSE,
mfw.min = 500,
mfw.max = 500,
mfw.incr = 100,
start.at = 1,
culling.min = 0,
culling.max = 0,
culling.incr = 20,
mfw.list.cutoff = 5000,
delete.pronouns = FALSE,
use.existing.freq.tables = TRUE,
use.existing.wordlist = FALSE,
use.custom.list.of.files = FALSE,
analysis.type = "PCV",
distance.measure = "delta",
sampling = "no.sampling",
display.on.screen = TRUE,
write.pdf.file = FALSE,
write.jpg.file = FALSE,
write.svg.file = FALSE,
write.png.file = FALSE,
plot.custom.height = 15,
plot.custom.width = 15,
plot.font.size = 2,
plot.line.thickness = 1,
text.id.on.graphs = "both",
colors.on.graphs = "colors",
titles.on.graphs = TRUE,
label.offset = 3,
add.to.margins = 6,
dendrogram.layout.horizontal = TRUE,
pca.visual.flavour = "loadings",
save.distance.tables = FALSE,
save.analyzed.features = FALSE,
save.analyzed.freqs = FALSE,
dump.samples = FALSE
)
# 2-grams @ 250 MFF
saga_corpus_cluster_2gram=stylo(gui=F,
corpus.format = "plain",
corpus.lang = "Other",
corpus.dir="corpus",
analyzed.features = "w",
ngram.size = 2,
preserve.case = FALSE,
mfw.min = 250,
mfw.max = 250,
mfw.incr = 100,
start.at = 1,
culling.min = 0,
culling.max = 0,
culling.incr = 20,
mfw.list.cutoff = 5000,
delete.pronouns = FALSE,
use.existing.freq.tables = FALSE,
use.existing.wordlist = FALSE,
use.custom.list.of.files = FALSE,
analysis.type = "CA",
distance.measure = "delta",
sampling = "no.sampling",
display.on.screen = TRUE,
write.pdf.file = FALSE,
write.jpg.file = FALSE,
write.svg.file = FALSE,
write.png.file = FALSE,
plot.custom.height = 15,
plot.custom.width = 15,
plot.font.size = 2,
plot.line.thickness = 1,
text.id.on.graphs = "both",
colors.on.graphs = "colors",
titles.on.graphs = TRUE,
label.offset = 3,
add.to.margins = 6,
dendrogram.layout.horizontal = TRUE,
pca.visual.flavour = "classic",
save.distance.tables = FALSE,
save.analyzed.features = FALSE,
save.analyzed.freqs = FALSE,
dump.samples = FALSE
)
saga_corpus_PCV_2gram=stylo(gui=F,
corpus.format = "plain",
corpus.lang = "Other",
corpus.dir="corpus",
analyzed.features = "w",
ngram.size = 2,
preserve.case = FALSE,
mfw.min = 250,
mfw.max = 250,
mfw.incr = 100,
start.at = 1,
culling.min = 0,
culling.max = 0,
culling.incr = 20,
mfw.list.cutoff = 5000,
delete.pronouns = FALSE,
use.existing.freq.tables = TRUE,
use.existing.wordlist = FALSE,
use.custom.list.of.files = FALSE,
analysis.type = "CA",
distance.measure = "delta",
sampling = "no.sampling",
display.on.screen = TRUE,
write.pdf.file = FALSE,
write.jpg.file = FALSE,
write.svg.file = FALSE,
write.png.file = FALSE,
plot.custom.height = 15,
plot.custom.width = 15,
plot.font.size = 2,
plot.line.thickness = 1,
text.id.on.graphs = "both",
colors.on.graphs = "colors",
titles.on.graphs = TRUE,
label.offset = 3,
add.to.margins = 6,
dendrogram.layout.horizontal = TRUE,
pca.visual.flavour = "classic",
save.distance.tables = FALSE,
save.analyzed.features = FALSE,
save.analyzed.freqs = FALSE,
dump.samples = FALSE
)
#Pre-Processing-------
#Loading Corpus and create n-grams
sc_1gram = load.corpus.and.parse(files = "all",
corpus.dir = "corpus",
markup.type= "plain",
corpus.lang = "Other",
splitting.rule = NULL,
sampling = "no.sampling",
sampling.with.replacement = FALSE,
features = "w",
ngram.size = 1,
preserve.case = FALSE,
encoding = "UTF-8")
#create frequency list and table
sc_500MFW_1gram_l = make.frequency.list(sc_1gram, head=500)
sc_500MFW_1gram_t = make.table.of.frequencies(sc_1gram, sc_500MFW_1gram_l, relative=T)
#if you want to view the the z-scores
z_score_500MFW_1gram = scale(sc_500MFW_1gram_t[,1:500])
#calculate distance
#dist.delta = Burrows' delta
#dist.wurzburg = Cosine delta
dist_matrix = as.matrix(dist.delta(sc_500MFW_1gram_t))
md1 = cmdscale(dist_matrix, eig = TRUE)
headers=dimnames(md1$points)[[1]]
df=data.frame(id=seq(48), headers, x=unlist(md1$points[, 1]), y=unlist(md1$points[,2]), stringsAsFactors = F)
df$category = "other"
df$category[headers=="Egla"]="Egla"
df$category[headers=="Heimskringla"]="Hmsk"
fig = plot_ly(type="scatter",
mode="markers",
data=df, x=~x, y=~y,
text=~headers,
color=~category,
colors=c("red", "green", "blue"),
marker=list(size=15)
)
fig = fig %>% layout(title="MDS 1-gram 500MFW")
fig
p=ggplot(df, aes(x,y))+
theme_bw()+
geom_point(data=df, col="black")+
geom_point(data=df[df$headers=="Egla",], col="red")+
geom_point(data=df[df$headers=="Heimskringla",], col="blue")+
geom_text(aes(label = headers), hjust = -0.1)
ggplotly(p)
########DANISH TEXTS ML######
#working with XML!
setwd("./corpus_danish")
danish = stylo()
#Multiclass Classification using Support Vector Machine
setwd("..")
svm = classify() #lunch GUI
primary_c = load.corpus.and.parse(files = "all",
corpus.dir = "primary_set",
markup.type= "xml",
corpus.lang = "Other",
splitting.rule = NULL,
sampling = "no.sampling",
sampling.with.replacement = FALSE,
features = "w",
ngram.size = 1,
preserve.case = FALSE,
encoding = "UTF-8")
secondary_c = load.corpus.and.parse(files = "all",
corpus.dir = "secondary_set",
markup.type= "xml",
corpus.lang = "Other",
splitting.rule = NULL,
sampling = "no.sampling",
sampling.with.replacement = FALSE,
features = "w",
ngram.size = 1,
preserve.case = FALSE,
encoding = "UTF-8")
primary_l = make.frequency.list(primary_c, head=1000)
primary_t = make.table.of.frequencies(primary_c, primary_l, relative=T)
secondary_l = make.frequency.list(secondary_c, head=1000)
secondary_t = make.table.of.frequencies(secondary_c, secondary_l, relative=T)
#svm_1= perform.svm(training.set = primary_t, test.set = secondary_t)
delta = perform.delta(training.set = primary_t, test.set = secondary_t, distance="wurzburg", no.of.candidates = 3)