【问题标题】:Splitting a document from a tm Corpus into multiple documents将 tm 语料库中的文档拆分为多个文档
【发布时间】:2015-06-17 20:31:28
【问题描述】:

有点奇怪的问题,有没有办法将已经使用 tm 中的语料库功能导入的语料库文档拆分为多个文档,然后可以在我的语料库中作为单独的文档重新阅读?例如,如果我使用 inspect(documents[1]) 有类似的东西

`<<VCorpus (documents: 1, metadata (corpus/indexed): 0/0)>>`

`[[1]]`

`<<PlainTextDocument (metadata: 7)>>`

The quick brown fox jumped over the lazy dog

I think cats are really cool

I want to split after this line!!!

Hi mom

Purple is my favorite color

I want to split after this line!!!

Words

And stuff

我想在“我想在这一行之后分割!!!”这句话之后分割文档在这种情况下出现两次,这可能吗?

使用inspect(documents)后的最终结果是这样的

&lt;&lt;VCorpus (documents: 1, metadata (corpus/indexed): 0/0)&gt;&gt;

[[1]]

&lt;&lt;PlainTextDocument (metadata: 7)&gt;&gt;

敏捷的棕狐跳过了懒狗

我觉得猫真的很酷

我想在这条线之后拆分!!!

[[2]]

&lt;&lt;PlainTextDocument (metadata: 7)&gt;&gt;

你好妈妈

紫色是我最喜欢的颜色

我想在这条线之后拆分!!!

[[3]]

&lt;&lt;PlainTextDocument (metadata: 7)&gt;&gt;

单词

还有东西

【问题讨论】:

  • 也许你可以操纵内部,但为什么呢?你想达到什么目的?你是否例如想要为小节生成单独的词频或 tfidf 矩阵?
  • 我有代码可以从许多格式相同的文档中提取数据,但事实证明,“真实”数据(与我被允许在办公室计算机上使用的测试数据相反)具有PDF 合并而不是拆分。我认为在 R 中处理它比手动拆分数百个文档更容易

标签: regex r split tm text-analysis


【解决方案1】:

您可以使用strsplit 拆分您的文档,然后重新创建语料库:

Corpus(VectorSource(
          strsplit(as.character(documents[[1]]),  ## coerce to character
          "I want to split after this line!!!",   
          fixed=TRUE)[[1]]))       ## use fixed=T since you  have special
                                   ## characters in your separator  

要对此进行测试,我们应该首先创建一个可重现的示例:

documents <- Corpus(VectorSource(paste(readLines(textConnection("The quick brown fox jumped over the lazy dog
I think cats are really cool
I want to split after this line!!!
Hi mom
Purple is my favorite color
I want to split after this line!!!
Words
And stuff")),collapse='\n')))

然后应用以前的解决方案:

split.docs <- Corpus(VectorSource(
  strsplit(as.character(documents[[1]]),  ## coerce to character
           "I want to split after this line!!!",   
           fixed=TRUE)[[1]]))  

现在检查解决方案:

inspect(split.docs)
<<VCorpus (documents: 3, metadata (corpus/indexed): 0/0)>>

[[1]]
<<PlainTextDocument (metadata: 7)>>
The quick brown fox jumped over the lazy dog
I think cats are really cool


[[2]]
<<PlainTextDocument (metadata: 7)>>

Hi mom
Purple is my favorite color


[[3]]
<<PlainTextDocument (metadata: 7)>>

Words
And stuff

看起来strsplit 删除了分隔符:)

【讨论】:

    【解决方案2】:

    这是一个更简单的方法,使用 quanteda 包:

    require(quanteda)
    segment(mytext, what = "other", delimiter = "I want to split after this line!!!")
    

    这会生成一个长度为 1 的列表(因为它被设计为包含多个文档,如果您愿意的话)但如果您只想要一个向量,您可以随时 unlist() 它。

    [[1]]
    [1] "The quick brown fox jumped over the lazy dog\n\nI think cats are really cool\n\n"
    [2] "\n    \nHi mom\n\nPurple is my favorite color\n\n"                               
    [3] "\n    \nWords\n\nAnd stuff" 
    

    可以使用corpus(mytextSegmented)tm 语料库将其读回quanteda 语料库以进行后续处理。

    【讨论】:

    • 谢谢!我在我的文档docs[1] 上尝试了这种方法,这是一个 VCorpus。我使用segment(docs[1], what = "other", delimiter = "I want to split after this line!!!") 并得到错误:没有适用于“段”的方法应用于类“c('VCorpus','Corpus')”的对象。有什么建议吗?
    • 是的,您的选择是:a) 从 VCorpus 中提取文本,以便您的 mytext 在我的示例中是一个字符对象 - 老实说,我不知道如何在 @987654331 中执行此操作@,这也是我们开发quanteda的原因之一; b) 根本不创建 VCorpus,直接在你的向量源字符对象上使用segment();或 c) 在 quanteda 语料库上使用 segment(),您可以使用 quanteda::corpus(myVCorpus) 从您的 VCorpus 对象创建该语料库。当然,用我的示例对象名称替换您的实际名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 2015-11-21
    • 2015-03-06
    • 2016-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多