-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleitor_noticias.R
133 lines (76 loc) · 2.77 KB
/
leitor_noticias.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
##########################################
#Leitor de Noticias by Bruno Rafaeli#
##########################################
library(stringr)
library(parallel)
#Lista com os jornais
jornais = c('@JornalOGlobo','@jc_pe','@cbonlinedf','@em_com','@folha','@Estadao','@zerohora','@atarde')
#Define quais arquivos quer ler
files_0 <- (Sys.glob("../new_noticias_capturadas/new_noticias_capturada/*"))
# Calculate the number of cores
no_cores <- 7
# Initiate cluster
cl <- makeCluster(no_cores)
leitor <- function(j){
jornal = j
final_data <- data.frame(
date = character(),
n_encontrado = integer(),
n_total = integer()
)
caminho_0 = paste0("/*",jornal,"*")
for(i in 1:length(files_0)){
caminho = paste0(files_0[i],caminho_0)
files <- (Sys.glob(caminho))
total = 0
total_incerteza = 0
data = unlist(strsplit(files_0[i],"/"))
data = data[[3]]
if(length(files) > 0){
tempo_inicial = Sys.time()
contador = 0
for(j in 1:length(files)){
contador = contador + 1
total = total + 1
fileName <- files[j]
artigo <- readChar(fileName, file.info(fileName)$size)
artigo <- toupper(artigo)
economic_words <- c("ECON")
uncertainty_words <- c("INCERT","INSTAB")#,"CRISE")
politic_words <- c("GOVERNO","CONGRESSO","DILMA","PRESIDENT")
combina_termos = do.call(paste, expand.grid(economic_words,uncertainty_words, politic_words))
#combina_termos = do.call(paste, expand.grid(economic_words,uncertainty_words))
for(k in 1:length(combina_termos)){
termo = unlist(strsplit(combina_termos[k]," "))
resultado = all(str_detect(artigo,termo))
if(resultado){
total_incerteza = total_incerteza + 1
break
}
}
if(contador == 1000){
tempo_final = Sys.time() - tempo_inicial
message(i, " | ", tempo_final)
contador = 0
}
}
new_row <- data.frame(
date = data,
n_encontrado = total_incerteza,
n_total = total
)
final_data <- rbind(final_data,new_row)
message(paste0(files_0[i]," | ",i,"/", length(files_0)))
}
}
nome = paste0("3_statistic_",jornal,".Rda")
saveRDS(object = final_data,file = nome)
}
executar_leitor <- function(i){
leitor(jornais[i])
}
clusterExport(cl=cl, varlist=c("jornais","files_0","leitor"))
clusterEvalQ(cl,library(stringr))
numero_rows = length(jornais)-1
parLapply(cl,1:numero_rows,executar_leitor)
stopCluster(cl)