【问题标题】:Converting a dialogue tibble to .txt, and back again将对话小标题转换为 .txt,然后再返回
【发布时间】:2020-04-08 00:17:30
【问题描述】:

我想取一个代表对话的tibble,把它变成一个.txt,可以在文本编辑器中手动编辑,然后返回一个tibble进行处理。

我遇到的主要挑战是分离文本块,以便在编辑后可以将它们重新导入为类似的格式,同时保留“发言人”的名称。

速度很重要,因为文件的数量和每个文本段的长度都很大。

这是输入小标题:

tibble::tribble(
    ~word, ~speakerTag,
   "been",          1L,
  "going",          1L,
     "on",          1L,
    "and",          1L,
   "what",          1L,
   "your",          1L,
  "goals",          1L,
   "are.",          1L,
  "Yeah,",          2L,
     "so",          2L,
     "so",          2L,
   "John",          2L,
    "has",          2L,
     "15",          2L
  )

这是 .txt 中所需的输出:

###Speaker 1###
been going on and what your goals are.
###Speaker 2###
Yeah, so so John has 15

这是手动更正错误后所需的回报:

    ~word, ~speakerTag,
   "been",          1L,
  "going",          1L,
     "on",          1L,
    "and",          1L,
   "what",          1L,
   "your",          1L,
  "goals",          1L,
   "in",            1L,
   "r",             1L,
  "Yeah,",          2L,
     "so",          2L,
     "so",          2L,
   "John",          2L,
    "hates",        2L,
     "50",          2L
  )

【问题讨论】:

    标签: r dplyr purrr stringr google-language-api


    【解决方案1】:

    一种方法是在每个speakerTag 的开头添加演讲者姓名"\n"

    library(data.table)
    library(dplyr)
    library(tidyr)
    
    setDT(df)[, word := replace(word, 1, paste0("\n\nSpeaker", 
                first(speakerTag), '\n\n', first(word))), rleid(speakerTag)]
    

    我们可以在文本文件中使用

    writeLines(paste(df$word, collapse = " "), 'Downloads/temp.txt')
    

    看起来像这样:

    cat(paste(df$word, collapse = " "))
    
    #Speaker1
    #
    #been going on and what your goals are. 
    #
    #Speaker2
    #
    #Yeah, so so John has 15
    

    要在 R 中读回它,我们可以这样做:

    read.table('Downloads/temp.txt', sep="\t", col.names = 'word') %>%
        mutate(SpeakerTag = replace(word, c(FALSE, TRUE), NA)) %>%
        fill(SpeakerTag) %>%
        slice(seq(2, n(), 2)) %>%
        separate_rows(word, sep = "\\s") %>%
        filter(word != '')
    
    #    word SpeakerTag
    #1   been   Speaker1
    #2  going   Speaker1
    #3     on   Speaker1
    #4    and   Speaker1
    #5   what   Speaker1
    #6   your   Speaker1
    #7  goals   Speaker1
    #8   are.   Speaker1
    #9  Yeah,   Speaker2
    #10    so   Speaker2
    #11    so   Speaker2
    #12  John   Speaker2
    #13   has   Speaker2
    #14    15   Speaker2
    

    显然,如果不需要,我们可以删除SpeakerTag 列中的"Speaker" 部分。

    【讨论】:

    • 感谢您抽出宝贵时间发布答案。我的问题的一个关键部分是需要区分输出 .txt 上的说话者(用于编辑成绩单),然后是重新上传的过程。您的建议使我朝着这个解决方案前进,但我仍在努力解决如何输出像我提到的那样的 .txt,然后将其读回以进行分析。在整个对话过程中,说话者 1 和说话者 2 之间确实会有多次交流(来回切换)。
    • 我不确定我是否跟随。为什么你需要再次阅读文本?如果您使用data.table 回答df,您最后得到的是您想要的吗?你可以用它来写文本文件吗?
    • 数据来自从 Google Speech 到 text 的自动音频转录。转录会出错。我想要一个 .txt 用于手动审查文件以纠正错误,然后我想要一个 tibble 进行分析。所以工作流程是,google->R->manual edit->R.
    • 在这条链google->R->manual edit->R 中,如果您的manual edit 步骤中的所有内容都正确,您将返回上一个R 步骤,因为它是相同的数据。您无需再次将数据从 txt 读取到 R 中。虽然,如果您在文本文件中进行一些手动更改,您需要在 R 中读回它,我明白您的意思并打算这样做。
    • 这是更接近所需输出的一步: setDT(df)[, word := replace(word, .N, paste0(last(word), "\n\n")), rleid (speakerTag)] setDT(df)[, word := replace(word, 1,paste0("Speaker",first(speakerTag),"\n\n",first(word))), rleid(speakerTag)] cat (粘贴(df$word, collapse = " "))
    猜你喜欢
    • 2011-04-24
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    • 2011-06-20
    • 1970-01-01
    • 2013-11-15
    • 2017-01-02
    • 2011-02-15
    相关资源
    最近更新 更多