使用正则表达式格式化字幕中的文本时出现问题
Problem formatting text from captions using regular expressions
我正在尝试获取字幕文本以对其进行分析,但我无法以可读的方式获取字幕文本。我正在使用正则表达式来获取字幕编号、字幕时间和字幕语音。当谈到演讲时,我得到了很多空白行,因为字幕的设置就像图像一样。所以我只想创建一个只包含语音而不包含空行的列表。我得到的列表也在图像中。
这里也有一个来自字幕的示例:
1
00:00:00,030 --> 00:00:05,370
so here we are at the offices of my
2
00:00:02,240 --> 00:00:05,370
3
00:00:02,250 --> 00:00:07,319
accountants of your Eric Biddle mr.
4
00:00:05,360 --> 00:00:07,319
5
MY LIST
CAPTIONS:
import re
filename = r'test_subtitle.srt'
pattern_number = re.compile('^\d+$')
pattern_time = re.compile('^[\d]+:[\d]+:[\d]+,[\d]+ --> [\d]+:[\d]+:[\d]+,[\d]+$')
pattern_speech = re.compile("^[A-Za-z,;'\"\s]+[.?!]*$")
for i, line in enumerate(open(filename)):
for match in re.findall(pattern_number, line):
print(match)
for i, line in enumerate(open(filename)):
for match in re.findall(pattern_time, line):
print(match)
speech = []
for i, line in enumerate(open(filename)):
for match in re.findall(pattern_speech, line):
speech.append(match)
print(speech)
我建议您将文本作为一个整体而不是单独的行来浏览。您还可以在模式中使用组来捕获和包含数据。我会按如下方式读取数据:
with open('test_subtitle.srt', 'r') as f:
subtitles = f.read()
然后使用以下代码我将匹配单个部分并提取数据:
import re
num_pat = r'(\d+)'
time_pat = r'(\d{2,}:\d{2}:\d{2},\d{3}) --> (\d{2,}:\d{2}:\d{2},\d{3})'
sentence_pat = r'([^\n]*)\n'
data_pattern = re.compile(r'\n'.join([num_pat, time_pat, sentence_pat]))
print('data_pattern:', data_pattern)
for i in re.finditer(data_pattern, subtitles):
print('-'*20)
print(i.group(1))
print(f'time: {i.group(2)} --> {i.group(3)}')
print('text:', repr(i.group(4)))
print()
我在您的代码中还注意到的一个问题是,在定义模式时,您使用的是普通字符串而不是原始字符串,并且您没有转义反斜杠。如果你想在不转义的情况下使用反斜杠,你应该使用原始字符串。希望这对您有所帮助。
我正在尝试获取字幕文本以对其进行分析,但我无法以可读的方式获取字幕文本。我正在使用正则表达式来获取字幕编号、字幕时间和字幕语音。当谈到演讲时,我得到了很多空白行,因为字幕的设置就像图像一样。所以我只想创建一个只包含语音而不包含空行的列表。我得到的列表也在图像中。
这里也有一个来自字幕的示例:
1
00:00:00,030 --> 00:00:05,370
so here we are at the offices of my
2
00:00:02,240 --> 00:00:05,370
3
00:00:02,250 --> 00:00:07,319
accountants of your Eric Biddle mr.
4
00:00:05,360 --> 00:00:07,319
5
MY LIST
CAPTIONS:
import re
filename = r'test_subtitle.srt'
pattern_number = re.compile('^\d+$')
pattern_time = re.compile('^[\d]+:[\d]+:[\d]+,[\d]+ --> [\d]+:[\d]+:[\d]+,[\d]+$')
pattern_speech = re.compile("^[A-Za-z,;'\"\s]+[.?!]*$")
for i, line in enumerate(open(filename)):
for match in re.findall(pattern_number, line):
print(match)
for i, line in enumerate(open(filename)):
for match in re.findall(pattern_time, line):
print(match)
speech = []
for i, line in enumerate(open(filename)):
for match in re.findall(pattern_speech, line):
speech.append(match)
print(speech)
我建议您将文本作为一个整体而不是单独的行来浏览。您还可以在模式中使用组来捕获和包含数据。我会按如下方式读取数据:
with open('test_subtitle.srt', 'r') as f:
subtitles = f.read()
然后使用以下代码我将匹配单个部分并提取数据:
import re
num_pat = r'(\d+)'
time_pat = r'(\d{2,}:\d{2}:\d{2},\d{3}) --> (\d{2,}:\d{2}:\d{2},\d{3})'
sentence_pat = r'([^\n]*)\n'
data_pattern = re.compile(r'\n'.join([num_pat, time_pat, sentence_pat]))
print('data_pattern:', data_pattern)
for i in re.finditer(data_pattern, subtitles):
print('-'*20)
print(i.group(1))
print(f'time: {i.group(2)} --> {i.group(3)}')
print('text:', repr(i.group(4)))
print()
我在您的代码中还注意到的一个问题是,在定义模式时,您使用的是普通字符串而不是原始字符串,并且您没有转义反斜杠。如果你想在不转义的情况下使用反斜杠,你应该使用原始字符串。希望这对您有所帮助。