views.py 9.45 KB
Newer Older
Sylvain Coulange's avatar
Sylvain Coulange committed
1
from django.shortcuts import render, HttpResponseRedirect
2
from . import textPhonographer as txtphono
3
from .models import Phonograph, Data, Entree, commitData
4
from django.http import JsonResponse
5
6
from .liaisons import *
import json, spacy, subprocess, re
7

8
nlpFr = spacy.load('fr_core_news_sm')
Sylvain Coulange's avatar
Sylvain Coulange committed
9
nlpEn = spacy.load("en_core_web_sm")
Sylvain Coulange's avatar
Sylvain Coulange committed
10
nlpZh = spacy.load("zh_core_web_sm")
11
logFile = "../logs/dico_frwiktionary-20200301_v2.log"
Sylvain Coulange's avatar
Sylvain Coulange committed
12
13

def redirApp(request):
Sylvain Coulange's avatar
Sylvain Coulange committed
14
    return HttpResponseRedirect('/')
Sylvain Coulange's avatar
Sylvain Coulange committed
15
16

def main(request):
17
18
    data = Data()
    data.update = updateTime()
Sylvain Coulange's avatar
Sylvain Coulange committed
19
20
    return render(request, 'coloriseur.html', {'data': data})

21
22
23
24
def colorize(request):
    colis = json.loads(request.body)
    text = colis['inText']
    lang = colis['lang']
Sylvain Coulange's avatar
Sylvain Coulange committed
25
26
27
28
    if lang == "fr":
        nlpText = nlpFr(text)
    elif lang == "en":
        nlpText = nlpEn(text)
Sylvain Coulange's avatar
Sylvain Coulange committed
29
30
    elif lang == "zh":
        nlpText = nlpZh(text)
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
    

    ######### traitement du tiret ############
    if lang == "fr":
        listeMots = []

        for i, mot in enumerate(nlpText):
            # Traiter les mots qui contiennent le '-'.
            # Ex : "-il", "Peut-être"
            if "-" in mot.text and len(mot.text) > 1 :
                temp = ""
                for l in mot.text :
                    if l != "-":
                        temp += l
                    else:
                        if temp == '':
                            listeMots.append("-")
                        else:
                            listeMots.append(temp)
                            listeMots.append("-")
                            temp = ""
                if temp != "":
                    listeMots.append(temp)
            # Traiter uniquement le tiret
            else:
                listeMots.append(mot.text)

        tiret = {} # EX : Vas-y ! Pouvait-il partir ? ==> {'Vas': 1, 'Pouvait': 5}
        for k,item in enumerate(listeMots):
            if item == "-" and k > 0 :
                tiret[listeMots[k-1]] = k
        print(tiret)

        # Remplacer tous les tirets dans le texte par le caractere espace
        if "-" in listeMots:
            text = text.replace('-', ' ')
        
        nlpText = nlpFr(text)
    #########################################
    
71
72
    outText = []

73
74
75
76
77
    # appeler la fonction pour savoir s'il y a une locution
    index = liaison_locution(text, nlpText)
    print("Index", index)

    for j, token in enumerate(nlpText):
78
79
80
81
82
83
84
85
        sdl = re.findall(r'\r\n',token.text)
        print("sdl =",sdl)
        if len(sdl) > 0:
            for s in sdl:
                print("Saut de ligne.")
                outText.append('§')
        else:
            print("Mot en entrée :",token.text)
Sylvain Coulange's avatar
Sylvain Coulange committed
86
            if lang == "fr" or lang == "en":
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
                liaison = None

                ## TRAITEMENT LIAISON POUR FR
                if lang == "fr":
                    if j + 1 < len(nlpText):
                        # ici on met la liaison directement s'il s'agit d'une locution avec exception
                        if index != None and (index[0]+index[1]) == j:
                                bigram = (nlpText[j].text, nlpText[j+1].text)
                                liaison = index[2]
                                print("bigram de locution :",bigram)
                        # appel de la fonction obtenirM1M2
                        else:
                            bigram = obtenirM1M2(token.text, nlpText[j+1].text)
                            print("bigram normal :",bigram)
                            # vérifie les règles de liaisons et denasalisation
                            if bigram != None:
                                print("bigram normal :",token.pos_,nlpText[j+1].pos_)
                                liaison = verifliaison(token, nlpText[j+1])
                                print("type de liaison : ",liaison)

                result = txtphono.traitement(token.text,lang,liaison) # LIAISON : ajoute le booléen liaison en argument
Sylvain Coulange's avatar
Sylvain Coulange committed
108
109
110
111
112
113
114
115
116
117
118
119

                phonographieList = []
                for r in result:
                    phonographie = []
                    for i in r[0]:
                        ph = {}
                        ph['phon'] = i[0]
                        ph['graph'] = i[1]
                        phonographie.append(ph)
                    phonographieList.append((phonographie,r[1],r[2],r[3]))
                outText.append(phonographieList)
            
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
                ## SUITE LIAISON ##
                if lang == 'fr':
                    ## Affichage de la liaison hors du mot
                    juge = False
                    if len(tiret) > 0 and token.text in tiret.keys() :
                        if liaison != None :
                            result1 = txtphono.traitement("-͜", lang, token.text+liaison)
                            juge = True
                        elif liaison == None :
                            result1 = txtphono.traitement("-", lang, liaison)
                            juge = True
                    else:
                        if liaison != None:
                            result1 = txtphono.traitement("‿", lang, token.text+liaison)
                            juge = True
                    if juge == True :
                        phonographieList = []
                        for r in result1:
                            phonographie = []
                            for i in r[0]:
                                ph = {}
                                ph['phon'] = i[0]
                                ph['graph'] = i[1]
                                phonographie.append(ph)
                            phonographieList.append((phonographie, r[1], r[2], r[3]))
                        outText.append(phonographieList)

