File size: 1,343 Bytes
b2f76ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os

METADAT = {

}
CSV = "file_name,country,region,gender,age,date,text"
for root, folders, files in os.walk(os.path.dirname(__file__)):
    if files:
        r = root.split(os.path.dirname(__file__) + "/")[-1]

        country = r.split("/")[0]
        wavs = [f for f in files if f.endswith(".wav")]
        txt = [f for f in files if f.endswith(".txt")]

        for idx, t in enumerate(txt):
            wav = f"{r}/" + t.replace(".txt", ".wav")
            #print(wav)
            METADAT[wav] = {"country": country}
            #print(t)
            with open(f"{root}/{t}") as f:
                lines = f.read().split("\n")
            keys = [l for l in lines if ":" in l]
            txt[idx] = ". ".join([l.lstrip("-> ") for l in lines if l.startswith("-")]).replace('"',"'")
            for k in keys:
                k, v = k.split(":", 1)
                k = k.lower().strip()
                v = v.strip()
                if v:
                    METADAT[wav][k] = v
            CSV += f"\n{wav},{country},{METADAT[wav].get('local', '').split('-',1)[-1].replace(',', ' -').strip()},{METADAT[wav].get('sexo', '')},{METADAT[wav].get('idade', '').split('anos')[0].strip()},{METADAT[wav].get('data', '')},\"{txt[idx]}\""

        #print(txt)
with open(f"{os.path.dirname(__file__)}/metadata.csv", "w") as f:
    f.write(CSV)