繁体   English   中英

将语音开始和结束时间转换为时间序列

[英]Convert Speech Start and End Time into Time Series

我希望将以下 R 数据帧转换为以秒为索引的数据帧,但不知道该怎么做。 也许 dcast 但随后对如何扩展正在说的单词感到困惑。

startTime endTime           word
1     1.900s  2.300s         hey
2     2.300s  2.800s         I'm
3     2.800s      3s        John
4         3s  3.400s       right
5     3.400s  3.500s         now
6     3.500s  3.800s           I
7     3.800s  4.300s        help

Time           word
1.900s         hey
2.000s         hey
2.100s         hey
2.200s         hey
2.300s         I'm
2.400s         I'm
2.500s         I'm
2.600s         I'm
2.700s         I'm
2.800s         John
2.900s         John
3.000s         right
3.100s         right
3.200s         right
3.300s         right

使用tidyr::expand可以实现一种解决方案。

编辑:根据 OP 的反馈,因为他的数据有重复的startTime

library(tidyverse)
step = 0.1
df %>% group_by(rnum = row_number()) %>%
  expand(Time = seq(startTime, max(startTime, (endTime-step)), by=step), word = word) %>%
  arrange(Time) %>% 
  ungroup() %>%
  select(-rnum)

# # A tibble: 24 x 2
# # Groups: word [7]
#    Time word 
#   <dbl> <chr>
# 1  1.90 hey  
# 2  2.00 hey  
# 3  2.10 hey  
# 4  2.20 hey  
# 5  2.30 I'm  
# 6  2.40 I'm  
# 7  2.50 I'm  
# 8  2.60 I'm  
# 9  2.70 I'm  
# 10  2.80 John
# ... with 14 more rows

数据

df <- read.table(text = 
"startTime endTime           word
     1.900  2.300         hey
     2.300  2.800         I'm
     2.800      3        John
     3      3.400       right
     3.400  3.500         now
     3.500  3.800           I
     3.800  4.300        help",
header = TRUE, stringsAsFactors = FALSE)

dcast()用于将数据从长格式改造成宽格式(从而聚合),而 OP 想要从宽格式改成长格式,从而填充丢失的时间戳。

有一种使用non-equi join的替代方法。

准备数据

但是,在我们继续之前,需要将startTimeendTime在删除尾随的"s"后转换为数字变量。

library(data.table)
cols <- stringr::str_subset(names(DF), "Time$")
setDT(DF)[, (cols) := lapply(.SD, function(x) as.numeric(stringr::str_replace(x, "s", ""))), 
          .SDcols = cols]

非平等加入

创建涵盖整个时间段的时间戳序列并将其正确连接到数据集,但仅保留那些落在给定时间间隔内的时间戳。 从接受的答案来看,似乎endTime不得包含在结果中。 因此,必须相应地调整连接条件。

DF[DF[, CJ(time = seq(min(startTime), max(endTime), 0.1))], 
   on = .(startTime <= time, endTime > time), nomatch = 0L][
     , endTime := NULL][]   # a bit of clean-up
 startTime word 1: 1.9 hey 2: 2.0 hey 3: 2.1 hey 4: 2.2 hey 5: 2.3 I'm 6: 2.4 I'm 7: 2.5 I'm 8: 2.6 I'm 9: 2.7 I'm 10: 2.8 John 11: 2.9 John 12: 3.0 right 13: 3.1 right 14: 3.2 right 15: 3.3 right 16: 3.4 now 17: 3.5 I 18: 3.6 I 19: 3.7 I 20: 3.8 help 21: 3.9 help 22: 4.0 help 23: 4.1 help 24: 4.2 help startTime word

请注意,此方法不需要引入行号。

nomatch = 0L在对话中出现间隙时避免 NA 行。

数据

library(data.table)
DF <- fread("
rn startTime endTime           word
1     1.900s  2.300s         hey
2     2.300s  2.800s         I'm
3     2.800s      3s        John
4         3s  3.400s       right
5     3.400s  3.500s         now
6     3.500s  3.800s           I
7     3.800s  4.300s        help
", drop = 1L)

暂无
暂无

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

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