How to run sentiment So, that I can get result for every file separately

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:
Screenshot 2022-03-15 182038
I want this same output for 542 files. Thanks in advance.

I would write a function that accepts the filename as an input argument and returns a one-row data frame with the results for that file. Then, you can produce a dataframe of the entire results with map_dfr(setNames(files, files), my_function, .id = "filename").

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.