parseText.py 6.22 KB
Newer Older
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
1
#!/usr/bin/env python
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
2
# -*- coding: utf-8 -*-
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
3 4 5 6 7

from xml.etree import ElementTree as ET
from unicodedata import normalize
from sys import argv
from num2words import num2words
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
8
from unidecode import unidecode
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
9 10
import re
import os.path
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
11
import sys
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
12 13 14 15 16 17 18 19 20 21 22 23
# ( in text
# ) in text
def transformation_text(text):
    bool=True
    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:
        #print text
        #print "Ligne Supprime"
        bool=False
    else:
        # 4x4
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
24
        # Remove noise sound (BIP) over Name of places and person
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
25
        #text = re.sub(r"¤[^ ]+|[^ ]+¤|¤", "", text.strip())
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
26 27 28 29 30 31 32 33 34
        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)
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
35 36 37 38
        # remove silence character : OK
        #text=re.sub(r"(/.+/","remplacer par la 1er",text)
        # Liaison non standard remarquable
        text=re.sub(r'=','',text)
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52
        # 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
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
53
        #<[^\p{L}]|[^\p{L}]>|#+|<\p{L}+[ ]|<\p{L}+$
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
54
        text=re.sub(r":|\?|/|\!|<|>|#+","",text)
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
55 56
        # replace silence character with <sil> : OK
        #text=re.sub(r"(\+)", "<sil>", text)
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
57 58
        text=re.sub(r"(\+)", "", text)
        text=re.sub(r"(///)", "", text)
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
59 60 61 62 63 64 65 66 67 68 69 70
        #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
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
71 72
                        # Remove noise sound (BIP) over Name of places and person
        text=re.sub(r"(¤.+¤)",'',text)
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
73
        # replace unkown syllable
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        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+| \d+$", text)
        if len(num_list) > 0:
            #print text
            #print "********************************* NUM2WORD"
            for num in num_list:
                num_in_word = num2words(int(num), lang='fr')
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
89
                #num_in_word=normalize('NFKD', num_in_word).encode('ascii', 'ignore')
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
90 91 92 93 94
                text = text.replace(str(num), " " + str(num_in_word) + " ")
                #print text
                # replace n succesive spaces with one space. : OK
        text=re.sub(r"\s{2,}"," ",text)
        text = re.sub("^ ", '', text)
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
95 96 97
    # change bounding | to < and > : OK
    #balise=set(re.findall(r"\|\w+_?\w+\|",text))
    #if len(balise)>0:
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
98
        #print(balise)
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
99 100 101
    #    for b in balise:
    #        new_balise='<'+b[1:len(b)-1]+'>'
    #        text=text.replace(b,new_balise)
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
102
            #print(text)
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
103 104 105 106 107 108
    # c'est l'essaim ....
    text=text.lower()
    return bool,text
if __name__=="__main__":
    # Inputs
    file_trs=argv[1]
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
109 110
    #print(file_trs)
    #print file_trs
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    # Read Trans File
    tree_trs = ET.parse(file_trs)
    trsdoc= tree_trs.getroot()
    text=""
    Turn_count=0
    count=0
    has_attrib_speaker=False
    # set for uniq add
    for Element in trsdoc.iter():
        if Element.tag=="Turn" and Element.get('speaker') is None:
            has_attrib_speaker=False
        elif Element.tag=="Turn":
            # If the latest Utterance of previous Speaker is the latest one of his Turn speech
            if Turn_count>0:
                count = 0
                bool, text = transformation_text(text)
                # File wav.scp
                # File utt2spk
                # File text
                # File speaker_gender
                if bool and text!="":
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
132
                    print(text)
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
133 134 135 136 137 138 139 140 141 142 143
                    #for spk_tuple in speaker_gender:
                    #    if spk_tuple[0]==spkr:
                    #        print >> spk2gender,'%s %s' % (seg_id, spk_tuple[1])
                    #        break
            has_attrib_speaker=True
            # count sync for computing start and end utterance
            Turn_count = Turn_count+1
        elif Element.tag=="Sync" and has_attrib_speaker:
            if count>0:
                bool, text = transformation_text(text)
                if bool and text!="":
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
144
                    print(text)
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
145 146 147 148 149
            text=Element.tail.replace('\n', '')
            count=count+1
        elif Element.tag=="Comment" and has_attrib_speaker and not Element.tail is None:
            text=text+" "+Element.tail.replace('\n', '')
        elif Element.tag=="Event" and has_attrib_speaker and not Element.tail is None :
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
150
            text=text+" "+Element.tail.replace('\n', '')
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
151 152 153 154 155
        elif Element.tag=="Who" and has_attrib_speaker and not Element.tail is None:
            text=text+" "+Element.tail.replace('\n', '')
    if count > 0 and has_attrib_speaker and not Element.tail is None:
        bool, text = transformation_text(text)
        if bool and text != "":
Abdelwahab HEBA's avatar
Abdelwahab HEBA committed
156
            print(text)