0

I'm transforming a text that's being read from a pdf file.

In particular, I have a character vector which contains hyphens ("-") that preform syllabification, or separation of the words to new lines, but only when it occurs for numbers. For example:

text text text 123-
456 text text..

What I want to do is remove the all the hypens and paste those words toghether.

text text text 123456 
text text..

My starting attempt:

library(pdftools)
library(tidytext)
library(readxl)
library(dplyr)
setwd("~/Automation - Official Guazzete")
path <- getwd()
pdf_file <- file.path(path, "stecajni_postapki.pdf")

test <- pdf_text(pdf_file)

dput(tail(test)[1])
"10 јули 2017                                                     Бр. 86 - Стр. 1\r\n             Стечајни постапки\r\n                                  СТЕЧАЈНИ ПОСТАПКИ\r\n                           Основниот суд Скопје II – Скопје преку стечајниот\r\n                       судија Вероника Станојевска и привремениот стечаен\r\n                       управник Ѓорѓе Костов, објавува дека со Решение 2\r\n                       Ст. бр. 841/17 од 16.6.2017 година, се отвора стечајна\r\n                       постапка над должникот Друштво за производство, тр-\r\n                       говија КБ ТРЕЈД Ќиро ДООЕЛ Скопје, со трансакцис-\r\n                       ка сметка 300000000744414 при Комерцијална банка\r\n                       АД Скопје со ЕДБ 403099419454 Скопје, ЕМБС\r\n                       4854217 и единствен даночен број 4030003477097 и\r\n                       приоритетна дејност на мало во неспецијализирани про-\r\n                       давници претежно со храна и пијалаци... <truncated>

From here, I tried:

test <- gsub("-", "", test)

But this returns seperate numbers. For example

  1. 123 2. 456

Not one word - 123456.

Any ideas?

Prometheus
  • 1,977
  • 3
  • 30
  • 57

2 Answers2

0

If you include the \n character in your gsub, it should do it

S <- "text text text 123-
      456 text text"
"text text text 123-\n456 text text"

gsub("-\n", "", S)
# "text text text 123456 text text"
CPak
  • 13,260
  • 3
  • 30
  • 48
0

This slight variation might fix your issue. It would find instances of hyphens followed by newlines and collapse them.

test <- gsub("-\\n+", "", test)

Failing that, you would probably define a function that replaces element[i] containing "-" with itself and element[i+1], and then replaces element[i+1] with NA or some placeholder, to be deleted later.