parseESTERSyncV2.py 16.4 KB
Newer Older
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from xml.etree import ElementTree as ET
from sys import argv
from num2words import num2words
from unidecode import unidecode
import re
import os.path

def transformation_text(text):
    # character normalization:
    text=re.sub("æ","ae",text)
    text=re.sub("œ","oe",text)
    #if "###" in text or len(re.findall(r"\[.+\]", text)) > 0 or \
    #    len(re.findall(r"\p{L}+-[^\p{L}]|\p{L}+-$",text)) > 0 \
    #    or len(re.findall("[^\p{L}]-\p{L}+|^-\p{L}+", text)) > 0:
    #    bool=False
    #else:
    # ^^ remove
    text=re.sub(r"\^+","",text)
    text=re.sub(r"\_+","",text)
    # 4x4
    # Remove noise sound (BIP) over Name of places and person
    #text = re.sub(r"¤[^ ]+|[^ ]+¤|¤", "", text.strip())
    if len(re.findall(r"\dx\d",text))>0:
        text=re.sub(r"x","  ",text)
    if len(re.findall("\d+h\d+",text))>0:
        heures=re.findall("\d+h\d+",text)
        for h in heures:
            split_h=h.split('h')
            text_rep=split_h[0]+' heure '+split_h[1]
            text=text.replace(h, text_rep)
    text=re.sub(r',',' ',text)
# remove silence character : OK
    #text=re.sub(r"(/.+/","remplacer par la 1er",text)
# Liaison non standard remarquable
    text=re.sub(r'=','',text)
# Comment Transcriber
    text=re.sub(r'\{.+\}','',text)
    text=re.sub(r'\(.+\}','',text)
    #print "detecter (///|/|<|>)"
# Remove undecidable variant heared like on (n') en:
    text=re.sub(r"\(.+\)|\(\)","",text)
    #text = re.sub(r"(\+|[*]+|///|/|<|>)", "", text.strip())
    #text=re.sub(r"-|_|\."," ",text.strip())
    text=re.sub(r'(O.K.)','ok',text)
    text = re.sub(r'(O.K)', 'ok', text)
    # Replace . with ''
    text=re.sub(r'\.|,|;','',text)
#text=re.sub(r"{[^{]+}"," ",text.strip())
    # Remove ? ! < > : OK
    #<[^\p{L}]|[^\p{L}]>|#+|<\p{L}+[ ]|<\p{L}+$
    text=re.sub(r":|\?|/|\!|#+","",text)
    text=re.sub(r"%","pour cent",text)
# replace silence character with <sil> : OK
    #text=re.sub(r"(\+)", "<sil>", text)
    #text=re.sub(r"(\+)", "!SIL", text)
    #text=re.sub(r"(///)", "!SIL", text)
    #text=re.sub(r"(///)", "<long-sil>", text)
    #if len(re.findall(r"/.+/", text)) > 0:
        #print "AVANT***********"+text
    #    for unchoosen_text in re.findall(r"/.+/", text):
            # choose first undecideble word
    #        unchoosen_word=unchoosen_text.split(',')
    #        for choosen_word in unchoosen_word:
                # isn't incomprehensible word
    #            if len(re.findall(r"\*+|\d+", choosen_word))==0:
    #                choosen_word = choosen_word.replace('/', '')
    #                text = text.replace(unchoosen_text, choosen_word)
        #print "Apres************"+text
# Remove noise sound (BIP) over Name of places and person
    #text=re.sub(r"(¤.+¤)",'<NOISE>',text)
# replace unkown syllable
    text=re.sub(r"\*+","",text)
# cut of recording : OK
    #text=re.sub(r"\$+","",text)
# remove " character: OK
    text = re.sub(r"\"+", "", text)
    # t 'avais
    text = re.sub(r"[ ]\'", " ", text)
    text = re.sub(r"\'", "\' ", text)
