【问题标题】:String Segmentation字符串分割
【发布时间】:2020-03-07 22:15:01
【问题描述】:

已解决

我有一个字符串,其中包含两个人之间的对话以及他们的说话者标签。

我想将字符串拆分为两个子字符串,其中仅包含扬声器 1 和扬声器 2 的对话。

这是我用来获取成绩单的代码。

operation = client.long_running_recognize(config, audio)
response = operation.result(timeout=10000)
result = response.results[-1]
words_info = result.alternatives[0].words
transcript = ''
tag=1
speaker=""
for word_info in words_info:
    if word_info.speaker_tag==tag:
        speaker=speaker+" "+word_info.word
    else:
        transcript += "speaker {}: {}".format(tag,speaker) + '\n'
        tag=word_info.speaker_tag
        speaker=""+word_info.word
transcript += "speaker {}: {}".format(tag,speaker)

这会将扬声器 1 和扬声器 2 转录到同一个文件中。

已解决:解决方案要简单得多。感谢您的帮助。

transcript_1 = ''
transcript_2 = ''

for word_info in words_info:
    if word_info.speaker_tag==1:
        #speaker += " "+word_info.word
        transcript_1 += " " + word_info.word
    elif word_info.speaker_tag==2:
        #speaker += " "+word_info.word
        transcript_2 += " " + word_info.word

【问题讨论】:

  • 到目前为止您尝试过什么?你能告诉我们一些你的代码吗?还有你输入数据的样本?
  • 您的问题非常不清楚:(“当前结果”和“预期结果”都没有多大意义。如前所述,要给出更好的答案,这确实需要一些示例输入数据。尝试拆分执行 TTS 的代码和拆分数据的代码。这样您提供示例数据会更容易。
  • 非常感谢 Chefhose 和 exhuma。解决方案比我简单得多,并且在转录原文时没有考虑过。

标签: python string python-3.7 text-segmentation


【解决方案1】:

取决于你如何获取数据,我的意思是,如果你得到一个唯一的原始字符串,其中包含来自两个发言者的所有消息,或者你分别从每个发言者那里获取消息。

一种基本方法是建立字符串“speaker X:”(其中 N 是扬声器编号)作为第一个扬声器的扬声器标签,然后您可以使用 NLTK 和/或内置工具从每个扬声器中提取每条消息-in 函数,如 find()。

注意:当我谈论标签时,我指的是一些表达式,可以让我们确定消息是否来自某个说话者。

示例: 你会得到包含演讲者所有干预的整个文本。

  • 要遵循的步骤:

1) 设置所有发言者标签以区分他们在整个文本中的干预。 示例:第一个扬声器的扬声器标签可以是“扬声器 1:”

2) 使用 str.find("speaker_tag") 查找说话者的所有干预

3) 将每个说话者的所有干预添加到不同的数据结构中。 我认为演讲者的干预列表可能很有用,然后如果您想再次在一条短信中获得所有这些干预,您可以使用 一些内置函数,如 str.join() 将它们再次连接成一个字符串。

解决这个问题的其他选择是使用像 NLTK 这样的工具(我认为这个工具非常适合对文本进行分类)

它具有非常有用的功能,例如标记化,我认为这对解决您的问题很有用。

在下面的示例中,我将使用 find() 和切片作为文本标记化的基本示例:

文本数据:

text = "speaker 1: hello everyone, I am Thomas speaker 2: Hello friends, I am John speaker 1: How are you? I am great being here speaker 2: It's the same for me"

代码示例:

from itertools import islice, tee

FIRST_SPEAKER_TAG = "speaker 1:"
SECOND_SPEAKER_TAG = "speaker 2:"

def get_speaker_positions(text, speaker_tag):

    total_interventions = text.count(speaker_tag)
    positions = []
    position = 0
    for i in range(total_interventions):
        positions.append(text.find(speaker_tag, position))
        # we increase the position by the addition of all the previous 
        # positions to reach the following occurrences through the list of 
        # positions
    position += sum(positions) + 1

    return positions

def slices(iterable, n):
    return zip(*(islice(it, i, None) for i, it in enumerate(tee(iterable, n))))

def get_text_interventions(text, speaker_tags):

    # speakers' interventions of the text
    interventions = { speaker_tag: "" for speaker_tag in speaker_tags }

    # positions where start each intervention in the text
    # (the last one is used to get the rest of the text, because it's the 
    # last intervention)
    # (we need to sort the positions to get the interventions in the correct 
    # order)
    speaker_positions = [
        get_speaker_positions(text, speaker) for speaker in speaker_tags
    ]
    all_positions = [
        position for sublist in speaker_positions for position in sublist
    ]
    all_positions.append(len(text))
    all_positions.sort()

    # generate the list of pairs that match a certain intervention
    # the pairs are formed by the initial and the end position of the 
    # intervention
    text_chunks = list(slices(all_positions, 2))

    for chunk in text_chunks:

        # we assign the intervention according to which 
        # list of speaker interventions the position exists
        # when slicing we add the speaker tag's length to exclude 
        # the speaker tag from the own intervention
        if chunk[0] in speaker_positions[0]:
            intervention = text[chunk[0]+len(speaker_tags[0]):chunk[1]]
            interventions[speaker_tags[0]] += intervention

        elif chunk[0] in speaker_positions[1]:
            intervention = text[chunk[0]+len(speaker_tags[1]):chunk[1]]
            interventions[speaker_tags[1]] += intervention

    return interventions

text_interventions = get_text_interventions(text, [ FIRST_SPEAKER_TAG, SECOND_SPEAKER_TAG ])

注意事项:

如果您有任何疑问,可以阅读 itertools 文档中的更多详细信息:

  • 有关 itertools.islice 和 itertools.tee 的文档: islice tee

如果您对该示例有任何不明白的地方,请随时问我。 希望对你有帮助! =)

【讨论】:

  • 我对 python 很陌生,所以请耐心等待。函数的返回是 first_speaker_interventions, second_speaker_interventions 对吗?如果我是对的,find_speaker_position 也需要是 first_speaker_position 吗? text = text[first_speaker_position:] 你能在 if 语句之后解释一下吗?我尝试使用您提供的示例文本运行,但它没有加载
  • @YasirAhmedPirkani 关于拼写错误的问题,我已经解决了。关于return的问题,是一个修饰符的方法,所以目的只是修改文字,不是return的干预。给我一些时间来复习一下,给你一个更解释的例子。
  • @YasirAhmedPirkani 你好 Yasir,很高兴你解决了这个问题,在这里我告诉你我的方法,以防你想查看它 =)
猜你喜欢
  • 2011-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多