-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeoDCPresentation.Rpres
368 lines (244 loc) · 9.79 KB
/
GeoDCPresentation.Rpres
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
Parsing Public Health PDF's
========================================================
author: Collin Schwantes
date: 04 February 2019
css: custom.css
https://github.com/collinschwantes/GeoDCPublicHealthPDFs
Why Should I care?
========================================================
- Ebola
- Zika
- Vaccine preventable diseases
- Lead levels
Why PDFs?
========================================================
### PDF's are EVERYWHERE in Govt. and especially in Public Health
- World Health Organization
- Minstry of Health Madagascar
- Your local public health department*
- * willing to bet you a beer
Why Are PDF's hard to work with?
========================================================
- Designed to be human readable
- Designed to be printed
- Generally not designed as a machine-readable data store
But isn't the XML uniform and manipulatable?
========================================================
# Not really

So you're just manipulating strings?
========================================================
Hopefully!

What are my options?
========================================================
- PDFTools - text based pdfs
- Tabulizer - Java-based tool with R wrapper
- Tesseract - OCR without leaving R
Why use OCR?
========================================================
- PDFs and websites often contain content in images
- Adobe OCR works pretty well for tables sometimes
- Tesseract can provide a programtic way of extracting text
An Example: OCR
================

An Example: OCR
==================

