sent1 = data.frame(Sentences=c("abundant bad abnormal activity was due to 2- face people","strange exciting activity was due to great 2-face people"), user = c(1,2))
pos1 = c("abound" , "abounds", "abundant", "exciting", "great")
neg1 = c("2-face","abnormal", "strange", "bad", "weird")
Then I used:
words = (str_split(unlist(sent1$Sentences)," "))
tmp <- data.frame()
tmn <- data.frame()
for (i in 1:nrow(sent1)) {
for (j in 1:length(words)) {
for (k in 1:length(pos1)){
if (words[[i]][j] == pos1[k]) {
print(paste(i,words[[i]][j],1))
tmn <- cbind(i,words[[i]][j],1)
tmp <- rbind(tmp,tmn)
}
}
for (m in 1:length(neg1)){
if (words[[i]][j] == neg1[m]) {
print(paste(i,words[[i]][j],-1))
tmn <- cbind(i,words[[i]][j],-1)
tmp <- rbind(tmp,tmn)
}
}
}
}
That resulted into:
print(tmp)
i V2 V3
1 1 abundant 1
2 1 bad -1
3 2 strange -1
4 2 exciting 1
If I do something like that:
sent1$Sentences <- as.character(sent1$Sentences)
List <- strsplit(sent1$Sentences, " ")
a <- data.frame(Id=rep(sent1$user, sapply(List, length)), Words=unlist(List))
a$Words <- as.character(a$Words)
a[a$Words %in% pos1,]
resulted into possitive:
Id Words
1 abundant
2 exciting
2 great
and negative:
a[a$Words %in% neg1,]
Id Words
1 bad
1 abnormal
1 2-face
2 strange
2 2-face
But I need to add value 1 for possitive and -1 for negative words.