Я следую коду этого урока https://thewikihow.com/video_JyMBwydhYR8
All_Files <- list.files(pattern = "pdf$")
All_opinions <- lapply(All_Files, pdf_text)
document <- Corpus(VectorSource(All_opinions))
social_sentences <- document %>%
tolower() %>%
paste0(collapse= " ") %>%
stringr::str_squish() %>%
stringr::str_split(fixed(".")) %>%
unlist() %>%
tm::removePunctuation()
Но после создания вектора «social_sentences» разрывы строк не были удалены.
Вместо этого после удаления знаков препинания остается только буква «н», которая соединяется с ближайшими словами.
Даже в уроке это можно увидеть со словом «холм».
Функция str_squish() уже является частью кода, и я даже изменил ее место, чтобы посмотреть, решит ли она проблему. Я также попробовал функции gsub() и str_replace_all().





Действительно, в видео появились лишние n символов. Но на самом деле код полностью удаляет \n.
Попробуй это:
text <- "\n\nString with excess, trailing and: leading! white space\n\n"
text %>%
tolower() %>%
paste0(collapse= " ") %>%
stringr::str_split(fixed(".")) %>%
unlist() %>%
tm::removePunctuation() %>%
stringr::str_squish()
Результат:
[1] "string with excess trailing and leading white space"
просто добавьте str_replace_all("\\\\n", " ") в свою трубку:
> pdf_text("stack_1003.pdf") |>
+ VectorSource()|>
+ Corpus() |>
+ tolower() |>
+ unlist() |>
+ paste0(collapse= " ") |>
+ str_split(fixed(".")) |>
+ str_replace_all("\\\\n", " ") |>
+ removePunctuation() |>
+ str_squish()
[1] "c string with excess trailing and leading white space string with excess trailing and leading white space string with excess trailing and leading white space string with excess trailing and leading white space string with excess trailing and leading white space string with excess trailing and leading white space string with excess trailing and leading white space string with excess trailing and leading white space string with excess trailing and leading white space string with excess trailing and leading white space string with excess trailing and leading white space string with excess trailing and leading white space string with excess trailing and leading white space string with excess trailing and leading white space string with excess trailing and leading white space string with excess trailing and leading white space string with excess trailing and leading white space string with excess trailing and leading white space string with excess trailing and leading white space
Я провел несколько тестов и обнаружил, что ошибка связана с форматом PDF, когда в нем более одной страницы. Пожалуйста, попытайтесь создать PDF-документ, содержащий более одной страницы, повторив ту же самую фразу, которую вы мне показывали: «Строка с лишним, конечным и начальным!» белое пространство. Нет необходимости включать разрывы строк; результат для меня уже включал "nstring", "withnexcess",...
Почему вы уверены, что переносы строк не были удалены? Я попробовал код с небольшим предложением, но
\nне появилось. Создайте компактный воспроизводимый пример и покажите результат.