views.py 12.7 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
from .liaisons import *
6
from .english_spacyExceptions import getOneWordList
Sylvain Coulange's avatar
Sylvain Coulange committed
7
8
import json, spacy, subprocess, re, requests
from user_agents import parse
9

10
11
print('Chargement des modèles de langue...')
print('fr_core_news_md...')
12
nlpFr = spacy.load('fr_core_news_md')
Sylvain Coulange's avatar
Sylvain Coulange committed
13
14
15
16
17
18
print('en_core_web_sm...')
nlpEn = spacy.load("en_core_web_sm")
print('zh_core_web_sm...')
nlpZh = spacy.load("zh_core_web_sm")
print('OK.')
logFile = "../logs/dico_frwiktionary-20200301_v2.log"
Sylvain Coulange's avatar
Sylvain Coulange committed
19

20
21
22
23

## RÉCUPÉRATION DE LA LISTE DES EXCEPTIONS POUR L'ANGLAIS
asOneWord = getOneWordList()

Sylvain Coulange's avatar
Sylvain Coulange committed
24
def redirApp(request):
Sylvain Coulange's avatar
Sylvain Coulange committed
25
    return HttpResponseRedirect('/')
Sylvain Coulange's avatar
Sylvain Coulange committed
26

27
28
29
30
31
def main0(request):
    data = Data()
    data.update = updateTime()
    return render(request, 'coloriseur.html', {'data': data, 'pageLang':"fr"})

32
def main(request,pageLang,**mySettings):
33
34
    data = Data()
    data.update = updateTime()
35
    return render(request, 'coloriseur.html', {'data': data, 'pageLang':pageLang})
Sylvain Coulange's avatar
Sylvain Coulange committed
36

37
38
39
40
def colorize(request):
    colis = json.loads(request.body)
    text = colis['inText']
    lang = colis['lang']
Sylvain Coulange's avatar
Sylvain Coulange committed
41
42
43
44
    if lang == "fr":
        nlpText = nlpFr(text)
    elif lang == "en":
        nlpText = nlpEn(text)
Sylvain Coulange's avatar
Sylvain Coulange committed
45
46
    elif lang == "zh":
        nlpText = nlpZh(text)
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
    

    ######### 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)
    #########################################
    
87
88
    outText = []

89
90
91
92
93
    # 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):
94
95
96
97
98
99
100
        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:
101
102
103
104
105

            thisTokenText = token.text

            # TRAITEMENT CONTRACTIONS ANGLAIS AUTRES gonna cannot gotta
            if lang == "en":
106
                if thisTokenText.lower() in asOneWord.keys() and nlpText[j+1].text.lower() == asOneWord[thisTokenText.lower()]:
107
108
                    print("CONCATÉNATION CONTRACTION ANGLAIS:", thisTokenText, nlpText[j+1].text)
                    thisTokenText = thisTokenText + nlpText[j+1].text
109
                elif thisTokenText.lower() in asOneWord.values() and nlpText[j-1].text.lower() in asOneWord.keys() and asOneWord[nlpText[j-1].text.lower()] == thisTokenText.lower():
110
111
112
113
                    continue # => si on est passé par le if à l'itération suivante, on passe au token suivant directement (ignorer "n't"..etc)


            print("Mot en entrée :",thisTokenText)
Sylvain Coulange's avatar
Sylvain Coulange committed
114
            if lang == "fr" or lang == "en":
115
116
117
118
119
120
121
122
123
124
125
126
                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:
127
                            bigram = obtenirM1M2(thisTokenText, nlpText[j+1].text)
128
129
130
131
132
133
134
                            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)

135
                result = txtphono.traitement(thisTokenText,lang,liaison) # LIAISON : ajoute le booléen liaison en argument
Sylvain Coulange's avatar
Sylvain Coulange committed
136
137
138
139
140
141
142
143
144
145
146
147

                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)
            
148
149
150
151
                ## SUITE LIAISON ##
                if lang == 'fr':
                    ## Affichage de la liaison hors du mot
                    juge = False
152
                    if len(tiret) > 0 and thisTokenText in tiret.keys() :
153
                        if liaison != None :
154
                            result1 = txtphono.traitement("-͜", lang, thisTokenText+liaison)
155
156
157
158
159
160
                            juge = True
                        elif liaison == None :
                            result1 = txtphono.traitement("-", lang, liaison)
                            juge = True
                    else:
                        if liaison != None:
161
                            result1 = txtphono.traitement("‿", lang, thisTokenText+liaison)
162
163
164
165
166
167
168
169
170
171
172
173
174
                            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
175
            elif lang == "zh":
176
                result = txtphono.traitementzh(thisTokenText)
Sylvain Coulange's avatar
Sylvain Coulange committed
177
178
179
                outText.append(result)
            print("Résultat en sortie :", result)
               
180
181
182
183
184
    rep = {
        'outText': outText
    }
    return JsonResponse(rep)

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

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
219
                result = txtphono.traitement(token.text,lang)
220
            else:
221
                result = txtphono.traitement(token.text,lang,None)
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
            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)

237
238
def updateTime():
    upd = str(subprocess.check_output(["git", "log", "-1", "--format=%cd", "--date=short"]))
239
    #ver = str(subprocess.check_output(["git", "rev-list", "--all", "--count"]))
Sylvain Coulange's avatar
Sylvain Coulange committed
240
    return 'Version 5.0 (mis à jour le '+upd[2:-3]+')'
