-
Notifications
You must be signed in to change notification settings - Fork 0
/
histogram.R
45 lines (44 loc) · 1.13 KB
/
histogram.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
folder="D:\\R Files\\tech"
print(folder)
filelist=list.files(path=folder,pattern="*.txt")
print(filelist)
filelist=paste(folder,"\\",filelist,sep="")
print(filelist)
rm(filelist)
mtext= lapply(filelist,FUN=readLines)
print(mtext)
mtext1= lapply(mtext,FUN=paste,collapse=" ")
print(mtext)
mtext1= gsub(pattern="\\d",replace=" ",mtext1)
mtext1= gsub(pattern="\\W",replace=" ",mtext1)
print(mtext1)
mtext1= tolower(mtext1)
mtext1= removeWords(mtext1,stopwords())
print(mtext1)
tb= str_split(mtext1,pattern="\\s+")
tb
class(mtext1)
tb1=unlist(tb)
print(tb1)
wordcloud(tb1,col=rainbow(7))
Corpus1=Corpus(VectorSource(mtext1))
x11()
tdm=TermDocumentMatrix(Corpus1)
print(tdm)
mat1=as.matrix(tdm)
print(mat1)
colnames(mat1)
comparison.cloud(mat1)
mtext2=str_split(mtext1,pattern="\\s+")
print(mtext2)
Poswords=scan("positive-words.txt",what='character',comment.char=";")
print(Poswords)
negwords=scan("negative-words.txt",what='character',comment.char=";")
print(negwords)
p=unlist(lapply(mtext2,function(x){sum(!is.na(match(x,Poswords)))}))
print(p)
n=unlist(lapply(mtext2,function(x){sum(!is.na(match(x,negwords)))}))
print(n)
score=p-n
print(score)
hist(score)