Sylvain Coulange's avatar
Sylvain Coulange committed
147
148
149
150
151
            elif lang == "zh":
                result = txtphono.traitementzh(token.text)
                outText.append(result)
            print("Résultat en sortie :", result)
               
152
153
154
155
156
    rep = {
        'outText': outText
    }
    return JsonResponse(rep)

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

def getPhonoOf(request):
    # Renvoie une liste(texte) de listes(phonographiesPossibles) de listes(phonographie) de phonèmes à partir d'un mot ou d'une expression
    # ex. input="chat" → output=[[[phon_s_maj, phon_a], [phon_ts_maj, phon_a, phon_t]]]
    # ex. input="le chat" → output= [
    #   [
    #       [phon_l,phon_2],
    #       [phon_e_maj,phon_l,phon_2]
    #   ],
    #   [
    #       [phon_s_maj, phon_a],
    #       [phon_ts_maj, phon_a, phon_t]
    #   ]
    # ]
     
    colis = json.loads(request.body)
    text = colis['inText']
    lang = colis['lang']
    if lang == "fr":
        nlpText = nlpFr(text)
    elif lang == "en":
        nlpText = nlpEn(text)
    outPhono = []

    for token in nlpText:
        sdl = re.findall(r'\r\n',token.text)
        print("sdl =",sdl)
        if len(sdl) > 0:
            for s in sdl:
                print("Saut de ligne.")
                outText.append('§')
        else:
            print("Mot en entrée :",token.text)
            if lang == "en":
Sylvain Coulange's avatar
Sylvain Coulange committed
191
                result = txtphono.traitement(token.text,lang)
192
            else:
Sylvain Coulange's avatar
Sylvain Coulange committed
193
                result = txtphono.traitement(token.text,lang)
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
            print(result)
        
            phonographieList = []
            for r in result:      
                phonoliste = []
                for i in r[0]:
                    phonoliste.append(i[0])
                    
                phonographieList.append(phonoliste)
            outPhono.append(phonographieList)
    rep = {
        'outText': outPhono
    }
    return JsonResponse(rep)

209
210
def updateTime():
    upd = str(subprocess.check_output(["git", "log", "-1", "--format=%cd", "--date=short"]))
211
    #ver = str(subprocess.check_output(["git", "rev-list", "--all", "--count"]))
Sylvain Coulange's avatar
Sylvain Coulange committed
212
    return 'Version 4.0 (mis à jour le '+upd[2:-3]+')'
Sylvain Coulange's avatar
Sylvain Coulange committed
213

214
215
216
def main0(request):
    data = Data()
    data.text = ''
Sylvain Coulange's avatar
Sylvain Coulange committed
217
    data.pphh = [[[]]]
218
    data.update = updateTime()
Sylvain Coulange's avatar
Sylvain Coulange committed
219
    data.colorType = 'sw'
220
    data.defaultBg = 'black'
221
222
223
    return render(request, 'coloriseur.html', {'data': data})


Sylvain Coulange's avatar
Sylvain Coulange committed
224
225
def dicoView(request):
    data = Entree()
226
    data.update = updateTime()
Sylvain Coulange's avatar
Sylvain Coulange committed
227
    data.lenDic = txtphono.getLenDicFr()
228
    data.logStat = txtphono.getLogStat()
Sylvain Coulange's avatar
Sylvain Coulange committed
229
230
    return render(request, 'editDico.html', {'data': data})

231
232
233
234
235
236
237
def dicoReq(request):
    listeEntrees = []
    mot = request.GET.get('mot', None)
    trans = request.GET.get('trans', None)
    motCond = request.GET.get('motCond', None)
    transCond = request.GET.get('transCond', None)
    
Sylvain Coulange's avatar
Sylvain Coulange committed
238
    rep = {
Sylvain Coulange's avatar
Sylvain Coulange committed
239
        "listeEntrees" : txtphono.getEntryByWord(m=mot,mc=motCond,t=trans,tc=transCond,lang='fr')
Sylvain Coulange's avatar
Sylvain Coulange committed
240
241
242
243
244
245
246
247
248
249
    }
    return JsonResponse(rep)

def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip
250

Sylvain Coulange's avatar
Sylvain Coulange committed
251
252
253
254
255
def editEntry(request):
    mot = request.GET.get('mot', None)
    transList = request.GET.get('transList', None)
    ipCli = get_client_ip(request)
    rep = {
Sylvain Coulange's avatar
Sylvain Coulange committed
256
        'reponse': txtphono.modifEntry(ipCli,mot,transList,'fr')
Sylvain Coulange's avatar
Sylvain Coulange committed
257
258
    }
    return JsonResponse(rep)
Sylvain Coulange's avatar
Sylvain Coulange committed
259

260
def newEntry(request):
261
262
263
264
    mot = request.GET.get('mot', None)
    trans = request.GET.get('trans', None)
    ipCli = get_client_ip(request)
    rep = {
Sylvain Coulange's avatar
Sylvain Coulange committed
265
        'reponse': txtphono.addEntry(ipCli,mot,trans,'fr')
266
267
268
    }
    return JsonResponse(rep)

269
270
271
272
def supprEntry(request):
    mot = request.GET.get('mot', None)
    ipCli = get_client_ip(request)
    rep = {
Sylvain Coulange's avatar
Sylvain Coulange committed
273
        'reponse': txtphono.delEntry(ipCli,mot,'fr')
274
275
276
    }
    return JsonResponse(rep)

277
def getLog(request):
Sylvain Coulange's avatar
Sylvain Coulange committed
278
    rep = txtphono.sendMeLogPlease('fr')
279
    return JsonResponse(rep)