繁体   English   中英

R:解析引文的文本文件/拆分为段落

[英]R: parsing text file of quotations / splitting into paragraphs

我正在寻找一个 R 解决方案来解析一个引用文本文件(如下),给出一个 data.frame,每个引用一个观察,变量textsource如下所述。

DIAGRAMS are of great utility for illustrating certain questions of vital statistics by
conveying ideas on the subject through the eye, which cannot be so readily grasped when
contained in figures.
--- Florence Nightingale, Mortality of the British Army, 1857

To give insight to statistical information it occurred to me, that making an
appeal to the eye when proportion and magnitude are concerned, is the best and
readiest method of conveying a distinct idea. 
--- William Playfair, The Statistical Breviary (1801), p. 2


Regarding numbers and proportions, the best way to catch the imagination is to speak to the eyes.
--- William Playfair, Elemens de statistique, Paris, 1802, p. XX.

The aim of my carte figurative is to convey promptly to the eye the relation not given quickly by numbers requiring mental calculation.
--- Charles Joseph Minard

在这里,每个引用都是一个段落,与下一个用"\\n\\n"分隔。 在段落中,直到一个开头的所有行---包括text和接下来的---source

我想我可以解决这个问题,如果我可以首先将文本行拆分为段落(由'\\\\n\\\\n+'分隔(2 个或更多空行),但我在这样做时遇到了麻烦。

假设您在rawText变量中加载了初始文本

library(stringr)

strsplit(rawText, "\n\n")[[1]] %>% 
  str_split_fixed("\n--- ", 2) %>% 
  as.data.frame() %>% 
  setNames(c("text", "source"))

这应该完成您需要实现的大部分工作。 我假设您已经在名为txt的长度为 1 的字符向量中拥有该文件:

library(tidyverse)

txt                                             %>% 
strsplit("\n{2,5}")                             %>% 
unlist()                                        %>% 
lapply(function(x) unlist(strsplit(x, "--- "))) %>%
{do.call("rbind", .)}                           %>%
as.data.frame(stringsAsFactors = FALSE)         %>%
setNames(c("Text", "Source"))                    ->
df

如果然后通过用空格替换换行符来整理文本,则会得到以下结果:

df$Text <- gsub("\n", " ", df$Text)
as_tibble(df)
#> # A tibble: 4 x 2
#>   Text                                              Source                             
#>   <chr>                                             <chr>                              
#> 1 "DIAGRAMS are of great utility for illustrating ~ Florence Nightingale, Mortality of~
#> 2 "To give insight to statistical information it o~ William Playfair, The Statistical ~
#> 3 "Regarding numbers and proportions, the best way~ William Playfair, Elemens de stati~
#> 4 "The aim of my carte figurative is to convey pro~ Charles Joseph Minard 

假设您的文本文件是工作目录中的quote.txt

R 基本解决方案:将其拆分 2 次:(1) 通过\\n\\n和 (2) 通过--- ,然后组合成数据帧。

quote <- readLines("quote.txt")
quote <- paste(quote, collapse = "\n")

DF <- strsplit(unlist(strsplit(quote, "\n\n")), "---")
DF <- data.frame(text= trimws(sapply(DF, "[[", 1)), 
           source = trimws(sapply(DF, "[[", 2)))

输出

DF
                                                                                                                                                                                                                                                                                 # text
# 1     DIAGRAMS are of great utility for illustrating certain questions of vital statistics by\nconveying ideas on the subject through the eye, which cannot be so readily grasped when\ncontained in figures.
# 2 To give insight to statistical information it occurred to me, that making an\nappeal to the eye when proportion and magnitude are concerned, is the best and\nreadiest method of conveying a distinct idea.
# 3                                                                                                           Regarding numbers and proportions, the best way to catch the imagination is to speak to the eyes.
# 4                                                                     The aim of my carte figurative is to convey promptly to the eye the relation not given quickly by numbers requiring mental calculation.
#                                                          source
# 1     Florence Nightingale, Mortality of the British Army, 1857
# 2       William Playfair, The Statistical Breviary (1801), p. 2
# 3 William Playfair, Elemens de statistique, Paris, 1802, p. XX.
# 4                                         Charles Joseph Minard

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM