使用 R 解析语音记录
Parsing Speech Transcripts Using R
我有几份大型演讲稿,我正试图将其转化为一种数据帧格式,其中每一行代表一个 speech/utterance,相应的演讲者姓名在一列中。
这是文本当前结构的快照:
"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.)"
""
我已使用以下循环尝试以某种方式解析文本,使每一行代表一位发言人和相应的 speech/utterance:
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 没有提供错误消息。
我是解析新手,还是 R 的新手,所以我确定这是我的问题的一部分。任何建议将不胜感激。
问题看起来很简单。该函数采用一个名为 testtxt 的变量。但是,在您引用 testtext 的函数体中(注意额外的 e)。你 return 的变量,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.)"
很难确切知道您的输入格式是什么,因为该示例无法完全重现,但我们假设您在问题中打印的文本是来自单个文本文件的行。在这里,我将它(没有双引号)保存为这样一个文本文件,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")
我有几份大型演讲稿,我正试图将其转化为一种数据帧格式,其中每一行代表一个 speech/utterance,相应的演讲者姓名在一列中。
这是文本当前结构的快照:
"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.)"
""
我已使用以下循环尝试以某种方式解析文本,使每一行代表一位发言人和相应的 speech/utterance:
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 没有提供错误消息。
我是解析新手,还是 R 的新手,所以我确定这是我的问题的一部分。任何建议将不胜感激。
问题看起来很简单。该函数采用一个名为 testtxt 的变量。但是,在您引用 testtext 的函数体中(注意额外的 e)。你 return 的变量,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.)"
很难确切知道您的输入格式是什么,因为该示例无法完全重现,但我们假设您在问题中打印的文本是来自单个文本文件的行。在这里,我将它(没有双引号)保存为这样一个文本文件,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")