-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountingFunctions.R
61 lines (51 loc) · 2.19 KB
/
CountingFunctions.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
# Scripted by: J.L. Reyes Acosta
# e-mail: [email protected]
# MindShare WW Business planning
#MAIN FUCTION FOR COUNTING, MODIFY AT YOUR OWN RISK :P
#dataSys.corpus <- Corpus(VectorSource(res$contents))
#summary(dataSys.corpus)
## Cleaning extra spaces
removeExtraSpaces <- function(x) gsub(' {2,}',' ',x)
removepunctuationLeo <- function(x)str_replace_all(x, "[(),.:;@#!?/\\*-_]", "")
FuseBrands<-function(x,y,z){gsub(paste(y,z), paste(y,"-",z,sep=""),x)
return(x)}
#str2 <- gsub(' {2,}',' ',str1)
#dataSys.corpus <- tm_map(dataSys.corpus, tolower)
removeURL <- function(x) gsub("http://[[:alnum:]]*", "", x)
# makeCombination <- function(x) {
# y <- gsub('diamonds','diamond', x)
# y <- gsub('rings','ring', x)
# return(y)
# }
tolowerLeo<-function(x){y<-x
for (i in 1:length(LETTERS)){
y<-chartr(LETTERS[i],letters[i],y)
}
return(y)
}
#Searching for specific word and counting Spaces
CountingSpaces <- function(Sentence,Adjective,Brand){
for (i in 1:length(Sentence)){
try(RelativePositionAdjective<-which(strsplit(Sentence[i],' ')[[1]] == Adjective))
try(RelativePositionBrand<-which(strsplit(Sentence[i],' ')[[1]] == Brand))
try(SpacesBetween<-RelativePositionAdjective-RelativePositionBrand)
if (i==1){Output<-as.matrix(SpacesBetween)}else{Output<-as.matrix(rbind(Output,SpacesBetween))}
#rownames(Output)<-c(1:length(Sentence))
}
return(Output)
}
CountingWords <- function(Sentence,Adjective,Brand){
for (i in 1:length(Sentence)){
RelativePositionAdjective<-which(strsplit(Sentence[i],' ')[[1]] == Adjective)
RelativePositionBrand<-which(strsplit(Sentence[i],' ')[[1]] == Brand)
if (length(RelativePositionAdjective)>0 & length(RelativePositionBrand)>0){
WordsBetween<-if(RelativePositionAdjective-RelativePositionBrand>0)
{RelativePositionAdjective-RelativePositionBrand-1}
else{RelativePositionAdjective-RelativePositionBrand+1}
}
else{WordsBetween<-as.integer()
}
if (i==1){Output<-as.matrix(WordsBetween)}else{Output<-as.matrix(rbind(Output,WordsBetween))}
}
return(Output)
}