-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path6. LDAfitbyyear.R
69 lines (51 loc) · 1.62 KB
/
6. LDAfitbyyear.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
require(data.table)
require(dplyr)
require(lubridate)
require(tm)
require(topicmodels)
prepare_prosp <- function(text){
require(qdapDictionaries)
require(stringr)
text <- tolower(text)
text <- gsub("\\d+", "", text)
text <- unlist(str_extract_all(text, "\\w+"))
text <- text[text %in% GradyAugmented]
text <- text[str_length(text) > 2]
text <- text[!text %in% c(stopwords("english"))]
text <- paste0(text, collapse = " ")
return(text)
}
lda.trained <- "/Users/evolkova/Dropbox/Projects/Govt Agenda/Data/LDA_Data/lda_random_sample_topics100.rds" %>%
readRDS
outfolder <- "/Users/evolkova/Dropbox/Projects/Govt Agenda/Data/LDA_Data/"
for(yr in 1995:2000)
{
print(yr)
print(Sys.time())
dtm.name <- yr %>%
paste0(outfolder, "dtm_random_sample_topics100_",.,".rds")
lda.name <- yr %>%
paste0(outfolder, "lda_random_sample_topics100_",.,".rds")
sample <- yr %>%
paste0("/Users/evolkova/Dropbox/Projects/Govt Agenda/Data/Master and Texts/",.,".rds") %>%
readRDS
alltexts <- lapply(sample$texts, prepare_prosp)
#create corpus from vector
docs <- alltexts %>% tolower %>% VectorSource %>% Corpus
#remove stopwords
docs <- tm_map(docs, removeWords, stopwords("english"))
#remove whitespace
docs <- tm_map(docs, stripWhitespace)
#Stem document
require(SnowballC)
docs <- tm_map(docs, stemDocument)
#Create document-term matrix
dtm <- DocumentTermMatrix(docs)
#convert rownames to filenames
rownames(dtm) <- sample$document_number
ui = unique(dtm$i)
dtm = dtm[ui,]
saveRDS(dtm, dtm.name)
lda <- posterior(lda.trained, dtm)
saveRDS(lda, lda.name)
}