-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathREADME.Rmd
500 lines (349 loc) · 16.2 KB
/
README.Rmd
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
---
title: "textreadr"
output:
md_document:
toc: true
---

[](https://www.repostatus.org/#active)
[](https://travis-ci.org/trinker/textreadr)
[](https://coveralls.io/github/trinker/textreadr)
[](https://cran.r-project.org/package=textreadr)
**textreadr** is a small collection of convenience tools for reading text documents into R. This is not meant to be an exhaustive collection; for more see the [**tm**](https://CRAN.R-project.org/package=tm) package.
```{r, eval=FALSE, echo=FALSE}
#render("README.Rmd", md_document(variant="markdown", toc=TRUE, preserve_yaml = TRUE))
```
# Functions
Most jobs in my workflow can be completed with `read_document` and `read_dir`. The former generically reads in a .docx, .doc, .pdf, .html, .pptx, or .txt file without specifying the extension. The latter reads in multiple .docx, .doc, .html, .odt .pdf, .pptx, .rtf, or .txt files from a directory as a `data.frame` with a file and text column. This workflow is effective because most text documents I encounter are stored as a .docx, .doc, .html, .odt .pdf, .pptx, .rtf, or .txt file. The remaining common storage formats I encounter include .csv, .xlsx, XML, structured .html, and SQL. For these first 4 forms the [**readr**](https://CRAN.R-project.org/package=readr), [**readx**l](https://CRAN.R-project.org/package=readxl), [**xml2**](https://CRAN.R-project.org/package=xml2), and [**rvest**](https://CRAN.R-project.org/package=rvest). For SQL:
| R Package | SQL |
|-------------|------------------------|
| ROBDC | Microsoft SQL Server |
| RMySQL | MySQL |
| ROracle | Oracle |
| RJDBC | JDBC |
These packages are already specialized to handle these very specific data formats. **textreadr** provides the basic reading tools that work with the five basic file formats in which text data is stored.
The main functions, task category, & descriptions are summarized in the table below:
| Function | Task | Description |
|---------------------------|-------------|---------------------------------------|
| `read_transcript` | reading | Read 2 column transcripts |
| `read_docx` | reading | Read .docx |
| `read_doc` | reading | Read .doc |
| `read_rtf` | reading | Read .rtf |
| `read_document` | reading | Generic text reader for .doc, .docx, .rtf, .txt, .pdf |
| `read_html` | reading | Read .html |
| `read_pdf` | reading | Read .pdf |
| `read_odt` | reading | Read .odt |
| `read_dir` | reading | Read and format multiple .doc, .docx, .rtf, .txt, .pdf, .pptx, .odt files |
| `read_dir_transcript` | reading | Read and format multiple transcript files |
| `download` | downloading | Download documents |
| `peek` | viewing | Truncated viewing of `data.frame`s |
# Installation
To download the development version of **textreadr**:
Download the [zip ball](https://github.com/trinker/textreadr/zipball/master) or [tar ball](https://github.com/trinker/textreadr/tarball/master), decompress and run `R CMD INSTALL` on it, or use the **pacman** package to install the development version:
```r
if (!require("pacman")) install.packages("pacman")
pacman::p_load_gh("trinker/textreadr")
```
# Contact
You are welcome to:
* submit suggestions and bug-reports at: <https://github.com/trinker/textreadr/issues>
* send a pull request on: <https://github.com/trinker/textreadr/>
* compose a friendly e-mail to: <[email protected]>
# Demonstration
## Load the Packages/Data
```{r, message=FALSE, warning=FALSE}
if (!require("pacman")) install.packages("pacman")
pacman::p_load(textreadr, magrittr)
pacman::p_load_gh("trinker/pathr")
trans_docs <- dir(
system.file("docs", package = "textreadr"),
pattern = "^trans",
full.names = TRUE
)
docx_doc <- system.file("docs/Yasmine_Interview_Transcript.docx", package = "textreadr")
doc_doc <- system.file("docs/Yasmine_Interview_Transcript.doc", package = "textreadr")
pdf_doc <- system.file("docs/rl10075oralhistoryst002.pdf", package = "textreadr")
html_doc <- system.file('docs/textreadr_creed.html', package = "textreadr")
txt_doc <- system.file('docs/textreadr_creed.txt', package = "textreadr")
pptx_doc <- system.file('docs/Hello_World.pptx', package = "textreadr")
odt_doc <- system.file('docs/Hello_World.odt', package = "textreadr")
rtf_doc <- download(
'https://raw.githubusercontent.com/trinker/textreadr/master/inst/docs/trans7.rtf'
)
pdf_doc_img <- system.file("docs/McCune2002Choi2010.pdf", package = "textreadr")
```
## Download & Browse
The `download` and `browse` functions are utilities for downloading and opening files and directories.
### Download
`download` is simply a wrapper for `curl::curl_download` that allows multiple documents to be download, has the `tempdir` pre-set as the `destfile` (named `loc` in **textreadr**), and also returns the path to the file download for easy use in a **magrittr** chain.
Here I download a .docx file of presidential debated from 2012.
```{r}
'https://github.com/trinker/textreadr/raw/master/inst/docs/pres.deb1.docx' %>%
download() %>%
read_docx() %>%
head(3)
```
### Browse
`browse` is a system dependent tool for opening files and directories. In the example below we open the directory that contains the example documents used in this README.
```{r, eval=FALSE}
system.file("docs", package = "textreadr") %>%
browse()
```
We can open files as well:
```{r, eval = FALSE}
html_doc %>%
browse()
```
## Generic Document Reading
The `read_document` is a generic wrapper for `read_docx`, `read_doc`, `read_html`, `read_odt`, `read_pdf`, `read_rtf`, and `read_pptx` that detects the file extension and chooses the correct reader. For most tasks that require reading a .docx, .doc, .html, .odt, .pdf, .pptx, .rtf or .txt file this is the go-to function to get the job done. Below I demonstrate reading each of these five file formats with `read_document`.
```{r}
doc_doc %>%
read_document() %>%
head(3)
docx_doc %>%
read_document() %>%
head(3)
html_doc %>%
read_document() %>%
head(3)
odt_doc %>%
read_document() %>%
head(3)
pdf_doc %>%
read_document() %>%
head(3)
pptx_doc %>%
read_document() %>%
head(3)
rtf_doc %>%
read_document() %>%
head(3)
txt_doc %>%
read_document() %>%
paste(collapse = "\n") %>%
cat()
```
## Read Directory Contents
Often there is a need to read multiple files in from a single directory. The `read_dir` function wraps other **textreadr** functions and `lapply` to create a data frame with a document and text column (one row per document). We will read the following documents from the 'pos' directory in **textreadr**'s system file:
```
levelName
pos
|--0_9.txt
|--1_7.txt
|--10_9.txt
|--11_9.txt
|--12_9.txt
|--13_7.txt
|--14_10.txt
|--15_7.txt
|--16_7.txt
|--17_9.txt
|--18_7.txt
|--19_10.txt
|--2_9.txt
|--3_10.txt
|--4_8.txt
|--5_10.txt
|--6_10.txt
|--7_7.txt
|--8_7.txt
\--9_7.txt
```
Here we have read the files in, one row per file.
```{r}
system.file("docs/Maas2011/pos", package = "textreadr") %>%
read_dir() %>%
peek(Inf, 40)
```
## Basic Readers
### Read .doc
A .doc file is a bit trickier to read in than .docx but is made easy by the **antiword** package which wraps the [Antiword](http://www.winfield.demon.nl) program in an OS independent way.
```{r}
doc_doc %>%
read_doc() %>%
head()
```
```{r}
doc_doc %>%
read_doc(15) %>%
head(7)
```
### Read .docx
A .docx file is nothing but a fancy container. It can be parsed via XML. The `read_docx` function allows the user to read in a .docx file as plain text. Elements are essentially the p tags (explicitly `//w:t` tags collapsed with `//w:p` tags) in the markup.
```{r}
docx_doc %>%
read_docx() %>%
head(3)
```
```{r}
docx_doc %>%
read_docx(15) %>%
head(3)
```
### Read .html
Often a researcher only wishes to grab the text from the body of .html files. The `read_html` function does exactly this task. For finer control over .html scraping the user may investigate the **xml2** & **rvest** packages for parsing .html and .xml files. Here I read in HTML with `read_html`.
```{r}
html_doc %>%
read_html()
```
### Read .odt
Open Document Texts (.odt) are rather similar to .docx files in how they behave. The `read_odt` function reads them in in a similar way.
```{r}
odt_doc %>%
read_odt()
```
### Read .pdf
Like .docx a .pdf file is simply a container. Reading PDF's is made easier with a number of command line tools. A few methods of PDF reading have been incorporated into R. Here I wrap **pdftools** `pdf_text` to produce `read_pdf`, a function with sensible defaults that is designed to read PDFs into R for as many folks as possible right out of the box.
Here I read in a PDF with `read_pdf`. Notice the result is a data frame with meta data, including page numbers and element (row) ids.
```{r}
pdf_doc %>%
read_pdf()
```
#### Image Based .pdf: OCR
Image based .pdfs require optical character recognition (OCR) in order for the images to be converted to text. The `ocr` argument of `read_pdf` allows the user to read in image based .pdf files and allow the [**tesseract**](https://CRAN.R-project.org/package=tesseract) package do the heavy lifting in the backend. You can look at the .pdf we'll be using by running:
```{r,eval=FALSE}
browse(pdf_doc_img)
```
First let's try the task without using OCR.
```{r, eval = FALSE}
pdf_doc_img %>%
read_pdf(ocr = FALSE)
```
```r
## Table: [0 x 3]
##
## [1] page_id element_id text
## <0 rows> (or 0-length row.names)
## ... ... ... ...
```
And now using OCR via **tesseract**. Note that `ocr = TRUE` is the default behavior of `read_pdf`.
```{r, eval = FALSE}
pdf_doc_img %>%
read_pdf(ocr = TRUE)
```
```r
## Converting page 1 to C:\Users\AppData\Local\Temp\RtmpKeJAnL/McCune2002Choi2010_01.png... done!
## Converting page 2 to C:\Users\AppData\Local\Temp\RtmpKeJAnL/McCune2002Choi2010_02.png... done!
## Table: [104 x 3]
##
## page_id element_id text
## 1 1 1 A Survey of Binary Similarity and Distan
## 2 1 2 Seung-Seok Choi, Sung-Hyuk Cha, Charles
## 3 1 3 Department of Computer Science, Pace Uni
## 4 1 4 New York, US
## 5 1 5 ABSTRACT ecological 25 <U+FB01>sh species [2|].
## 6 1 6 conventional similarity measures to solv
## 7 1 7 The binary feature vector is one of the
## 8 1 8 representations of patterns and measurin
## 9 1 9 distance measures play a critical role i
## 10 1 10 such as clustering, classi<U+FB01>cation, etc.
## .. ... ... ...
```
### Read .pptx
Like the .docx, a .pptx file is also nothing but a fancy container. Likewise, it can be parsed via XML. The `read_pptx` function allows the user to read in a .pptx file as a data.frame with plain text that tracks slide id numbers.
```{r}
pptx_doc %>%
read_pptx()
```
### Read .rtf
Rich text format (.rtf) is a plain text document with markup similar to latex. The **striprtf** package provides the backend for `read_rtf`.
```{r}
rtf_doc %>%
read_rtf()
```
## Read Transcripts
Many researchers store their dialogue data (including interviews and observations) as a .docx or .xlsx file. Typically the data is a two column format with the person in the first column and the text in the second separated by some sort of separator (often a colon). The `read_transcript` wraps up many of these assumptions into a reader that will extract the data as a data frame with a person and text column. The `skip` argument is very important for correct parsing.
Here I read in and parse the different formats `read_transcript` handles. These are the files that will be read in:
```{r}
base_name(trans_docs)
```
### doc
```{r}
read_transcript(trans_docs[6], skip = 1)
```
### docx Simple
```{r}
read_transcript(trans_docs[1])
```
### docx With Skip
`skip` is important to capture the document structure. Here not skipping front end document matter throws an error, while `skip = 1` correctly parses the file.
```{r,error=TRUE}
read_transcript(trans_docs[2])
read_transcript(trans_docs[2], skip = 1)
```
### docx With Dash Separator
The colon is the default separator. At times other separators may be used to separate speaker and text. Here is an example where hypens are used as a separator. Notice the poor parse with colon set as the default separator the first go round.
```{r}
read_transcript(trans_docs[3], skip = 1)
read_transcript(trans_docs[3], sep = "-", skip = 1)
```
### odt
```{r}
read_transcript(trans_docs[8])
```
### rtf
```{r}
read_transcript(rtf_doc, skip = 1)
```
### xls and xlsx
```{r}
read_transcript(trans_docs[4])
read_transcript(trans_docs[5])
```
### Reading Text
Like `read.table`, `read_transcript` also has a `text` argument which is useful for demoing code.
```{r}
read_transcript(
text=
"34 The New York Times reports a lot of words here.
12 Greenwire reports a lot of words.
31 Only three words.
2 The Financial Times reports a lot of words.
9 Greenwire short.
13 The New York Times reports a lot of words again.",
col.names = c("NO", "ARTICLE"), sep = " "
)
```
### Authentic Interview
Here I read in an authentic interview transcript:
```{r}
docx_doc %>%
read_transcript(c("Person", "Dialogue"), skip = 19)
```
## Pairing textreadr
**textreadr** is but one package used in the text analysis (often the first package used). It pairs nicely with a variety of other text munging and analysis packages. In the example below I show just a few other package pairings that are used to extract case names (e.g., "Jones v. State of New York") from a [Supreme Court Database Code Book](http://scdb.wustl.edu/_brickFiles/2012_01/SCDB_2012_01_codebook.pdf). I demonstrate pairings with [**textshape**](https://github.com/trinker/textshape), [**textclean**](https://github.com/trinker/textclean), [**qdapRegex**](https://github.com/trinker/qdapRegex), and [**dplyr**](https://github.com/tidyverse/dplyr).
```{r, message=FALSE, warning=FALSE}
library(dplyr)
library(qdapRegex)
library(textreadr)
library(textshape)
library(textclean)
## Read in pdf, split on variables
dat <- 'http://scdb.wustl.edu/_brickFiles/2012_01/SCDB_2012_01_codebook.pdf' %>%
textreadr::download() %>%
textreadr::read_pdf() %>%
filter(page_id > 5 & page_id < 79) %>%
mutate(
loc = grepl('Variable Name', text, ignore.case=TRUE),
text = textclean::replace_non_ascii(text)
) %>%
textshape::split_index(which(.$loc) -1) %>%
lapply(select, -loc)
## Function to extract cases
ex_vs <- qdapRegex::ex_(pattern = "((of|[A-Z][A-Za-z'.,-]+)\\s+)+([Vv]s?\\.\\s+)(([A-Z][A-Za-z'.,-]+\\s+)*((of|[A-Z][A-Za-z',.-]+),?($|\\s+|\\d))+)")
## Extract and filter cases
dat %>%
lapply(function(x) {
x$text %>%
textshape::combine() %>%
ex_vs() %>%
c() %>%
textclean::mgsub(c("^[ ,]+", "[ ,0-9]+$", "^(See\\s+|E\\.g\\.?,)"), "", fixed=FALSE)
}) %>%
setNames(seq_along(.)) %>%
{.[sapply(., function(x) all(length(x) > 1 | !is.na(x)))]}
```
# Other Implementations
Some other implementations of text readers in R:
1. [tm](https://CRAN.R-project.org/package=tm )
1. [readtext](https://CRAN.R-project.org/package=readtext)