Удалить пробел после лемматизации

Я просто лемматизировал вектор символов. Проблема в том, что лемматизация создает пробел между словами, объединенными тире (например, краткосрочный становится краткосрочный). Мой вектор символов полон этих слов, поэтому я хотел бы найти способ убрать это искажение.

Позвольте мне привести пример:

text <- c("Stackoverflow is a great website where you can find great and very skilled people who are so kind to solve your coding problems. In the short-term is a very good thing because you can speed up your research, in the long-term is better if you learn how to code on your own. Let me add more non-sense to make my point. The growth-friendly composition of public finance is a good thing.")

ch_vector <- lemmatize_strings(text)

Как я уже сказал, результат таков:

"Stackoverflow be a great website where you can find great and very skill people who be so kind to solve your code problem. In the **short - term** be a very good thing because you can speed up your research, in the **long - term** be good if you learn how to code on your own. Let me add much **non - sense** to make my point. The **growth - friendly** composition of public finance be a good thing."

Вместо этого я хочу этого:

"Stackoverflow be a great website where you can find great and very skill people who be so kind to solve your code problem. In the **short-term** be a very good thing because you can speed up your research, in the **long-term** be good if you learn how to code on your own. Let me add much **non-sense** to make my point. The **growth-friendly** composition of public finance be a good thing."

До сих пор я делал это таким образом для каждого интересующего слова:

ch <- sub(pattern = "growth - friendly", replacement = "growth-friendly", x = ch_vector, fixed = TRUE)

Но это честно трудоемко, неэффективно и не всегда нормально работает (зависит от заглавных букв и т.д.)

Можете ли вы предложить лучший способ сделать это?

Большое спасибо


person Rollo99    schedule 13.11.2019    source источник


Ответы (1)


x <- "Stackoverflow be a great website where you can find great and very skill people who be so kind to solve your code problem. In the **short - term** be a very good thing because you can speed up your research, in the **long - term** be good if you learn how to code on your own. Let me add much **non - sense** to make my point. The **growth - friendly** composition of public finance be a good thing."

Использование функции gsub() для замены всех тире окружающими пробелами на один тире, кажется, может выполнить то, что вам нужно, с минимальными усилиями.

gsub(" - ","-",x)

# [1] "Stackoverflow be a great website where you can find great and very skill people
# who be so kind to solve your code problem. In the **short-term** be a very good thing
# because you can speed up your research, in the **long-term** be good if you learn how to
# code on your own. Let me add much **non-sense** to make my point. The 
# **growth-friendly** composition of public finance be a good thing."

Однако я не уверен, как это будет взаимодействовать с проектным использованием с пакетом textstem, так что это может соответствовать или не соответствовать вашим потребностям.

person Matt Summersgill    schedule 13.11.2019
comment
Это работает очень хорошо, танки! Я пробовал это сам, но со следующим кодом sub(" - ", "-", x), но это не сработало. Тем не менее, с gsub работает прекрасно! Спасибо еще раз - person Rollo99; 13.11.2019