【问题标题】:Extracting sentences using scan() in R在 R 中使用 scan() 提取句子
【发布时间】:2015-02-17 08:35:12
【问题描述】:

有人告诉我,我不应该使用 R 来扫描文本(但无论如何,我一直在这样做,直到获得其他技能)并且遇到了一个让我感到困惑的问题,足以让我退回到这些论坛。提前感谢您的帮助。

我正在尝试将大量文本(例如,短篇小说)存储为字符串向量,每个字符串都是一个单独的句子。我一直在使用 scan() 函数执行此操作,但遇到了两个基本问题:(1) scan() 似乎只允许单个分隔字符,而句子显然可以以多种方式结束。我知道如何使用正则表达式标记句子的结尾(例如 [!?\.],但我不知道 R 中使用正则表达式拆分文本的函数。(2)scan() 似乎自动考虑一个新行作为一个新字段,而我希望它忽略新行,除非它们与句子的结尾重合。

download.file("http://www.textfiles.com/stories/3lpigs.txt","threelittlepigs.txt")
threelittlepigs_s<-scan("threelittlepigs.txt",character(0),
                    sep=".",quote=NULL)

如果我不包含 'quote=NULL' 选项,scan() 会抛出警告,即 EOF(我猜是字段结尾)落在带引号的字符串中。这会产生一些多行元素/字段,但非常不稳定。我似乎无法辨别模式。

对不起,如果以前有人问过这个问题。我确信有一个简单的解决方案。我更喜欢一个能帮助我理解为什么 scan() 没有按照我期望的方式工作的工具,但是如果有更好的工具来阅读 R 中的文本,请告诉我。

【问题讨论】:

标签: regex r string text


【解决方案1】:

R 有一些非常强大的文本挖掘能力,有很多强大的包。例如tmrveststringi 等。

但这里是一个几乎完全在基础 R 中执行此操作的简单示例。我只使用来自 magrittr%&gt;% 管道,因为我认为这使代码更具可读性。

您的问题的具体答案是您可以使用正则表达式来搜索多个标点符号。在下面的示例中,我使用"[\\.?!] ",表示句号、问号或感叹号,后跟一个空格。您可能需要进行实验。

试试这个:

library("magrittr")
url <- "http://www.textfiles.com/stories/3lpigs.txt"

corpus <- url %>% 
  paste(readLines(url), collapse=" ") %>% 
  gsub("http://www.textfiles.com/stories/3lpigs.txt", "", .)

head(corpus)

z <- corpus %>% 
  gsub(" +", " ", .) %>% 
  strsplit(split = "[\\.?!] ")

z[[1]]

结果:

 z[[1]]
 [1] " THE THREE LITTLE PIGS Once upon a time "                                                                                                                                                                                                       
 [2] ""                                                                                                                                                                                                                                               
 [3] ""                                                                                                                                                                                                                                               
 [4] "there were three little pigs, who left their mummy and daddy to see the world"                                                                                                                                                                  
 [5] "All summer long, they roamed through the woods and over the plains,playing games and having fun"                                                                                                                                                
 [6] "None were happier than the three little pigs, and they easily made friends with everyone"                                                                                                                                                       
 [7] "Wherever they went, they were given a warm welcome, but as summer drew to a close, they realized that folk were drifting back to their usual jobs, and preparing for winter"                                                                    
 [8] "Autumn came and it began to rain"                                                                                                                                                                                                               
 [9] "The three little pigs started to feel they needed a real home"                                                                                                                                                                                  
[10] "Sadly they knew that the fun was over now and they must set to work like the others, or they'd be left in the cold and rain, with no roof over their heads"                  

...etc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-29
    • 2018-12-01
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 2022-12-19
    相关资源
    最近更新 更多