-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_short_description_semantic_data.R
70 lines (51 loc) · 1.94 KB
/
generate_short_description_semantic_data.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
source("functions.R")
source("variables.R")
source("extract_essays.R")
projects.data <- get.projects.data(force=FALSE)
# projects.data <- subset(projects.data, typedataset == "train")
projects.data <- subset(projects.data, days_since_posted <= nb.days)
projects.data <- merge(projects.data, essays.data, by="projectid")
library(tm)
library(plyr)
print("selection")
tmp <- projects.data[, names(projects.data) %in% c("projectid", "title.y", "short_description.y", "need_statement.y", "essay.y")]
names(tmp) <- c("projectid", "title", "short_description", "need_statement", "essay")
print("cleanup")
rm(list=c("essays.data", "projects.data"))
gc(TRUE)
print("vectorize")
docs <- tmp$short_description
names(docs) <- as.character(tmp$projectid)
ds <- VectorSource(docs)
tmp <- tmp[,c("projectid")]
rm(list=c("con", "docs", "drv", "sqlitedb.filename"))
gc(TRUE)
print("generation corpus")
corpus <- VCorpus(ds)
rm(list=c("ds"))
gc(TRUE)
print("generatition dtm")
corpus <- tm_map(corpus, removeNumbers)
corpus <- tm_map(corpus, removePunctuation)
# corpus <- tm_map(corpus, toupper)
corpus <- tm_map(corpus, stripWhitespace)
corpus <- tm_map(corpus, stemDocument)
corpus <- tm_map(corpus, removeWords, stopwords("english"))
dtm <- DocumentTermMatrix(corpus,
control=list(
weighting=weightTfIdf,
stopwords=TRUE))
rm(list=c("corpus"))
gc(TRUE)
sparsed.dtm <- removeSparseTerms(dtm, 0.90)
sparsed.dtm.tmp <- inspect(sparsed.dtm)
sparsed.dtm.tmp <- data.frame(sparsed.dtm.tmp)
colnames(sparsed.dtm.tmp) <- paste("word", "short_description", colnames(sparsed.dtm.tmp), sep=".")
# for(col in colnames(sparsed.dtm.tmp)) {
# sparsed.dtm.tmp[, col] <- ifelse(sparsed.dtm.tmp[,col] > 0, 1, 0)
# }
sparsed.dtm.tmp$projectid <- tmp
semantic.short_description.data <- sparsed.dtm.tmp
save(semantic.short_description.data, file=file.path("tmp","semantic_short_description.RData"))
rm(list=ls())
gc(TRUE)