-
Notifications
You must be signed in to change notification settings - Fork 1
/
2. ExtractTextsXML_year2000.R
141 lines (103 loc) · 4.05 KB
/
2. ExtractTextsXML_year2000.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
require(data.table)
require(httr)
require(rvest)
require(dplyr)
require(lubridate)
fedreg.master <- "/Users/evolkova/Dropbox/Projects/Govt Agenda/Data/Raw Data/masterfile_fedreg.csv" %>%
fread
correction <- function(x)
{
if(length(x) > 0) return(x)
if(length(x) == 0) return(NA)
}
### cleaning html files
### * drop first 13 lines and last two lines
### * drop [[Page XXX]] and [Page XXX]
### * drop [GRAPHIC] and [TIFF OMITTED]
clean.earlier.files <- function(txt)
{
txt <- txt %>%
strsplit("\n") %>%
unlist
txt <- try(txt <- txt[13:(length(txt) - 3)])
if(class(txt) == "try-error") return("")
txt <- gsub("(<a href=).*?(</a>)", "",txt, perl = T)
txt <- gsub("<SUP>.*?</SUP>", "", txt, ignore.case = T)
txt <- gsub("<span>.*?</span>", "", txt, ignore.case = T)
#txt <- gsub("</SUP>", "", txt, ignore.case = T)
txt <- gsub("\\[\\[Page \\d+\\]\\]", "" ,txt)
txt <- gsub("\\[Page \\d+\\]", "" ,txt)
txt <- gsub("\\[GRAPHIC\\]", "" ,txt)
txt <- gsub("\\[TIFF OMITTED\\]", "" ,txt)
txt <- paste0(txt, collapse = "\n")
return(txt)
}
for(yr in 1999:1995)
{
print(yr)
require(DBI)
require(RSQLite)
year.masterfile <- fedreg.master[publication_date %>% mdy %>% year == yr]
dbname <- paste0("/Users/evolkova/Dropbox/Projects/Govt Agenda/Data/Raw Data/ScriptDownload/FedReg_",yr,".sqlite")
con = dbConnect(SQLite(), dbname = dbname)
add.data <- dbGetQuery(con, 'SELECT * FROM docs')
m <- match(year.masterfile$document_number, add.data$DOCUMENTNUMBER)
year.masterfile$TYPE <- ""
year.masterfile$texts <- add.data$HTMLTEXT[m]
year.masterfile[, ind := 1:.N]
year.masterfile[,texts := clean.earlier.files(texts), by = ind]
year.masterfile[, ind := NULL]
saveRDS(year.masterfile, paste0("/Users/evolkova/Dropbox/Projects/Govt Agenda/Data/Master and Texts/",yr,".rds"))
}
###########################################################################
######################### YEAR 2000 CORREXCTION ###########################
###########################################################################
folder <- "/Users/evolkova/Dropbox/Projects/Govt Agenda/Data/Raw Data/BulkFedReg/"
daily.files <- list.files(folder,pattern = "xml$", recursive = TRUE)
types <- c("rule", "prorule", "notice", "presdocu", "correct")
for(yr in 2000)
{
print(yr)
print(Sys.time())
year.daily.files <- daily.files[grepl(paste0("FR-",yr,"-"), daily.files)]
year.masterfile <- fedreg.master[publication_date %>% mdy %>% year == yr]
texts <- NULL
for(fl in year.daily.files)
{
print(fl)
one.day <- fl %>%
paste0(folder,.) %>%
read_html
for(TYPE in types)
{
split.docs <- html_nodes(one.day, TYPE)
frtext <- lapply(split.docs, html_text) %>%
sapply(., correction)
frdocs <- lapply(split.docs, function(nd) nd %>% html_nodes("frdoc") %>% html_text) %>%
sapply(., correction)
if(length(frtext) == 0 | length(frdocs) == 0) next
if(length(frtext) != length(frdocs))
{
next
}
res <- try(tmp <- data.table(document_number = frdocs, text = frtext))
if(class(res)[1] == "try-error") next
tmp$type <- TYPE
texts <- rbind(texts, tmp)
}
}
texts[, docs := gsub(".*FR Doc. ", "", document_number)]
texts[, docs := gsub("\\s+Filed\\s+.*", "" ,docs)]
m <- match(year.masterfile$document_number, texts$docs)
year.masterfile$texts <- texts$text[m]
year.masterfile$TYPE <- texts$type[m]
}
dbname <- paste0("//Users/evolkova/Dropbox/Projects/Govt Agenda/Data/Raw Data/ScriptDownload/FedReg_2000_jan118.sqlite")
con = dbConnect(SQLite(), dbname = dbname)
add.data <- dbGetQuery(con, 'SELECT * FROM docs')
ind <- which(is.na(year.masterfile$texts))
m <- match(year.masterfile[ind]$document_number, add.data$DOCUMENTNUMBER)
year.masterfile[ind]$TYPE <- ""
year.masterfile[ind]$texts <- add.data$HTMLTEXT[m]
year.masterfile[ind, texts := clean.earlier.files(texts), by = document_number]
saveRDS(year.masterfile, "/Users/evolkova/Dropbox/Projects/Govt Agenda/Data/Master and Texts/2000.rds")