【问题标题】:Read the first two lines of each document in a corpus in R读取 R 语料库中每个文档的前两行
【发布时间】:2017-08-26 06:13:15
【问题描述】:

我无法弄清楚如何阅读 R 语料库中每个文档的前两行。前两行包含我要分析的新闻文章的标题。我想在标题(而不是每个文本的其余部分)中搜索“堕胎”这个词。

这是我创建语料库的代码:

myCorp <- corpus(readtext(file='~/R/win-library/3.3/quanteda/Abortion/1972/*'))

我尝试在 for 循环中使用 readLines:

for (mycorp in myCorp) {
titles <- readLines(mycorp, n = 2)
write.table(mycorp, "1972_text_P.txt", sep="\n\n", append=TRUE)
write.table(titles, "1972_text_P.txt", append=TRUE)
}

readLines(mycorp, n = 2) 中的错误:'con' 不是连接

我故意不创建 DFM,因为我想将 465 个文件作为单个文档保留在语料库中。如何从文章 textx 中获取标题?或者,理想情况下,我将如何仅在每个文档的前两行搜索关键字(堕胎)并创建一个仅包含带有关键字的标题的文件?感谢您对此提供的所有帮助。

【问题讨论】:

    标签: r for-loop corpus readlines quanteda


    【解决方案1】:

    readLines 函数需要一个连接对象作为参数。由于corpus 函数不返回连接,因此您需要在循环中创建与语料库中的字符串的连接。

    myCorp <- Corpus(quanteda::data_corpus_inaugural)
    
    for (text in myCorp$documents$texts) {
      con <- textConnection(text,)
      first_lines <- readLines(con, n = 2)
      close.connection(con)
    
      # Test if the word "speaker" is in the two lines
      if(any(grepl(pattern = "speaker",x = first_lines, ignore.case = T))){
        print(first_lines)
      }
    }
    

    【讨论】:

    • 给出错误。 Corpus not found。尽管 docs&lt;- Corpus(DirSource(folder)) 有效,但 docs$documents$texts 是空的!
    • @ThejKiran,问题是使用 tm 包上的“语料库”功能。所以需要使用require(tm)来加载库。
    • 我做了library(tm)Corpus 是函数名。不是corpus(这是我在上一条评论中想说的)。
    • 什么是data_char_inaugural
    • data_char_inauguralquanteda 库的数据集。此名称现已弃用,您需要使用 data_corpus_inaugural
    【解决方案2】:

    我建议两个选项:

    正则表达式替换只保留前 2 行

    如果您的前两行包含您需要的内容,则只需使用提取前两行的正则表达式来提取它们。这比循环快。

    @rconradin 的解决方案有效,但正如您将在 ?corpus 中指出的那样,我们强烈反对直接访问语料库对象的内部结构(因为它很快就会改变)。不循环也更快。

    # test corpus for demonstration
    testcorp <- corpus(c(
        d1 = "This is doc1, line 1.\nDoc1, Line 2.\nLine 3 of doc1.",
        d2 = "This is doc2, line 1.\nDoc2, Line 2.\nLine 3 of doc2."
    ))
    
    summary(testcorp)
    ## Corpus consisting of 2 documents.
    ## 
    ##  Text Types Tokens Sentences
    ##    d1    12     17         3
    ##    d2    12     17         3
    

    现在只用前两行覆盖文本。 (这也会丢弃第二个换行符,如果您想保留它,只需将其移动到第一个捕获组。)

    texts(testcorp) <- 
        stringi::stri_replace_all_regex(texts(testcorp), "(.*\\n.*)(\\n).*", "$1")
    ## Corpus consisting of 2 documents.
    ## 
    ##  Text Types Tokens Sentences
    ##    d1    10     12         2
    ##    d2    10     12         2
    
    texts(testcorp)
    ##                                     d1                                     d2 
    ## "This is doc1, line 1.\nDoc1, Line 2." "This is doc2, line 1.\nDoc2, Line 2." 
    

    使用corpus_segment():

    另一种解决方案是使用corpus_segment()

    testcorp2 <- corpus_segment(testcorp, what = "other", delimiter = "\\n", 
                                valuetype = "regex")
    summary(testcorp2)
    ## Corpus consisting of 6 documents.
    ## 
    ##  Text Types Tokens Sentences
    ##  d1.1     7      7         1
    ##  d1.2     5      5         1
    ##  d1.3     5      5         1
    ##  d2.1     7      7         1
    ##  d2.2     5      5         1
    ##  d2.3     5      5         1
    
    # get the serial number from each docname
    docvars(testcorp2, "sentenceno") <- 
        as.integer(gsub(".*\\.(\\d+)", "\\1", docnames(testcorp2)))
    summary(testcorp2)
    ## Corpus consisting of 6 documents.
    ## 
    ##  Text Types Tokens Sentences sentenceno
    ##  d1.1     7      7         1          1
    ##  d1.2     5      5         1          2
    ##  d1.3     5      5         1          3
    ##  d2.1     7      7         1          1
    ##  d2.2     5      5         1          2
    ##  d2.3     5      5         1          3
    
    testcorp3 <- corpus_subset(testcorp2, sentenceno <= 2)
    texts(testcorp3)
    ##                    d1.1                    d1.2                    d2.1                    d2.2 
    ## "This is doc1, line 1."         "Doc1, Line 2." "This is doc2, line 1."         "Doc2, Line 2."
    

    【讨论】:

      猜你喜欢
      • 2019-11-01
      • 2015-06-29
      • 2019-02-13
      • 1970-01-01
      • 2016-09-24
      • 2014-08-21
      • 1970-01-01
      • 1970-01-01
      • 2021-07-27
      相关资源
      最近更新 更多