1

在这些人的帮助下,我能够生成以下代码,它读取两个文件(即 SA1.WRD 和 SA1.PHN),将它们合并,并将结果与​​删除的单词子列表进行比较一本字典:

导入 sys 导入 o​​s 导入重新导入 itertools

#generator function to merge sound and word files
def takeuntil(iterable, stop):
    for x in iterable:
        yield x
        if x[1] == stop:
            break

#open a dictionary file and create subset of words
class_defintion = re.compile('([1-2] [lnr] t en|[1-2] t en)')
with open('TIMITDIC.TXT') as w_list:
    entries = (line.split(' ', 1) for line in w_list)
    comp_set = [ x[0] for x in entries if class_defintion.search(x[1]) ]

#open word and sound files
total_words = 0
with open(sys.argv[1]) as unsplit_words, open(sys.argv[2]) as unsplit_sounds:
    sounds = (line.split() for line in unsplit_sounds)
    words = (line.split() for line in unsplit_words)
    output = [
    (word, " ".join(sound for _, _, sound in
        takeuntil(sounds, stop)))
    for start, stop, word in words
]
for x in output:
    total_words += 1

#extract words from above into list of words in dictionary set
glottal_environments = [ x for x in output if x[0] in comp_set ]

我正在尝试修改该部分 #open a dictionary files以在具有多个子目录的大目录上运行。每个子目录都包含 .txt 文件、.wav 文件、.wrd 和 .phn 文件。我只想打开 .wrd 和 .phn 文件,并且我希望能够一次打开它们两个,并且只有当基本文件名匹配时,即 SA1.WRD 和 SA1.PHN,而不是 SA1。 WRD 和 SI997.PHN。

我的直接猜测是做这样的事情:

for root, dir, files in os.walk(sys.argv[1]):
    words = [f for f in files if f.endswith('.WRD')]
    phones = [f for f in files if f.endswith('.PHN')]
    phones.sort()
    words.sort()
    files = zip(words, phones)

返回:[('SA1.WRD', 'SA1.PHN'), ('SA2.WRD', 'SA2.PHN'), ('SI997.WRD', 'SI997.PHN')]

我的第一个问题是我是否走在正确的轨道上,如果是这样,我的第二个问题是如何将这些元组中的每一项视为要读取的文件名。

感谢您提供的任何帮助。

编辑

我想我可以将代码块放入 for 循环中:

for f in files:
        #OPEN THE WORD AND PHONE FILES, COMAPRE THEM (TAKE A WORD COUNT)
        total_words = 0
        with open(f[0]) as unsplit_words, open(f[1]) as unsplit_sounds:

        ...

但是,这会导致 IOError,可能是由于每个元组中每个项目的单引号引起的。

更新 我修改了我的原始脚本以包含os.path.join(root, f)如下所述。该脚本现在遍历目录树中的所有文件,但它只处理它找到的最后两个文件。这是输出print files

[]
[('test/test1/SI997.WRD', 'test/test1/SI997.PHN')]
[('test/test2/SI997.WRD', 'test/test2/SI997.PHN')]
4

1 回答 1

1

我已经测试了与文件系统相关的不同部分,但您更容易确认实际文件以确认它适用于您的数据。

编辑以允许包含路径名

import sys
import os
import os.path
import re
import itertools

#generator function to merge sound and word files
def takeuntil(iterable, stop):
    for x in iterable:
        yield x
        if x[1] == stop:
            break

def process_words_and_sounds(word_file, sound_file):
    #open word and sound files
    total_words = 0
    with open(word_file) as unsplit_words, open(sound_file) as unsplit_sounds:
        sounds = (line.split() for line in unsplit_sounds)
        words = (line.split() for line in unsplit_words)
        output = [
            (word, " ".join(sound for _, _, sound in
                            takeuntil(sounds, stop)))
            for start, stop, word in words
            ]
        for x in output:
            total_words += 1
    return total_words, output

for root, dir, files in os.walk(sys.argv[1]):
    words = [ os.path.join( root, f ) for f in files if f.endswith('.WRD')]
    phones = [ os.path.join( root, f ) for f in files if f.endswith('.PHN')]
    phones.sort()
    words.sort()
    files = zip(words, phones)
    # print files

output = []
total_words = 0
for word_sounds in files:
    word_file, sound_file = word_sounds
    word_count, output_subset = process_words_and_sounds(word_file, sound_file)
    total_words += word_count
    output.extend( output_subset )

#open a dictionary file and create subset of words
class_defintion = re.compile('([1-2] [lnr] t en|[1-2] t en)')
with open('TIMITDIC.TXT') as w_list:
    entries = (line.split(' ', 1) for line in w_list)
    comp_set = [ x[0] for x in entries if class_defintion.search(x[1]) ]

#extract words from above into list of words in dictionary set
glottal_environments = [ x for x in output if x[0] in comp_set ]
于 2012-05-23T05:00:17.740 回答