我正在尝试对输入文件的内容进行一些更改。输入文件我看起来像下面这样:更改应用不同条件的文件的内容
18800000 20400000 pau
20400000 21300000 aa
21300000 22500000 p
22500000 23200000 l
23200000 24000000 ay
24000000 25000000 k
25000000 26500000 pau
此文件是一个音频文件的转录。第一个数字表示开始时间,下一个数字表示结束时间。然后字母表示声音。
我必须做的改变是,有一些声音是由两种不同的声音组成的,即也有一些双元音。所以这些双元音必须分成两个声音。在上面的例子中,双元音是'ay'。它由'ao'和'ih'组成。 这里发生的是,'ay'的持续时间是24000000 - 232000000 = 8被分配到这两个声音中。其结果将是,
23200000 24000000 ay
变化
23200000 236000000 ao
23600000 240000000 ih
我试图写一个伪代码看起来垃圾。
def test(transcriptionFile) :
with open("transcriptions.txt", "r+") as tFile :
for line in tFile :
if 3rd_item = ay
duration = (2nd_item[1] - 1st_item[2])/2
delete the line
tFile.write(1st_item, 1st_item + d, ao)
tfile.write(1st_item + d, 1st_item, ih) # next line
if__name__ == "__main__" :
test("transcriptions.txt")
谢谢。
随着我给出的建议,我将代码更改为以下内容。它仍然不正确。
def test(transcriptionFile) :
with open("transcriptions.txt", "r") as tFile :
inp = tFile.readlines()
outp = []
for ln in inp :
start, end, sound = ln.strip()
if sound == ay :
duration = (end - start)/2
ln.delete
start = start
end = start + duration
sound = ao
outp.append(ln)
start = start + duration # next line
end = start
sound = ih
outp.append(ln)
with open("transcriptions.txt", "w") as tFile:
tFile.writelines(outp)
__name__ == "__main__"
test("transcriptions.txt")
一旦你得到你的代码运行,你需要测试,如果__name__ ==“__main__”,只有执行测试,如果这是真的 –