如何删除通过比较文本而不是使用词典找到的相同单词?

问题描述 投票:0回答:1

我正在比较两个相似的文本。 x1是模型文本,x2是有错误的文本(例如,拼写,新字符等)。我试图删除在两个文本中找到的相同词。由于我的实际文字不是英语,因此无法使用字典。

我尝试过逐步遍历x1的每个字符,如果它在x2中是相同的字符,则从x2删除并移至x1的下一个字符。

我一直在研究的代码:

    x1 <- "This is a test. Weather is fine. What do I do? I am clueless this coding. Let’s do this as soon as possible." 
    x2 <- "This text is a test. This weather is fine. What id I do? I am clueless thius coding. Ley’s do ythis as soon as possiblke."

    x1<-str_split(x1, "(?<=\\.)\\s")
    x1<- lapply(x1,tolower)
    x2<-str_split(x2, "(?<=\\.)\\s")
    x2<- lapply(x2,tolower)
    library(tidyverse)
    delete_a_from_b <- function(a,b){
      a_as_list <- str_remove_all(a,"word") %>% 
         str_split(boundary("character")) %>% unlist
      b_n <- nchar(b)
      b_as_list <- str_remove_all(b,"word") %>% 
         str_split(boundary("character")) %>% unlist
      previous_j <-1
      for(i in 1:length(a_as_list))
      {

        if(previous_j > length(b_as_list)) 
           break
          for (j in previous_j:length(b_as_list)){

             if(a_as_list[[i]]==b_as_list[[j]]){
               b_as_list[[j]] <- ""
               previous_j <- j+1
               break
              }
            }
          }

     print(paste0(b_as_list,collapse = ""))
     paste0(b_as_list,collapse = "")
    }

    x3 <- delete_a_from_b(x1,x2)
    x3<-strsplit(x3,"\\s")

输出:“文本”,“ this”,“ i”,“ i”,“ d?am”,“笨拙”,“ thius”,“ coing”,“ \” ley's,“ dysssoonoon”,“ s”,

[我想要的是:'文本''this''id''thius''ley's'ythis''possiblke'

我正在比较两个相似的文本。 x1是模型文本,x2是有错误的文本(例如,拼写,新字符等)。我试图删除在两个文本中找到的相同词。由于我实际的...

r text-mining
1个回答
0
投票

认为我做到了,这是您需要的吗?

© www.soinside.com 2019 - 2024. All rights reserved.