-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspell_checker.R
192 lines (141 loc) · 5.16 KB
/
spell_checker.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
# Ryan McMahon, Christopher Boylan, Orly
# Date: 02/20/2016
# Automated spell checking for CAPR
# Credit to Peter Norvig: http://www.sumsar.net/blog/2014/12/peter-norvigs-spell-checker-in-two-lines-of-r/
rm(list=ls())
load("~/Downloads/capr.RData")
setwd("~/Dropbox/capr_poll/")
library(stringr);library(qdap)
# Read in words that are correctly spelled
sorted_words <- names(sort(table(strsplit(tolower(paste(readLines("big.txt"),
collapse = " ")),
"[^a-z]+")), decreasing = TRUE))
# Use Peter Norvig's function to compare distances:
correct <- function(word) {
c(sorted_words[ adist(word, sorted_words) <= min(adist(word, sorted_words), 2)],
word)[1]
}
# Example:
correct(word = "speling")
# spelling
################################################################################
# Start with a column that already has a hand cleaned version so we can
# compare:
# Ballot 2, Angry (Var = II2):
angry_2 <- capr$II2
angry_2[which(angry_2 == "__NA__")] <- NA
# Create empty vector to hold edited strings:
angry_2_auto <- NA
# create list to hold corrections:
corrections <- list()
for(i in 1:length(angry_2)){
if(is.na(angry_2[i])==T){
angry_2_auto[i] <- NA
} else{
temp <- NA
# split strings on spaces, lowercase, and remove non-alphanumeric
a <- unlist(str_split(angry_2[i], pattern = " "))
a <- tolower(a)
a <- gsub(pattern = "[^[:alnum:] ]", x = a, replacement = " ")
if(length(which(a==" "))>0){
a <- a[-which(a==" ")]
}
a <- paste(a, collapse = " ")
temp <- which_misspelled(a, suggest = T)
if(length(temp)>0){
a <- unlist(str_split(a, " "))
temp$word.no <- as.integer(temp$word.no)
for(j in 1:nrow(temp)){
a[temp[j,1]] <- temp[j,3]
}
corrections[[i]] <- temp
} else{
corrections[[i]] <- NULL
}
angry_2_auto[i] <- paste(a, collapse = " ")
}
}
angry_2_comparison <- cbind(angry_2, angry_2_auto)
colnames(angry_2_comparison) <- c("original", "automated")
################################################################################
preprocess_spelling <- function(x){
# Takes a column, x, and generates an auto-corrected vector, processed.
# Stores the changes in a list object, corrections.
errors <- NA
processed <- NA
corrections <- list()
for(i in 1:length(x)){
if(is.na(x[i])==T){
processed[i] <- NA
errors[i] <- NA
} else{
temp <- NA
# split strings on spaces, lowercase, and remove non-alphanumeric
a <- unlist(str_split(x[i], pattern = " "))
a <- tolower(a)
a <- gsub(pattern = "[^[:alnum:] ]", x = a, replacement = " ")
if(length(which(a==" "))>0){
a <- a[-which(a==" ")]
}
a <- paste(a, collapse = " ")
temp <- which_misspelled(a, suggest = T)
if(length(temp)>0){
a <- unlist(str_split(a, " "))
errors[i] <- nrow(temp)/length(a)
temp$word.no <- as.integer(temp$word.no)
for(j in 1:nrow(temp)){
a[temp[j,1]] <- temp[j,3]
}
corrections[[i]] <- temp
} else{
corrections[[i]] <- NULL
errors[i] <- 0
}
processed[i] <- paste(a, collapse = " ")
}
}
object <- list(processed = processed, corrections = corrections, errors = errors)
return(object)
}
test <- preprocess_spelling(x = angry_2)
names(test)
mass_preprocessing <- function(data){
text_responses <- function(data){
temp <- NA
for(i in 1:ncol(data)){
temp[i] <- class(data[,i])
}
return(temp)
}
cat(paste0("finding text response variables...", " \n\n"))
marks <- text_responses(data)
marks <- ifelse(test = marks == "character", yes = T, no = F)
cat(paste0("Found ", length(which(marks==T)), " columns...", "\n\n"))
cat(paste0("Now processing text for ", length(which(marks==T)), " columns...", "\n\n"))
processed_cols <- apply(data[,which(marks==T)], 2, preprocess_spelling)
ret_obj <- list(cleaned = processed_cols, columns_used = marks)
return(ret_obj)
}
test2 <- mass_preprocessing(capr)
cleaned_answers <- test2$cleaned[[1]]$processed
for(i in 2:length(test2$cleaned)){
cleaned_answers <- cbind(cleaned_answers, test2$cleaned[[i]]$processed)
}
cleaned_answers[which(cleaned_answers == " na ")] <- NA
colnames(cleaned_answers) <- names(test2$cleaned)
errors <- test2$cleaned$I1$errors
for(i in 2:length(test2$cleaned)){
errors <- cbind(errors, test2$cleaned[[i]]$errors)
}
colnames(errors) <- names(test2$cleaned)
errors[which(is.na(cleaned_answers)==T)] <- NA
a <- lm(errors[,1] ~ capr$I1A + capr$votechoice + capr$ideo5.int + capr$page_I1_pg_timing, weights = capr$weight)
summary(a)
models <- list()
for(i in 1:14){
models[[i]] <- lm(errors[,i] ~ capr$ideo5.int + capr$educ +
capr$votereg + capr$page_I1_pg_timing, weights = capr$weight)
}
for(i in 1:14){
print(summary(models[[i]]))
}