Sylvain Coulange's avatar
Sylvain Coulange committed
241

242

Sylvain Coulange's avatar
Sylvain Coulange committed
243
def dicoView(request):
244
    print("Réception requête dicoView().")
245
246
    data = Entree()
    data.update = updateTime()
247
248
249
    data.nbMotsFr = txtphono.getLenDic('fr')
    data.nbMotsEn = txtphono.getLenDic('en')
    data.nbMotsZh = txtphono.getLenDic('zh')
250
251
252
    return render(request, 'editDicoHome.html', {'data': data})

def dicoViewFr(request):
Sylvain Coulange's avatar
Sylvain Coulange committed
253
    data = Entree()
254
    data.update = updateTime()
255
    data.lenDic = txtphono.getLenDic('fr')
256
    data.logStat = txtphono.getLogStat('fr')
257
258
259
260
261
262
    data.dicoLang = "fr"
    return render(request, 'editDico.html', {'data': data})

def dicoViewEn(request):
    data = Entree()
    data.update = updateTime()
263
    data.lenDic = txtphono.getLenDic('en')
264
    data.logStat = txtphono.getLogStat('en')
265
    data.dicoLang = "en"
Sylvain Coulange's avatar
Sylvain Coulange committed
266
267
    return render(request, 'editDico.html', {'data': data})

Sylvain Coulange's avatar
Sylvain Coulange committed
268
269
270
def dicoViewZh(request):
    data = Entree()
    data.update = updateTime()
271
    data.lenDic = txtphono.getLenDic('zh')
Sylvain Coulange's avatar
Sylvain Coulange committed
272
273
274
275
    data.logStat = txtphono.getLogStat('zh')
    data.dicoLang = "zh"
    return render(request, 'editDico.html', {'data': data})

276
def dicoReq(request):
277
    dicoLang = request.GET.get('dicoLang', None)
278
279
280
281
282
    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
283
    rep = {
284
        "listeEntrees" : txtphono.getEntryByWord(m=mot,mc=motCond,t=trans,tc=transCond,lang=dicoLang)
Sylvain Coulange's avatar
Sylvain Coulange committed
285
286
287
    }
    return JsonResponse(rep)

288
289
290
291
292
293
294
295
296
297
def checkWord(request):
    w = request.GET.get('w', None)
    t = request.GET.get('t', None)
    lang = request.GET.get('lang', None)

    rep = {
        'rep': txtphono.checkIfWordExists(w,t,lang)
    }
    return JsonResponse(rep)

298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
def checkWordList(request):
    colis = json.loads(request.body)
    wordPhonoSpansList = colis['wordPhonoSpansList']
    lang = colis['lang']

    repList = []

    for w in wordPhonoSpansList:
        repList.append(txtphono.checkIfWordExists(w['w'],w['t'],lang))

    rep = {
        'rep': repList
    }
    return JsonResponse(rep)

Sylvain Coulange's avatar
Sylvain Coulange committed
313
314
315
316
317
318
319
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
320

Sylvain Coulange's avatar
Sylvain Coulange committed
321
def editEntry(request):
Sylvain Coulange's avatar
Sylvain Coulange committed
322
323
324
325
326
    colis = json.loads(request.body)
    mot = colis['mot']
    lang = colis['lang']
    infos = colis['infos']
    print(infos)
Sylvain Coulange's avatar
Sylvain Coulange committed
327
    ipCli = get_client_ip(request)
Sylvain Coulange's avatar
Sylvain Coulange committed
328
329

    return JsonResponse(txtphono.modifEntry(ipCli,mot,infos,lang))
Sylvain Coulange's avatar
Sylvain Coulange committed
330

331
def newEntry(request):
332
333
    mot = request.GET.get('mot', None)
    trans = request.GET.get('trans', None)
334
    lang = request.GET.get('lang', None)
335
    ipCli = get_client_ip(request)
Sylvain Coulange's avatar
Sylvain Coulange committed
336
337

    return JsonResponse(txtphono.addEntry(ipCli,mot,trans,lang))
338

339
340
def supprEntry(request):
    mot = request.GET.get('mot', None)
341
    lang = request.GET.get('lang', None)
342
    ipCli = get_client_ip(request)
Sylvain Coulange's avatar
Sylvain Coulange committed
343
344

    return JsonResponse(txtphono.delEntry(ipCli,mot,lang))
345

346
def getLog(request):
Sylvain Coulange's avatar
Sylvain Coulange committed
347
348
    lang = request.GET.get('lang', None)
    rep = txtphono.sendMeLogPlease(lang)
349
350
351
352
    return JsonResponse({"logs":rep})

def addStat(request):

Sylvain Coulange's avatar
Sylvain Coulange committed
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
    if get_client_ip(request)!="127.0.0.1":
        colis = json.loads(request.body)
        app = colis["app"]
        module = colis["module"]
        ip = get_client_ip(request)
        agent = parse(request.META['HTTP_USER_AGENT'])
        country = getCountry(get_client_ip(request))
        lang = colis["lang"]

        txtphono.addStat(app, module, ip, agent, country, lang)

    return JsonResponse({})

def getCountry(ip):
    endpoint = f'https://ipinfo.io/{ip}/json'
    response = requests.get(endpoint, verify = True)

    if response.status_code != 200:
        return 'Status:', response.status_code, 'Problem with the request. Exiting.'
        exit()
373

Sylvain Coulange's avatar
Sylvain Coulange committed
374
375
376
    data = response.json()
    print(data)
    return data['country']