-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLecture10.R
155 lines (118 loc) · 3.33 KB
/
Lecture10.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
library(dplyr); library(tidyr); library(tibble);
library(tidytext); library(rvest);
library(tictoc)
dir.create('output')
unlink('output/*')
# Pay attention to memory!
get_ngramms <- function(url){
read_html(url) %>%
html_elements("p") %>%
html_text() %>%
enframe() %>%
drop_na() %>%
rename(line = 1, text = 2) %>%
unnest_tokens(bigram, text, token = "ngrams", n = 2) %>%
filter(!is.na(bigram)) %>%
separate(bigram, c("word1", "word2"), sep = " ") %>%
filter(!word1 %in% stop_words$word) %>%
filter(!word2 %in% stop_words$word) %>%
count(word1, word2, sort = TRUE) %>%
unite(bigram, word1, word2, sep = " ") %>%
filter(n >= 5)
}
all_urls <- read.csv('country_links.csv') %>%
pull(1) %>%
head(10)
## Simple FOR Loop ----
res_all <- tibble()
for(i in 1:length(all_urls)){
print('---------')
print(i)
url_i <- all_urls[i]
print(url_i)
res_i <- get_ngramms(url_i)
res_all <- res_all %>%
bind_rows(res_i)
}
## parallel computing code - using optimized function ----
library(doSNOW)
# https://cran.r-project.org/web/packages/doSNOW/index.html
all_urls <- read.csv('country_links.csv') %>%
pull(1)
tic()
get_ngramms(all_urls[1])
toc()
tic()
cl <- makeCluster(6)
registerDoSNOW(cl)
result <- foreach(i = all_urls,
.packages=c('dplyr', 'tidyr', 'tibble', 'rvest', 'tidytext'),
.errorhandling="stop") %dopar%
{
get_ngramms(i)
}
stopCluster(cl)
toc()
# Combine afterwards
result <- result %>%
purrr::reduce(bind_rows)
## parallel computing code - using error handling function ----
all_urls2 <- c(all_urls[1:10], 'abcd')
library(doSNOW)
tic()
cl <- makeCluster(3)
registerDoSNOW(cl)
res_stop <- foreach(i = all_urls2,
.packages=c('dplyr', 'tidyr', 'tibble', 'rvest', 'tidytext'),
.errorhandling="stop") %dopar%
{
get_ngramms(i)
}
res_remove <- foreach(i = all_urls2,
.packages=c('dplyr', 'tidyr', 'tibble', 'rvest', 'tidytext'),
.errorhandling="remove") %dopar%
{
get_ngramms(i)
}
res_pass <- foreach(i = all_urls2,
.packages=c('dplyr', 'tidyr', 'tibble', 'rvest', 'tidytext'),
.errorhandling="pass") %dopar%
{
get_ngramms(i)
}
res_pass %>%
purrr::reduce(bind_rows)
stopCluster(cl)
toc()
## Progress bar -----
get_ngramms_silent <- function(url, save_path = 'output/'){
res <- try(get_ngramms(url), silent = T)
if(length(class(res)) == 1 && class(res) == "try-error"){
warning(paste('Incorrect URL:', url)) # Warning will show in console
return(tibble())
}else{
url2 <- url %>% gsub('[/]', '_', .) %>%
gsub('[:]', '_', .) %>%
gsub('https___en.wikipedia.org_wiki_', '', .)
save(res, file = paste0(save_path, url2, '.Rdata'))
return(res)
}
}
unlink('output/*')
tic()
cl <- makeCluster(10)
registerDoSNOW(cl)
iterations <- length(all_urls)
pb <- txtProgressBar(max = iterations, style = 3)
progress <- function(n) setTxtProgressBar(pb, n)
opts <- list(progress = progress)
result <- foreach(i = all_urls,
.packages=c('dplyr', 'tidyr', 'tibble', 'rvest', 'tidytext'),
.options.snow = opts,
.errorhandling="stop") %dopar%
{
get_ngramms_silent(i)
}
close(pb)
stopCluster(cl)
toc()