-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
284 lines (284 loc) · 6.8 KB
/
.Rhistory
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
version
Final <- read.csv("~/R/RCode/TM/HotelReviewSA/Final.csv")
View(Final)
View(Final)
MEUSE <- read.csv("~/R/RCode/TM/HotelReviewSA/MEUSE.csv")
View(MEUSE)
SA <- read.csv("~/R/RCode/TM/HotelReviewSA/SA.csv")
View(SA)
SA <- SA[sample(nrow(SA), 100),]
View(SA)
require(openNLP)
install.packages("openNLP")
install.packages("openNLP")
install.packages("openNLP")
require(openNLP)
require(openNLP)
install.packages('rJava')
install.packages('rJava')
R CMD javareconf
install.packages('rJava')
library(rJava)
install.packages('rJava')
install.packages('rJava')
install.packages('rJava')
require(openNLP) ## Loads the package for use in the task
install.packages('rJava')
install.packages('rJava')
install.packages('rJava')
library(rJava)
remove.packages(rJava)
remove.packages('rJava')
install.packages('rJava')
install.packages('rJava')
version()
version
rm(SA)
install.packages('rJava')
library(rJava)
library(rjava)
library(rJava)
install.packages('rJava')
version
library(rJava)
require(openNLP) ## Loads the package for use in the task
install.packages('rJava')
install.packages('rJava')
library(rJava)
install.packages('rJava')
install.packages('rJava')
data(iris)
View(iris)
str(iris)
set.seed(1234)
# creating training and testing data randomly
ind <- sample(2, nrow(iris), replace=TRUE, prob=c(0.7, 0.3))
trainData <- iris[ind==1,]
testData <- iris[ind==2,]
library(party)
myFormula <- Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width
iris_ctree <- ctree(myFormula, data=trainData)
# check the prediction
install.packages('party')
library(party)
myFormula <- Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width
iris_ctree <- ctree(myFormula, data=trainData)
# check the prediction
table(predict(iris_ctree), trainData$Species)
print(iris_ctree)
plot(iris_ctree)
plot(iris_ctree, type="simple")
# predict on test data
testPred <- predict(iris_ctree, newdata = testData)
table(testPred, testData$Species)
??ctree
?ctree
install.packages(){}
install.packages('mboost')
words <- scan("http://norvig.com/big.txt", what = character())
a <- strip.text(words)
strip.text <- function(txt) {
# remove apostrophes (so "don't" -> "dont", "Jane's" -> "Janes", etc.)
txt <- gsub("'","",txt)
# convert to lowercase
txt <- tolower(txt)
# change other non-alphanumeric characters to spaces
txt <- gsub("[^a-z0-9]"," ",txt)
# change digits to #
txt <- gsub("[0-9]+"," ",txt)
# split and make one vector
txt <- unlist(strsplit(txt," "))
# remove empty words
txt <- txt[txt != ""]
return(txt)
}
a <- strip.text(words)
a[[1]]
a[[2]]
words[[2]]
words[2]
sumary(words)
summary(words)
Transpositions <- function(word = FALSE) {
N <- nchar(word)
if (N > 2) {
out <- rep(word, N - 1)
word <- unlist(strsplit(word, NULL))
# Permutations of the letters
perms <- matrix(c(1:(N - 1), 2:N), ncol = 2)
reversed <- perms[, 2:1]
trans.words <- matrix(rep(word, N - 1), byrow = TRUE, nrow = N - 1)
for(i in 1:(N - 1)) {
trans.words[i, perms[i, ]] <- trans.words[i, reversed[i, ]]
out[i] <- paste(trans.words[i, ], collapse = "")
}
}
else if (N == 2) {
out <- paste(word[2:1], collapse = "")
}
else {
out <- paste(word, collapse = "")
}
return(out)
}
Deletes <- function(word = FALSE) {
N <- nchar(word)
word <- unlist(strsplit(word, NULL))
for(i in 1:N) {
out[i] <- paste(word[-i], collapse = "")
}
return(out)
}
# Single-letter insertions.
Insertions <- function(word = FALSE) {
N <- nchar(word)
out <- list()
for (letter in letters) {
out[[letter]] <- rep(word, N + 1)
for (i in 1:(N + 1)) {
out[[letter]][i] <- paste(substr(word, i - N, i - 1), letter,
substr(word, i, N), sep = "")
}
}
out <- unlist(out)
return(out)
}
# Single-letter replacements.
Replaces <- function(word = FALSE) {
N <- nchar(word)
out <- list()
for (letter in letters) {
out[[letter]] <- rep(word, N)
for (i in 1:N) {
out[[letter]][i] <- paste(substr(word, i - N, i - 1), letter,
substr(word, i + 1, N + 1), sep = "")
}
}
out <- unlist(out)
return(out)
}
# All Neighbors with distance "1"
Neighbors <- function(word) {
neighbors <- c(word, Replaces(word), Deletes(word),
Insertions(word), Transpositions(word))
return(neighbors)
}
# Probability as determined by our corpus.
Probability <- function(word, dtm) {
# Number of words, total
N <- length(dtm)
word.number <- which(names(dtm) == word)
count <- dtm[word.number]
pval <- count/N
return(pval)
}
# Correct a single word.
Correct <- function(word, dtm) {
neighbors <- Neighbors(word)
# If it is a word, just return it.
if (word %in% names(dtm)) {
out <- word
}
# Otherwise, check for neighbors.
else {
# Which of the neighbors are known words?
known <- which(neighbors %in% names(dtm))
N.known <- length(known)
# If there are no known neighbors, including the word,
# look farther away.
if (N.known == 0) {
print(paste("Having a hard time matching '", word, "'...", sep = ""))
neighbors <- unlist(lapply(neighbors, Neighbors))
}
# Then out non-words.
neighbors <- neighbors[which(neighbors %in% names(dtm))]
N <- length(neighbors)
# If we found some neighbors, find the one with the highest
# p-value.
if (N > 1) {
P <- 0*(1:N)
for (i in 1:N) {
P[i] <- Probability(neighbors[i], dtm)
}
out <- neighbors[which.max(P)]
}
# If no neighbors still, return the word.
else {
out <- word
}
}
return(out)
}
words <- strip.text(words)
counts <- table(words)
Correct("speling", dtm = counts)
CorrectDocument <- function(document, dtm) {
by.word <- unlist(strsplit(document, " "))
N <- length(by.word)
for (i in 1:N) {
by.word[i] <- Correct(by.word[i], dtm = dtm)
}
corrected <- paste(by.word, collapse = " ")
return(corrected)
}
Correct("speling", dtm = counts)
rm(a)
library(slidify)
slidify()
slidify("index .Rmd")
slidify("index.Rmd")
slidify("index.Rmd")
setwd("/media/sda2/Data/RCode/DSTraining/Intro-to-R")
slidify("index.Rmd")
slidify("index.Rmd")
slidify("index.Rmd")
slidify("index.Rmd")
install.packages('sqldf')
install.packages('sqldf')
slidify("index.Rmd")
slidify("index.Rmd")
slidify("index.Rmd")
slidify("index.Rmd")
slidify("index.Rmd")
slidify("index.Rmd")
slidify("index.Rmd")
install.packages("plyr")
library(plyr)
slidify("index.Rmd")
?knitr
??knitr
slidify("index.Rmd")
install.packages("sqldf")
slidify("index.Rmd")
slidify("index.Rmd")
slidify("index.Rmd")
slidify("index.Rmd")
slidify("index.Rmd")
slidify("index.Rmd")
slidify("index.Rmd")
library(plyr)
slidify("index.Rmd")
slidify("index.Rmd")
summary(iris)
stry(iris)
str(iris)
str(iris)
summary(iris)
slidify("index.Rmd")
slidify("index.Rmd")
slidify("index.Rmd")
s <- c("Go to Heaven for the climate, Hell for the company.")
library(stringr)
?str_split
str_split(s, ",")
s <- c("I am in India, I love Cricket, and I like Sachin.")
str_split(s, ",")
str_extract_all(s, "[H][a-z]+ ")
str_extract_all(s, "[I][a-z]+ ")
s <- c("I am in India, I love Cricket, and I like Sachin.")
str_extract_all(s, "[I][a-z]+ ")
s <- c("I am in India, I love Cricket, and I like Sachin.")
str_extract_all(s, "[l][a-z]+")
slidify("index.Rmd")
slidify("index.Rmd")
slidify("index.Rmd")