# convert number if exist : OK
    num_list = re.findall("\d+", text)
    if len(num_list) > 0:
        #print text
        #print "********************************* NUM2WORD"
        for num in num_list:
            num_in_word = num2words(int(num), lang='fr')
            #num_in_word=normalize('NFKD', num_in_word).encode('ascii', 'ignore')
            text = re.sub(r"(^|[ ])"+str(num)+"([ ]|$)"," " + str(num_in_word) + " ",text)
        #print text
    # for example : A43
    num_list = re.findall("\w+?-?\d+", text)
    if len(num_list) > 0:
        print(text)
        print(num_list)
        for s in num_list:
            split_between_char_int=re.findall(r'(\w+?)-?(\d+)',s)
            print(split_between_char_int)
            num_in_word = num2words(int(split_between_char_int[0][1]), lang='fr')
            #num_in_word=normalize('NFKD', num_in_word).encode('ascii', 'ignore')
            text = re.sub(r"(^|[ ])"+str(s)+"([ ]|$)"," " + str(split_between_char_int[0][0]) +" "+ str(num_in_word) + " ",text)
# replace n succesive spaces with one space. : OK
    text=re.sub(r"\s{2,}"," ",text)
    text=re.sub(r" $","",text)
    text=re.sub("^ ", '', text)
# change bounding | to < and > : OK
    #balise=set(re.findall(r"\|\w+_?\w+\|",text))
    #if len(balise)>0:
        #print(balise)
    #    for b in balise:
    #        new_balise='<'+b[1:len(b)-1]+'>'
    #        text=text.replace(b,new_balise)
    #print(text)
    # c'est l'essaim ....
    text=text.lower()
    text=re.sub("[ ]-|-$","",text)
    return text
