Есть ли в R эффективный способ поиска и замены слов в нескольких строках в таблице? - PullRequest
0 голосов
/ 25 марта 2020

У меня есть столбик, и в этом столбце есть столбец с именем "описание". Здесь имеется около 380 000 описаний.

Пример описания:

"Сокращения очень полезны"

This это просто пример, чтобы познакомить вас с моими данными. Все описания разные.

У меня также есть тиббл с правильно написанными словами. Приблизительно 42 000 уникальных правильно написанных слов.

Моя задача состоит в том, чтобы заменить все слова с ошибками в описаниях правильно написанными словами. Таким образом, слово «hlpful» будет заменено на «полезный».

Мой код выглядит следующим образом:

countKeyWords <- 1
countDescriptions <- 1
amountKeyWords <- 42083
amountDescriptions <- 379571
while (countKeyWords < amountKeyWords){
  while (countDescriptions < amountDescriptions){
    semiFormatTet$description[countDescriptions] <-
      gsub(keyWords$SearchFor[countKeyWords], keyWords$Map[countKeyWords], semiFormatTet$description[countDescriptions], ignore.case = TRUE)
    countDescriptions = countDescriptions + 1
  }
  countDescriptions = 0
  countKeyWords = countKeyWords + 1
}

Примечание :

  • SearchFor: Префикс правильно написанных слов для сравнения с словом с ошибкой в ​​описании.

  • Карта: правильно написанное слово, которое заменит слово с ошибкой ,

Таким образом, l oop будет выполняться почти 16 000 000 000 раз. Это очень неэффективно, как бы я сделал этот l oop более эффективным, чтобы мне не пришлось ждать месяц до его окончания sh?

1 Ответ

1 голос
/ 26 марта 2020

Если я не ошибаюсь, это то, что вы ищете?

Library(stringr)
Library(Tidyverse)
Library(dplyr)
df <- data.frame(DESCRIPTION = c("This is the first description with hlpful", 
                              "This is the second description with hlpful", 
                              "This is the third description with hlpful", 
                              "This is the fourth description with hlpful", 
                              "This is the fifth description with hlpful", 
                              "This is the sixth description with hlpful",
                              "This is the seventh description with hlpful",
                              "This is the eighth description with hlpful",
                              "This is the ninth description with hlpful"))

df $ DESCRIPTION <- str_replace_all (df $ DESCRIPTION, "hlpful", "полезно") </p>

      DESCRIPTION
1   This is the first description with helpful
2  This is the second description with helpful
3   This is the third description with helpful
4  This is the fourth description with helpful
5   This is the fifth description with helpful
6   This is the sixth description with helpful
7 This is the seventh description with helpful
8  This is the eighth description with helpful
9   This is the ninth description with helpful
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...