【问题标题】:tm_map merging lines on conditiontm_map 在条件下合并行
【发布时间】:2017-04-05 21:16:17
【问题描述】:

我从 pdf 文件中提取了文本并创建了一个语料库对象。

在文本中,我有以“,”或“-”结尾的行,我想在它们后面附加下一行,因为它属于同一个句子。

比如我有

[1566] "this and other southeastern states (Eukerria saltensis,"      
[1567] "Sparganophilus helenae, Sp. tennesseensis). In the" 

而我想改为

[1566] "this and other southeastern states (Eukerria saltensis, Sparganophilus helenae, Sp. tennesseensis). In the" 

我尝试了替换换行符之类的方法,但没有成功:

tm_map(myCorpus, content_transformer(gsub), pattern =",$\n",replacement = "")

知道如何在 R 中做到这一点吗?

【问题讨论】:

    标签: r text-mining tm


    【解决方案1】:

    这是一种方法,基于您按换行符分割的想法...

    txt <- c("aaa","bbc,","df","fgh-","jkh-","dfsf","gghf")
    
    txt2 <- paste0(txt,collapse="\n")
    txt2 <- gsub(",\\n",", ",txt2)
    txt2 <- gsub("\\-\\n","-",txt2)
    txt2 <- unlist(strsplit(txt2,"\\n"))
    
    txt2
    [1] "aaa"  "bbc, df"  "fgh-jkh-dfsf"  "gghf" 
    

    【讨论】:

      【解决方案2】:

      谢谢,它确实有效!

      不过,我必须将它放入一个函数中以使其与 tm_map 一起使用:

      clean.X <- function(X){
      
        X2 <- paste0(X,collapse="\n")
        X2 <- gsub(",\\n",", ",X2)
        X2 <- gsub("\\-\\n","-",X2)
        X2 <- unlist(strsplit(X2,"\\n"))
        return(X2)
      
       }
      
      txt2 <- tm_map(txt, content_transformer(clean.X))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-11
        • 1970-01-01
        • 1970-01-01
        • 2012-05-01
        • 2017-04-12
        • 2020-11-11
        • 2017-03-08
        • 1970-01-01
        相关资源
        最近更新 更多