I am working on Sentiment Analysis. I have 542 text files and have to get sentiment scores for each of the files separately But in 1 Loop. How can I add for loop that prints result separately?
Here is the code:
path = 'C:/Users/Hamza/OneDrive/Desktop/talha.R/Project 5/text.files'
setwd(path)
getwd()
fileslist = list.files(pattern = ",*.txt")
datalist = lapply(fileslist, function(x) readLines(x))
datafr = do.call("rbind", datalist)
view(datalist[1])
filename <- datalist
filename <- trimws(filename)
filetext <- glue(read_file(filename))
filetext <- gsub("\\$", "", filetext)
tokens <- tibble(text = filetext) %>% unnest_tokens(word, text)
tokens %>%
inner_join(get_sentiments("loughran")) %>%
count(sentiment) %>%
spread(sentiment, n, fill = 0) %>%
mutate(sentiment = positive - negative)
This is the sample of the result:
I want this same output for 542 files. Thanks in advance.