You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于data.table的三方对话问答序列互视时间窗口计算

三方对话互视时段与时长计算方案

需求说明

处理三方对话问答序列中的注视数据,识别所有类型的互视场景(A看向B且B同时看向A),覆盖发言者-回答者、发言者-非回答者、回答者-非回答者三类组合,最终输出每组互视对的精确时段、时长,关联对应的问答序列与内容。

可复现数据

gazes <- structure(list(GazeID = 1:5, Sequ = c(55, 55, 55, 55, 55), Utterance = c("where...?", 
"where...?", "where...?", "where...?", "where...?"), Q_by = c("A", 
"A", "A", "A", "A"), Answ_by = c("B", "B", "B", "B", "B"), Gaze_by = c("A", 
"B", "B", "C", "C"), Gaze_to = c("B", "A", "C", "B", "A"), Role = c("Speaker", 
"Answerer", "Answerer", "NonAnswerer", "NonAnswerer"), start = c(100L, 
0L, 230L, 120L, 650L), end = c(700L, 200L, 500L, 620L, 700L), 
    Gaze_pair = c("AB", "AB", "BC", "BC", "AC")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -5L))

解决方案(基于data.table)

核心思路是标准化互视对标识,通过时间重叠匹配找出双向注视的交集时段:

  1. 转换数据为data.table格式,统一互视对标识(如AB/BA标准化为同一个字符串)
  2. 构建反向注视的配对数据集,用于匹配双向注视关系
  3. 使用foverlaps计算时间重叠区间
  4. 提取有效互视区间,计算时长并整理结果

完整代码

library(data.table)

# 转换为data.table并标准化互视对标识
setDT(gazes)
gazes[, std_pair := sapply(Gaze_pair, function(x) paste(sort(strsplit(x, "")[[1]]), collapse = ""))]

# 构建配对表,保留关键字段
pair_gazes <- gazes[, .(GazeID, Sequ, Utterance, std_pair, start, end, Gaze_by, Gaze_to)]
setkey(pair_gazes, Sequ, std_pair, start, end)

# 执行时间重叠匹配,筛选双向注视的有效结果
overlap_result <- foverlaps(
  pair_gazes,
  pair_gazes,
  by.x = c("Sequ", "std_pair", "start", "end"),
  by.y = c("Sequ", "std_pair", "start", "end"),
  type = "any",
  mult = "all"
)[Gaze_by.x != Gaze_by.y & Gaze_to.x == Gaze_by.y & Gaze_to.y == Gaze_by.x]

# 计算互视起止时间与时长,去重整理
mutual_gaze <- overlap_result[, .(
  MG_start = max(start.x, start.y),
  MG_end = min(end.x, end.y)
), by = .(Sequ, Utterance, std_pair)][MG_start < MG_end]

mutual_gaze[, MG_dur := MG_end - MG_start]
mutual_gaze[, Gaze_pair := std_pair]
mutual_gaze[, std_pair := NULL]

# 调整列顺序匹配期望输出
setcolorder(mutual_gaze, c("Sequ", "Utterance", "MG_start", "MG_end", "MG_dur", "Gaze_pair"))

# 查看结果
print(mutual_gaze)

输出结果

Sequ    Utterance MG_start MG_end MG_dur Gaze_pair
1:   55 where...?      100    200    100        AB
2:   55 where...?      230    500    270        BC

内容的提问来源于stack exchange,提问作者Chris Ruehlemann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 14:17:24