我有几个大的发言记录,我试图进入一种数据帧格式,其中每一行代表一个演讲/发言,相应的发言者名称在一个列中。
以下是当前结构的文本快照:
"sr. presidente domínguez.- "
" Tiene la palabra el señor diputado por Buenos Aires."
""
"sr. ATANASOF, ALFREDO NESTOR.- "
" Señor presidente: también quiero adherir en nombre del Frente Peronista a este homenaje al Gringo Soria. "
" Me tocó compartir con él muchos años de trabajo en esta Cámara y luego funciones en el Poder Ejecutivo nacional. Realmente, durante esos años pude descubrir los valores del Gringo: un gran militante y peronista, quien siempre anteponía la resolución de los temas a las situaciones de conflicto."
" Hemos sentido mucho dolor cuando nos enteramos de esta desgraciada situación. Por ello, en nombre de nuestro bloque, quiero adherir al homenaje que hacemos a un amigo. Justamente, el Gringo Soria era un amigo para mí. (Aplausos.)"
""我使用下面的循环尝试以一种方式解析文本,以便每一行代表一个说话者和相应的语音/话语:
test <- readtext(text)
testtxt <- test$text
trans.prep <- function(testtxt) {
testtxt <- gsub("\\s{2,}", " ", testtxt, perl = T)
#gets rid of double spaces and replaces them with single spaces
testtxt <- subset(testtxt, nchar(testtxt) > 0)
#gets rid of lines that are empty (length of line is zero)
#collapse down to utterances
my.line <- 1
while (my.line <= length (testtxt)) {
utterance <- length(grep(".-", testtxt[my.line], perl = T))
if (utterance == 1) {my.line <- my.line + 1 }
if (utterance == 0) {testtext[my.line-1] <-paste(testtext[(my.line-1):my.line], collapse = " ")
testtext <- testtext[-my.line]} }
testtxt <- subset(testtxt, nchar(testtxt) > 0)
return(testtxt)}循环应该返回解析的记录,但是当我运行循环时,什么都没有发生,而R没有提供错误消息。
我对解析还是新手,所以我确信这是我的问题的一部分。如有任何建议,将不胜感激。
发布于 2019-01-09 06:14:32
很难确切知道您的输入格式是什么,因为这个示例不能完全复制,但是让我们假设问题中打印的文本是来自单个文本文件的行。在这里,我将它保存为这样一个文本文件example.txt (没有双引号)。
我们为这个用例设计了corpus_segment()。
library("quanteda")
## Package version: 1.3.14
example_corpus <- readtext::readtext("example.txt") %>%
corpus()
summary(example_corpus)
## Corpus consisting of 1 document:
##
## Text Types Tokens Sentences
## example.txt 93 141 8
##
## Source: /private/var/folders/1v/ps2x_tvd0yg0lypdlshg_vwc0000gp/T/RtmpXk3YHc/reprex1325b73a1073d/* on x86_64 by kbenoit
## Created: Wed Jan 9 19:09:55 2019
## Notes:
example_corpus2 <-
corpus_segment(example_corpus, pattern = "sr\\..*-", valuetype = "regex")
summary(example_corpus2)
## Corpus consisting of 2 documents:
##
## Text Types Tokens Sentences pattern
## example.txt.1 10 10 1 sr. presidente domínguez.-
## example.txt.2 80 117 7 sr. ATANASOF, ALFREDO NESTOR.-
##
## Source: /private/var/folders/1v/ps2x_tvd0yg0lypdlshg_vwc0000gp/T/RtmpXk3YHc/reprex1325b73a1073d/* on x86_64 by kbenoit
## Created: Wed Jan 9 19:09:55 2019
## Notes: corpus_segment.corpus(example_corpus, pattern = "sr\\..*-", valuetype = "regex")我们可以把它整理一下。
# clean up pattern by removing unneeded elements
docvars(example_corpus2, "pattern") <-
stringi::stri_replace_all_fixed(docvars(example_corpus2, "pattern"),
c("sr. ", ".-"), "",
vectorize_all = FALSE
)
names(docvars(example_corpus2))[1] <- "speaker"
summary(example_corpus2)
## Corpus consisting of 2 documents:
##
## Text Types Tokens Sentences speaker
## example.txt.1 10 10 1 presidente domínguez
## example.txt.2 80 117 7 ATANASOF, ALFREDO NESTOR
##
## Source: /private/var/folders/1v/ps2x_tvd0yg0lypdlshg_vwc0000gp/T/RtmpXk3YHc/reprex1325b73a1073d/* on x86_64 by kbenoit
## Created: Wed Jan 9 19:09:55 2019
## Notes: corpus_segment.corpus(example_corpus, pattern = "sr\\..*-", valuetype = "regex")发布于 2019-01-08 17:45:26
这个问题似乎很简单。函数接受一个名为testtxt的变量。但是,在函数的正文中,您引用了testtext (注意额外的e)。您返回的变量testtxt不会在函数中被修改。要想在将来捕捉到这一点,请尝试从一个空工作区开始。
下面是一个对我有用的函数(您必须检查结果是否与您预期的一样)。
> text <- c("sr. presidente domínguez.- ",
+ " Tiene la palabra el señor diputado por Buenos Aires.",
+ "",
+ "sr. ATANASOF, ALFREDO NESTOR.- ",
+ " Señor presidente: también quiero adherir en nombre del Frente Peronista a este homenaje al Gringo Soria. ",
+ " Me tocó compartir con él muchos años de trabajo en esta Cámara y luego funciones en el Poder Ejecutivo nacional. Realmente, durante esos años pude descubrir los valores del Gringo: un gran militante y peronista, quien siempre anteponía la resolución de los temas a las situaciones de conflicto.",
+ " Hemos sentido mucho dolor cuando nos enteramos de esta desgraciada situación. Por ello, en nombre de nuestro bloque, quiero adherir al homenaje que hacemos a un amigo. Justamente, el Gringo Soria era un amigo para mí. (Aplausos.)",
+ "")
>
> trans.prep <- function(testtxt) {
+
+ testtxt <- gsub("\\s{2,}", " ", testtxt, perl = T)
+ #gets rid of double spaces and replaces them with single spaces
+
+ testtxt <- subset(testtxt, nchar(testtxt) > 0)
+ #gets rid of lines that are empty (length of line is zero)
+
+ #collapse down to utterances
+
+ my.line <- 1
+
+ while (my.line <= length (testtxt)) {
+ print(my.line)
+
+ utterance <- length(grep(".-", testtxt[my.line], perl = T))
+ if (utterance == 1) {my.line <- my.line + 1 }
+ if (utterance == 0) {testtxt[my.line-1] <-paste(testtxt[(my.line-1):my.line], collapse = " ")
+ testtxt <- testtxt[-my.line]} }
+ testtxt <- subset(testtxt, nchar(testtxt) > 0)
+
+ return(testtxt)}
>
> k <- trans.prep(text)
[1] 1
[1] 2
[1] 2
[1] 3
[1] 3
[1] 3
> k
[1] "sr. presidente domínguez.- Tiene la palabra el señor diputado por Buenos Aires."
[2] "sr. ATANASOF, ALFREDO NESTOR.- Señor presidente: también quiero adherir en nombre del Frente Peronista a este homenaje al Gringo Soria. Me tocó compartir con él muchos años de trabajo en esta Cámara y luego funciones en el Poder Ejecutivo nacional. Realmente, durante esos años pude descubrir los valores del Gringo: un gran militante y peronista, quien siempre anteponía la resolución de los temas a las situaciones de conflicto. Hemos sentido mucho dolor cuando nos enteramos de esta desgraciada situación. Por ello, en nombre de nuestro bloque, quiero adherir al homenaje que hacemos a un amigo. Justamente, el Gringo Soria era un amigo para mí. (Aplausos.)"https://stackoverflow.com/questions/54096936
复制相似问题