Как убрать разрывы строк из корпуса в R - PullRequest
0 голосов
/ 31 октября 2019

Я создал корпус из нескольких текстовых файлов (годовые отчеты). К сожалению, мой корпус содержит несколько разрывов строк (\ n и \ f), как вы можете видеть здесь:

"Page number\", \"in this Update\", \"189 – 319 to 322\", \"227 to 229 – 309 to 313 –\", \"319 to 322\", \"\", \"189\", \"\", \"2 – 34 to 35 – 319\", \"229 – 322\", \"229 – 322\", \n\"3\", \"\", \"192 to 367\", \"N.A.\", \"193 to 360\", \"361 to 367\", \"192\", \"N.A.\", \"\", \"194 to 197\", \"N.A.\", \"\", \"36 to 45 – 203 to 204 –\", \"302 to 304 – 317 to 318 –\", \"340 to 355\", \"\", \"\\fDocument obtained by Fitch\", \"from publicly available sources\", \"\", \"Photos credits: Getty Images\", \"Design and production:\", \"Tel.: +33 (0)1 55 32 29 74\", \"\", \"\\fDocument obtained by Fitch\", \"from publicly available sources\", \"\", \"A French limited company with share capital of €8,599,311,468\", \"Nanterre Trade and Company Registry No. 784 608 416 RCS\", \n\"12 place des États-Unis • 92127 Montrouge Cedex • France\", \"Tel. (33) 1 43 23 52 02 • www.credit-agricole.com\", \"\", \"\\f\")"

Было бы здорово, если бы кто-нибудь мог помочь мне удалить эти разрывы.

Я пробовал следующее:

corpustext3 <- tm_map(corpus, content_transformer(gsub), pattern = "\n", replacement = "")
corpustext4 <- tm_map(corpustext3, content_transformer(gsub), pattern = "\\f", replacement = "")

Это работает для \ n, но не удаляет \ f.

Мой код:

filenames <- list.files(pattern = "txt$")
files <- lapply(filenames, readLines)

corpus <- Corpus(VectorSource(files), readerControl = list(language="lat"))

corpustext2 <- tm_map(corpus, content_transformer(gsub), pattern = "\n", replacement = "")
corpustext3 <- tm_map(corpustext2, content_transformer(gsub), pattern = "\\f", replacement = "")

Результат:

"Page number\", \"in this Update\", \"189 – 319 to 322\", \"227 to 229 – 309 to 313 –\", \"319 to 322\", \"\", \"189\", \"\", \"2 – 34 to 35 – 319\", \"229 – 322\", \"229 – 322\", \"3\", \"\", \"192 to 367\", \"N.A.\", \"193 to 360\", \"361 to 367\", \"192\", \"N.A.\", \"\", \"194 to 197\", \"N.A.\", \"\", \"36 to 45 – 203 to 204 –\", \"302 to 304 – 317 to 318 –\", \"340 to 355\", \"\", \"\\fDocument obtained by Fitch\", \"from publicly available sources\", \"\", \"Photos credits: Getty Images\", \"Design and production:\", \"Tel.: +33 (0)1 55 32 29 74\", \"\", \"\\fDocument obtained by Fitch\", \"from publicly available sources\", \"\", \"A French limited company with share capital of €8,599,311,468\", \"Nanterre Trade and Company Registry No. 784 608 416 RCS\", \"12 place des États-Unis • 92127 Montrouge Cedex • France\", \"Tel. (33) 1 43 23 52 02 • www.credit-agricole.com\", \"\", \"\\f\")"
...