Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace strings in text based on dictionary

I am new to R and need suggestions. I have a dataframe with 1 text field in it. I need to fix the misspelled words in that text field. To help with that, I have a second file (dictionary) with 2 columns - the misspelled words and the correct words to replace them.

How would you recommend doing it? I wrote a simple "for loop" but the performance is an issue. The file has ~120K rows and the dictionary has ~5k rows and the program's been running for hours. The text can have a max of 2000 characters.

Here is the code:

output<-source_file$MEMO_MANUAL_TXT
for (i in 1:nrow(fix_file))  {           #dictionary file
target<-paste0(" ", fix_file$change_to_target[i], " ")
replace<-paste0(" ", fix_file$target[i], " ")
output<-gsub(target, replace, output, fixed = TRUE)
like image 688
user3100939 Avatar asked Feb 02 '26 01:02

user3100939


1 Answers

I would try agrep. I'm not sure how well it scales though.

Eg.

> agrep("laysy", c("1 lazy", "1", "1 LAZY"), max = 2, value = TRUE)
[1] "1 lazy"

Also check out pmatch and charmatch although I feel they won't be as useful to you.

like image 82
TheComeOnMan Avatar answered Feb 03 '26 17:02

TheComeOnMan