def test_to_praat_ends_after(self): mock_tier = mock.Mock() mock_tier.xmin = 0 mock_tier.xmax = 1 tg = TextGrid(0, 0.5, [mock_tier]) with assert_raises(ValueError): tg.to_praat()
def test_to_praat_starts_before(self): mock_tier = mock.Mock() mock_tier.xmin = 0 mock_tier.xmax = 1 tg = TextGrid(0.5, 1, [mock_tier]) with assert_raises(ValueError): tg.to_praat()
def test_to_dict(self): mock_tier = mock.Mock() mock_tier.to_dict.return_value = {'a tier': 'data'} tg = TextGrid(0, 1, [mock_tier]) res = tg.to_dict() assert_equal(res, {'xmin': 0, 'xmax': 1, 'tiers': [{'a tier': 'data'}]})
def test_to_praat(self): mock_tier = mock.Mock() mock_tier.xmin = 0 mock_tier.xmax = 1 mock_tier.to_praat.return_value = 'a tier' tg = TextGrid(0, 1, [mock_tier, mock_tier]) res = tg.to_praat() assert_equal(res, ('"ooTextFile"\n"TextGrid"\n' '0 to 1 seconds <exists>\n' '2 tiers\n\na tier\n\na tier'))
def test_to_dict(self): mock_tier = mock.Mock() mock_tier.to_dict.return_value = {'a tier': 'data'} tg = TextGrid(0, 1, [mock_tier]) res = tg.to_dict() assert_equal(res, { 'xmin': 0, 'xmax': 1, 'tiers': [{ 'a tier': 'data' }] })
def test_to_praat_with_path(self): mock_tier = mock.Mock() mock_tier.xmin = 0 mock_tier.xmax = 1 mock_tier.to_praat.return_value = 'a tier' tg = TextGrid(0, 1, [mock_tier, mock_tier]) tg.to_praat('test/files/output.TextGrid') with open('test/files/output.TextGrid') as output_file: res = output_file.read() assert_equal(res, ('"ooTextFile"\n"TextGrid"\n' '0 to 1 seconds <exists>\n' '2 tiers\n\na tier\n\na tier'))
def test_to_praat_with_path(self): mock_tier = mock.Mock() mock_tier.xmin = 0 mock_tier.xmax = 1 mock_tier.to_praat.return_value = 'a tier' tg = TextGrid(0, 1, [mock_tier, mock_tier]) tg.to_praat('tests/files/output.TextGrid') with open('tests/files/output.TextGrid') as output_file: res = output_file.read() assert_equal(res, ('"ooTextFile"\n"TextGrid"\n' '0 to 1 seconds <exists>\n' '2 tiers\n\na tier\n\na tier'))
def test_from_file(self): tg_file = 'tests/files/doubled-quotes-in-text-and-mark.TextGrid' tg = TextGrid.from_file(tg_file) assert_equal(tg.xmin, 0.25) assert_equal(tg.xmax, 1.5) assert_equal(len(tg.tiers), 2) assert_is(tg.tiers[0].__class__, IntervalTier) assert_equal(tg.tiers[0].name, 'words') assert_equal(tg.tiers[0].xmin, 0.1) assert_equal(tg.tiers[0].xmax, 1) assert_equal(len(tg.tiers[0]._items), 2) assert_equal(tg.tiers[0]._items[0].xmin, 0.1) assert_equal(tg.tiers[0]._items[0].xmax, 0.5) assert_equal(tg.tiers[0]._items[0].text, '"Is anyone home?"') assert_equal(tg.tiers[0]._items[1].xmin, 0.5) assert_equal(tg.tiers[0]._items[1].xmax, 1) assert_equal(tg.tiers[0]._items[1].text, 'asked "Pat"') assert_is(tg.tiers[1].__class__, TextTier) assert_equal(tg.tiers[1].name, 'points') assert_equal(tg.tiers[1].xmin, 0.1) assert_equal(tg.tiers[1].xmax, 1) assert_equal(len(tg.tiers[1]._items), 2) assert_equal(tg.tiers[1]._items[0].number, 0.25) assert_equal(tg.tiers[1]._items[0].mark, '"event"') assert_equal(tg.tiers[1]._items[1].number, 0.75) assert_equal(tg.tiers[1]._items[1].mark, '"event" with quotes again')
def test_from_file_armenian(self): tg = TextGrid.from_file('tests/files/numbers.TextGrid', encoding='utf_16') assert_equal(tg.tiers[0][0].text, u'մեկ') assert_equal(tg.tiers[0][1].text, u'երկու') assert_equal(tg.tiers[0][2].text, u'երեք') assert_equal(tg.tiers[0][3].text, u'չորս')
def test_from_file(self): tg_file = 'test/files/doubled-quotes-in-text-and-mark.TextGrid' tg = TextGrid.from_file(tg_file) assert_equal(tg.xmin, 0.25) assert_equal(tg.xmax, 1.5) assert_equal(len(tg.tiers), 2) assert_is(tg.tiers[0].__class__, IntervalTier) assert_equal(tg.tiers[0].name, 'words') assert_equal(tg.tiers[0].xmin, 0.1) assert_equal(tg.tiers[0].xmax, 1) assert_equal(len(tg.tiers[0]._items), 2) assert_equal(tg.tiers[0]._items[0].xmin, 0.1) assert_equal(tg.tiers[0]._items[0].xmax, 0.5) assert_equal(tg.tiers[0]._items[0].text, '"Is anyone home?"') assert_equal(tg.tiers[0]._items[1].xmin, 0.5) assert_equal(tg.tiers[0]._items[1].xmax, 1) assert_equal(tg.tiers[0]._items[1].text, 'asked "Pat"') assert_is(tg.tiers[1].__class__, TextTier) assert_equal(tg.tiers[1].name, 'points') assert_equal(tg.tiers[1].xmin, 0.1) assert_equal(tg.tiers[1].xmax, 1) assert_equal(len(tg.tiers[1]._items), 2) assert_equal(tg.tiers[1]._items[0].number, 0.25) assert_equal(tg.tiers[1]._items[0].mark, '"event"') assert_equal(tg.tiers[1]._items[1].number, 0.75) assert_equal(tg.tiers[1]._items[1].mark, '"event" with quotes again')
def main(rootDir, outDir): print("number of phones appeared in mapping:", len(set(phonesTargetMapper.values())), ",", "number of target phones:", len(targetPhones)) phonesLocations = { } # {"ph1": [(fpath1, xmin1, xmax1), (fpath2, xmin2, xmax2), ...], "ph2": [(fpath1, xmin1, xmax1), (fpath2, xmin2, xmax2), ...], ...} for targetPhone in targetPhones: phonesLocations[targetPhone] = [] phonesLocations['others'] = [] for filePath in getFilesPaths(rootDir): textGrid = TextGrid.from_file(filePath) phonesTier = next( filter(lambda interval: interval.name == "phones", textGrid), None) if (not phonesTier): print("the file has no phones tier !!") for interval in phonesTier: actualPhone = interval.text.upper() targetPhone = phonesTargetMapper.get(actualPhone, 'others') relDir = getRelDir(filePath, back=3) phonesLocations[targetPhone].append( (relDir, interval.xmin, interval.xmax)) for targetPhone in targetPhones: np.savetxt(f"{os.path.join(outDir, targetPhone)}.txt", phonesLocations[targetPhone], fmt='%a')
def test_from_file_armenian(self): tg = TextGrid.from_file('test/files/numbers.TextGrid', encoding='utf_16') assert_equal(tg.tiers[0][0].text, u'մեկ') assert_equal(tg.tiers[0][1].text, u'երկու') assert_equal(tg.tiers[0][2].text, u'երեք') assert_equal(tg.tiers[0][3].text, u'չորս')
def test_from_reader(self, TierFromReaderMock): TierFromReaderMock.return_value = 'tier' stream = iter([0, 1, 2]) tg = TextGrid.from_reader(stream) assert_equal(tg.xmin, 0) assert_equal(tg.xmax, 1) assert_equal(tg.tiers, ['tier', 'tier'])
def test_from_file_missing_header(self): with assert_raises(ValueError): TextGrid.from_file('tests/files/intervals-no-filetype.TextGrid') with assert_raises(ValueError): TextGrid.from_file('tests/files/intervals-no-object-class.TextGrid')
def test_utf8_bom(self): # BOM should be skipped by regex tg = TextGrid.from_file('tests/files/intervals-utf8-bom.TextGrid') assert_equal(tg.tiers[1][1].text, 'ciao')
def test_repr(self): tg = TextGrid(0, 1, [1, 2, 3]) assert_equal(repr(tg), "TextGrid(0, 1, [1, 2, 3])")
def test_str(self): tg = TextGrid(0, 1, [1, 2, 3]) assert_equal(str(tg), '<TextGrid from 0 to 1 seconds with 3 tiers>')
def test_get(self): tg = TextGrid(0, 1, ['tier 0', 'tier 1']) assert_equal(tg[1], 'tier 1')
def test_set(self): tg = TextGrid(0, 1, ['tier 0', 'tier 1']) tg[1] = 'new tier' assert_equal(tg[1], 'new tier')
def test_len(self): tg = TextGrid(0, 1, ['tier 0', 'tier 1']) assert_equal(len(tg), 2)
def test_from_reader_wrong_size(self, TierFromReaderMock): TierFromReaderMock.return_value = 'tier' stream = iter([0, 1, 2, 'extra_value']) with assert_raises(ValueError): TextGrid.from_reader(stream)
def test_reversed(self): tg = TextGrid(0, 1, ['tier 0', 'tier 1']) assert_equal(list(reversed(tg)), ['tier 1', 'tier 0'])
def test_from_file_missing_header(self): with assert_raises(ValueError): TextGrid.from_file('test/files/intervals-no-filetype.TextGrid') with assert_raises(ValueError): TextGrid.from_file('test/files/intervals-no-object-class.TextGrid')
def test_utf8_bom(self): # BOM should be skipped by regex tg = TextGrid.from_file('test/files/intervals-utf8-bom.TextGrid') assert_equal(tg.tiers[1][1].text, 'ciao')
def test_del(self): tg = TextGrid(0, 1, ['tier 0', 'tier 1']) del tg[0] assert_equal(tg[0], 'tier 1')
def test_init(self): tg = TextGrid(0, 1, []) assert_equal(tg.xmin, 0) assert_equal(tg.xmax, 1) assert_equal(tg.tiers, [])
def test_iter(self): tg = TextGrid(0, 1, ['tier 0', 'tier 1']) assert_equal(list(iter(tg)), ['tier 0', 'tier 1'])