***
- Health Zones nested in provinces
- Case status nested in category "cumulative"
- Different colors
- French (bias is in training data is real)
- Sparse text
What can go wrong: OCR Tesseract
========================================================
## I TRIED
it is finnicky, the params list is a mile long
```{r, echo=FALSE}
library(tesseract)
library(dplyr)
EbolaText <- ocr(engine = tesseract("fra"),image = "https://gallery.mailchimp.com/89e5755d2cca4840b1af93176/images/62e6af37-9999-49f3-b6ef-30b56ede0459.png")
cat(substr(EbolaText,start = 1,stop = 200))
```
How to fix: OCR issues
========================================================
Read the documentation
- [docs](https://github.com/tesseract-ocr/tesseract/wiki/ControlParams)
- [tutorial](http://www.joyofdata.de/blog/a-guide-on-ocr-with-tesseract-3-03/)
- Preprocess your image
- train a new engine with data from your region of interest
How to fix: OCR result
=========================================================
Before
```{r, echo=FALSE}
library(tesseract)
library(dplyr)
EbolaText <- ocr(engine = tesseract("fra"),image = "https://gallery.mailchimp.com/89e5755d2cca4840b1af93176/images/62e6af37-9999-49f3-b6ef-30b56ede0459.png")
cat(substr(EbolaText,start = 1,stop = 200))
```
***
After - needs improvement
```{r,echo=FALSE}
library(magick)
library(tesseract)
img <- "https://gallery.mailchimp.com/89e5755d2cca4840b1af93176/images/62e6af37-9999-49f3-b6ef-30b56ede0459.png"
imgp <- img %>% image_read() %>%
image_resize("2000x") %>%
image_convert(type = 'Grayscale') %>%
image_trim(fuzz = 40)
#set engine parameters to look for tables
EbolaText <- ocr(engine = tesseract("fra",
options = list(tessedit_pageseg_mode = 'auto',
textord_tabfind_find_tables = '1',
textord_tablefind_recognize_tables = '1')),
image = imgp)
cat(substr(EbolaText,start = 1,stop = 200))
```
Other OCR Tricks: Cropping
=======================================================

***
```{r,echo=FALSE}
library(magick)
library(tesseract)
img <- "./examplePDFs/EbolaCropHZ.png"
imgp <- img %>% image_read() %>%
image_resize("2000x") %>%
image_convert(type = 'Grayscale') %>%
image_trim(fuzz = 40)
#set engine parameters to look for tables
EbolaText <- ocr(engine = tesseract("fra",
options = list(tessedit_pageseg_mode = 'auto',
textord_tabfind_find_tables = '1',
textord_tablefind_recognize_tables = '1')),
image = imgp)
cat(EbolaText)
```
Tabulizer
========================================================
If you can get java JDK and R to connect properly it probably works great. [Troubleshooting docs](https://github.com/ropensci/tabulizer#installing-java-on-windows-with-chocolatey) from the great folks at ROpenSci

PDFTools
==========================================
"Utilities based on 'libpoppler' for extracting **text**, fonts, attachments and metadata from a PDF file. Also supports high quality rendering of PDF documents into PNG, JPEG, TIFF format, or into raw bitmap vectors for further processing in R"
- great for getting text from pdf's
- can also be used for getting images embedded in PDF
PDFTools: PAHO Example
=========================================

PDFTools: PAHO Example
======================================
*Before you start, review [regex](https://github.com/rstudio/cheatsheets/raw/master/regex.pdf)
```{r,eval=FALSE}
library(pdftools)
library(stringr)
library(dplyr)
library(purrr)
library(httr)
PahoDF <- GET(url = "https://www.paho.org/hq/index.php?option=com_docman&view=download&category_slu=cumulative-cases-pdf-8865&alias=43296-zika-cumulative-cases-4-january-2018-296&Itemid=270&lang=en")
PDFraw <- content(x = PahoPDF,as = "raw")
writeBin(object = PDFraw, con = "./ExamplePDFs/Paho.pdf")
pathPAHO <- list.files(path = "./ExamplePDFs",pattern = "Paho.pdf",full.names = T)
pdf_text(pdf = "./examplePDFs/Paho.PDF")
```
PDFTools: PAHO Example
======================================
```{r,echo=FALSE}
library(pdftools)
library(stringr)
library(dplyr)
library(purrr)
library(httr)
pathPAHO <- list.files(path = "./ExamplePDFs",pattern = "Paho.pdf",full.names = T)
pahotext <- pdf_text(pdf = pathPAHO)[[1]]
matches <- gregexpr(pattern = "North America|Subtotal",text = pahotext)
str_sub(string = pahotext,start = (matches[[1]][1]+14),end = matches[[1]][length(matches[[1]])]
)
```
PDFTools: PAHO Example
======================================
```{r,echo=FALSE}
library(pdftools)
library(stringr)
library(dplyr)
library(purrr)
library(httr)
pathPAHO <- list.files(path = "./ExamplePDFs",pattern = "Paho.pdf",full.names = T)
pahotext <- pdf_text(pdf = pathPAHO)[[1]]
matches <- gregexpr(pattern = "North America|Subtotal",text = pahotext)
tableText <- str_sub(string = pahotext,start = (matches[[1]][1]+14),end = matches[[1]][length(matches[[1]])]
)
str_split(string = tableText,pattern = "\n")[[1]][c(10,21,22,38)]
```
PDFTools: PAHO Example
======================================
```{r,echo=FALSE}
library(httr)
library(stringr)
library(dplyr)
library(pdftools)
library(geonames)
#set geonames username and host using options
# options(geonamesUsername = "my.geoname",geonamesHost = "api.geonames.org")
PahoPDF <- GET(url = "https://www.paho.org/hq/index.php?option=com_docman&view=download&category_slug=cumulative-cases-pdf-8865&alias=43296-zika-cumulative-cases-4-january-2018-296&Itemid=270&lang=en")
PDFraw <- content(x = PahoPDF,as = "raw")
dir.create("./examplePDFs")
writeBin(object = PDFraw, con = "./ExamplePDFs/Paho.pdf")
textPAHO <- pdf_text(pdf = "./examplePDFs/Paho.PDF")
pahotext <- pdf_text(pdf = pathPAHO)[[1]]
#see note below
pahotext <- gsub(pattern = "Guadeloupe\n ⁷,⁹",replacement = "Guadeloupe",x = pahotext)
#see note below
matches <- gregexpr(pattern = "North America|Subtotal",text = pahotext)
tableText <- str_sub(string = pahotext,
start = (matches[[1]][1] + 14),
end = c(matches[[1]][length(matches[[1]])]-1))
tableText <- gsub(pattern = "Guadeloupe\n ⁷,⁹",replacement = "Guadeloupe",x = tableText)
tableText <- gsub(pattern = "Saint Barthelemy\n ⁷",replacement = "Saint Barthelemy",x = tableText)
#split on line breaks
PAHOstr <- str_split(string = tableText,pattern = "\n")
# str(PAHOstr)
a <- PAHOstr %>%
map(str_split,pattern = " ") %>%
flatten()
## look for patterns
#a %>% map(length) %>% unlist %>% mean()
#a %>% keep(~length(.x) < 37)
b <- a %>% discard(~length(.x) < 37) %>% map(trimws) %>%
map(gsub,pattern = ",", replacement = "")
# you try to do things the right way,
#until its too easy to do it the wrong way
ValuePuller <- function(x) {
ab <- list()
for(i in 1:length(x)) {
li <- x[i]
if(nchar(li) > 0) {
ab[i] <- li
}
}
return(unlist(ab))
}
dataPaho <- b %>% map(ValuePuller)
PAHOtb <- do.call(rbind,dataPaho)
PAHOtb <- as_tibble(PAHOtb)
names(PAHOtb) <- c("Country", "Suspected","Confirmed", "Imported","Incidence","Deaths","ZikaCS","Pop")
PAHOtb$Country <- gsub(pattern = "[^[:alpha:]| ]",replacement = "",x = PAHOtb$Country)
PAHOtb <- PAHOtb %>% filter(Country != "Subtotal")
PAHOtb$Country <- gsub(pattern = "Virgin Islands UK",replacement = "British Virgin Islands",x = PAHOtb$Country)
PAHOtb$Country <- gsub(pattern = "Saint Martin",replacement = "MF",x = PAHOtb$Country)
PAHOtb$Country <- gsub(pattern = "Venezuela Bolivarian Republic of",replacement = "Venezuela",x = PAHOtb$Country)
PAHOtb
```
PDFTools: PAHO Example
=============
- Clean up data types
- Connect Admin areas to standard names
- Join to geopatial data (polygons)
- Display the data!
PDFTools: PAHO Example
=============
```{r, echo= FALSE}
shiny::includeHTML("./leafletMap.html")
```