if __name__=="__main__":
    # Inputs
    file_trs=argv[1]
    print(file_trs)
    outdir=argv[2]
    basename=os.path.basename(file_trs.split('.')[0])
    #print file_trs.split('.')[0]
    # Output File needed for kaldi input
    segments_file = open(outdir + '/segments', 'a')
    utt2spk_file = open(outdir + '/utt2spk', 'a')
    text_file = open(outdir + '/text', 'a')
    wav_scp = open(outdir + '/wav.scp', 'a')
    spk2gender= open(outdir + '/spk2gender', 'a')
    topic_file = open(outdir+'/topic','a')
    # Read Trans File
    tree_trs = ET.parse(file_trs)
    trsdoc= tree_trs.getroot()
    #============================ Read MetaData =======================================
    #============================ Topic section (ID,DESC) =======================================
    for topic in trsdoc.iter('Topic'):
        topic_id=unidecode(topic.get('id'))
        topic_desc=unidecode(topic.get('desc'))
        #print(str(basename)+" "+topic_id+" "+topic_desc+"\n")
        topic_file.write(str(basename)+" "+topic_id+" "+topic_desc+"\n")
    #============================ Speaker section (ID,GENDER) ===================================
    speaker_id=[]
    #namespk=[]
    speaker_gender=[]
    for spk in trsdoc.iter('Speaker'):
        id_spk=spk.get('id')
        #name_spk=unidecode(spk.get('name'))
        if spk.findall('type')==[]:
            gender="m"
        else:
            gender=unidecode(spk.get('type'))
            if gender =="female":
                gender="f"
            else:
                gender="m"
        #if isinstance(name_spk,str):
        #print(type(name_spk))
        #name_spk=normalize('NFKD', name_spk).encode('ascii', 'ignore')
        speaker_id.append(id_spk.replace(" ",""))
        speaker_gender.append([id_spk.replace(" ",""),gender.lower()])
        #namespk.append(name_spk.lower().replace(" ",""))
    #============================ Catch Transcription Segment and Topic Section ==================
    text=""
    Turn_count=0
    count=0
    has_attrib_speaker=False
    # set for uniq add
    Spk_that_contribute_to_meeting=set([])
    start_utt=0
    end_utt=0
    #Not used
    section_start_time=0
    section_end_time=0
    section_type=""
    section_topic=""
    nb_section=0
    spkr="spk1"
    for Element in trsdoc.iter():
        #OK validation
        #print("Print lekbirr "+str(Element.tail))
        #print("Print lekbirr "+str(Element.tail))
        if Element.tag=="Section":
            if nb_section>0:
                text = transformation_text(text)
                # File wav.scp
                # File utt2spk
                # File text
                # File speaker_gender
                if text!=""and has_attrib_speaker:
                    Spk_that_contribute_to_meeting.add(spkr)
                    #print("SAVED BY SECTION "+seg_id+" "+text)
                    seg_id = str(basename) + '_%s-%03d_Section%02d_Topic-%s_Turn-%03d_seg-%07d' % (
                    str(re.sub('\d+','',spkr)),int(re.sub('[a-zA-Z]','',spkr)),int(nb_section),str(section_topic), int(Turn_count), int(count))
                    spkr_id = str(basename)+'_%s-%03d' % (str(re.sub('\d+','',spkr)),int(re.sub('[a-zA-Z]','',spkr)))
                    segments_file.write(seg_id+" "+basename+" "+str(start_utt)+" "+str(section_end_time)+"\n")
                    start_utt=section_end_time
                    utt2spk_file.write(seg_id+" "+spkr_id+"\n")
                    text_file.write(seg_id+" "+text+"\n")
                    text=""
            # New section
            section_start_time=Element.get('startTime')
            section_end_time=Element.get('endTime')
            section_type=unidecode(Element.get('type'))
            #if Element.findall('topic')==[]:
            #    section_topic="None"
            #else:
            section_topic=unidecode(str(Element.get('topic')))
            if section_topic=="":
                section_topic="None"
            Turn_count=0
            count=0
            nb_section+=1
        elif Element.tag=="Turn":
            # if the turn is the spoken turn , not musical segment or noise
            #print(str(Element.tag))
            #print(Element.attrib)
            #print(Element.get("speaker"))
            if not "speaker" in Element.attrib:
                #print("pas de champ speaker")
                has_attrib_speaker=False
            else:
                if Element.get('speaker')=="":
                    #print("Cest vide: "+Element.get('speaker'))
                    has_attrib_speaker=False
                else:
                    #print(Element.get('speaker'))
                    # If the latest Utterance of previous Speaker is the latest one of his Turn speech
                    if Turn_count>0:
                        seg_id = str(basename) + '_%s-%03d_Section%02d_Topic-%s_Turn-%03d_seg-%07d' % (
                        str(re.sub('\d+','',spkr)),int(re.sub('[a-zA-Z]','',spkr)),int(nb_section),str(section_topic), int(Turn_count), int(count))
                        spkr_id = str(basename)+'_%s-%03d' % (str(re.sub('\d+','',spkr)),int(re.sub('[a-zA-Z]','',spkr)))
                        text = transformation_text(text)
                        # File wav.scp
                        # File utt2spk
                        # File text
                        # File speaker_gender
                        if bool and text!="":
                            #print("SAVED BY TURN "+seg_id+" "+text)
                            Spk_that_contribute_to_meeting.add(spkr)
                            segments_file.write(seg_id+" "+basename+" "+str(start_utt)+" "+str(endTime)+"\n")
                            start_utt=endTime
                            utt2spk_file.write(seg_id+" "+spkr_id+"\n")
                            text_file.write(seg_id+" "+text+"\n")
                            text=""
                        count = 0
                    # Get id_spkr
                    #print(Element.get('speaker'))
                    spkr=Element.get('speaker')
                    #print file_trs
                    has_attrib_speaker=True
                    spkr=spkr.split()[0]
                    #print spkr
                    # Get StartSegment
                    startTime = Element.get('startTime')
                    # Get EndSegment
                    endTime = Element.get('endTime')
                    # count sync for computing start and end utterance
                    Turn_count = Turn_count+1
        elif has_attrib_speaker:
            #print("Je rentre dans has_attrib_speaker et element.tail not null")
            #print(str(Element.tag))
            #print(str(Element.tail))
            if Element.tag=="Sync" or Element.tag=="Background":
                #print("Je rentre Sync+Background"+ text +"| et le next c'est "+ Element.tail)
                #print(Element.tag+" "+Element.tail)
                Time_start_current_sync=Element.get('time')
                #if count>0:
                    #print("save after Turn")
                    #print(str(basename))
                    #print(str(section_topic))
                    #print(str(spkr))
                    #print(str(int(Turn_count)))
                    #print(str(int(count)))
                    #print text
                    ### Save Files For Kaldi ###
                seg_id = str(basename) + '_%s-%03d_Section%02d_Topic-%s_Turn-%03d_seg-%07d' % (
                str(re.sub('\d+','',spkr)),int(re.sub('[a-zA-Z]','',spkr)),int(nb_section),str(section_topic), int(Turn_count), int(count))
                spkr_id = str(basename)+'_%s-%03d' % (str(re.sub('\d+','',spkr)),int(re.sub('[a-zA-Z]','',spkr)))
                text = transformation_text(text)
                #print("Sync or Background: wizzz "+text)
                end_utt=Time_start_current_sync
                if text!="":
                    #print("SAVED BY SYNC or BACKGROUND "+seg_id+" "+text)
                    Spk_that_contribute_to_meeting.add(spkr)
                    segments_file.write(seg_id+" "+basename+" "+str(start_utt)+" "+str(end_utt)+"\n")
                    utt2spk_file.write(seg_id+" "+spkr_id+"\n")
                    text_file.write(seg_id+" "+text+"\n")
                    text=""
                    count+=1
                start_utt=Time_start_current_sync
                text=Element.tail.replace('\n', '')
                #print(count)
                #count+=1
            elif Element.tag=="Comment" or Element.tag=="Background":
                text=text+" "+Element.tail.replace('\n', '')
            elif Element.tag=="Event":
            #    if Element.get('type')=='noise':
                    # ===== Respiration
                if Element.get('desc')=='r' or Element.get('desc')=='i' or Element.get('desc')=='e' or Element.get('desc')=='n':
                    text=text+" "+Element.tail.replace('\n', '')
                elif Element.get('desc')=='pf':
                    text=text+" "+Element.tail.replace('\n', '')
                # ===== Bruits bouches
                elif Element.get('desc')=='tx':
                    text=text+" "+Element.tail.replace('\n', '')
                elif Element.get('desc')=='bg':
                    text=text+" "+Element.tail.replace('\n', '')
                elif Element.get('desc')=='bb':
                    text=text+" "+Element.tail.replace('\n', '')
                elif Element.get('desc')=='rire':
                    text=text+" "+Element.tail.replace('\n', '')
                elif Element.get('desc')=='sif':
                    text=text+" "+Element.tail.replace('\n', '')
                elif Element.get('desc')=='ch' or Element.get('desc')=='ch-':
                    text=text+" "+Element.tail.replace('\n', '')
                # ====== Bruit exterieus a l'acte de parole
                elif Element.get('desc')=='b' or Element.get('desc')=='pap' or Element.get('desc')=='mic' or Element.get('desc')=='conv':
                    text=text+" "+Element.tail.replace('\n', '')
                elif Element.get('desc')=='top':
                    text=text+" "+Element.tail.replace('\n', '')
                # "pi" intellegible "pif" inaudible voir doc transcriber
            #elif Element.get('type')=='pronounce':
            #    text=text+" "+Element.tail.replace('\n', '')
            # desc="EN"
            #if Element.get('type')=='language':
            #    text=text+" "+Element.tail.replace('\n', '')
            #if Element.tag=="Who":
                else:
                    text=text+" "+Element.tail.replace('\n', '')
    if count > 0 and has_attrib_speaker and not Element.tail is None:
        #print text
        ### Save Files For Kaldi ###
        seg_id = str(basename) + '_%s-%03d_Section%02d_Topic-%s_Turn-%03d_seg-%07d' % (
        str(re.sub('\d+','',spkr)),int(re.sub('[a-zA-Z]','',spkr)),int(nb_section),str(section_topic), int(Turn_count), int(count))
        #seg_id = str(basename) + '_spk-%03d_Turn-%03d_seg-%07d' % (
        #int(spkr.split('spk')[1]), int(Turn_count), int(count))
        spkr_id = str(basename)+'_%s-%03d' % (str(re.sub('\d+','',spkr)),int(re.sub('[a-zA-Z]','',spkr)))
        text = transformation_text(text)
        if bool and text != "":
            #print("Last SAVE"+seg_id+" "+text)
            segments_file.write(seg_id+" "+basename+" "+str(start_utt)+" "+str(endTime)+"\n")
            utt2spk_file.write(seg_id+" "+spkr_id+"\n")
            text_file.write(seg_id+" "+text+"\n")
    # Gender file edition
    #print(Spk_that_contribute_to_meeting)
    #print(len(Spk_that_contribute_to_meeting))
    #print(speaker_gender)
    #print(len(speaker_gender))
    for spk in speaker_gender:
        if spk[0] in Spk_that_contribute_to_meeting:
            spk_id = str(basename)+'_%s-%03d' % (str(re.sub('\d+','',spk[0])),int(re.sub('[a-zA-Z]','',spk[0])))
            spk2gender.write(spk_id+" "+spk[1]+"\n")
    wav_scp.write(basename+" sox "+os.path.dirname(file_trs) + '/' + basename + '.wav'+" -t wav -r 16000 -c 1 - |\n")
    segments_file.close()
    utt2spk_file.close()
    text_file.close()
    wav_scp.close()
    topic_file.close()
    spk2gender.close()