views.py 5.54 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
5
from django.http import JsonResponse
import json
Sylvain Coulange's avatar
Sylvain Coulange committed
6
import spacy
7
import subprocess
8
import re
9

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

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

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

23
24
25
26
def colorize(request):
    colis = json.loads(request.body)
    text = colis['inText']
    lang = colis['lang']
Sylvain Coulange's avatar
Sylvain Coulange committed
27
28
29
30
    if lang == "fr":
        nlpText = nlpFr(text)
    elif lang == "en":
        nlpText = nlpEn(text)
Sylvain Coulange's avatar
Sylvain Coulange committed
31
32
    elif lang == "zh":
        nlpText = nlpZh(text)
33
34
35
36
37
38
39
40
41
42
43
    outText = []

    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)
Sylvain Coulange's avatar
Sylvain Coulange committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
            if lang == "fr" or lang == "en":
                result = txtphono.traitement(token.text,lang)

                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)
            
            elif lang == "zh":
                result = txtphono.traitementzh(token.text)
                outText.append(result)
            print("Résultat en sortie :", result)
               
63
64
65
66
67
    rep = {
        'outText': outText
    }
    return JsonResponse(rep)

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

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":
                result = txtphono.mimi(token.text,lang)
            else:
                result = txtphono.mimi(token.text,lang)
            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)

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

125
126
127
def main0(request):
    data = Data()
    data.text = ''
Sylvain Coulange's avatar
Sylvain Coulange committed
128
    data.pphh = [[[]]]
129
    data.update = updateTime()
Sylvain Coulange's avatar
Sylvain Coulange committed
130
    data.colorType = 'sw'
131
    data.defaultBg = 'black'
132
133
134
    return render(request, 'coloriseur.html', {'data': data})


Sylvain Coulange's avatar
Sylvain Coulange committed
135
136
def dicoView(request):
    data = Entree()
137
    data.update = updateTime()
Sylvain Coulange's avatar
Sylvain Coulange committed
138
    data.lenDic = txtphono.getLenDicFr()
139
    data.logStat = txtphono.getLogStat()
Sylvain Coulange's avatar
Sylvain Coulange committed
140
141
    return render(request, 'editDico.html', {'data': data})

142
143
144
145
146
147
148
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
149
    rep = {
Sylvain Coulange's avatar
Sylvain Coulange committed
150
        "listeEntrees" : txtphono.getEntryByWord(m=mot,mc=motCond,t=trans,tc=transCond,lang='fr')
Sylvain Coulange's avatar
Sylvain Coulange committed
151
152
153
154
155
156
157
158
159
160
    }
    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
161

Sylvain Coulange's avatar
Sylvain Coulange committed
162
163
164
165
166
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
167
        'reponse': txtphono.modifEntry(ipCli,mot,transList,'fr')
Sylvain Coulange's avatar
Sylvain Coulange committed
168
169
    }
    return JsonResponse(rep)
Sylvain Coulange's avatar
Sylvain Coulange committed
170

171
def newEntry(request):
172
173
174
175
    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
176
        'reponse': txtphono.addEntry(ipCli,mot,trans,'fr')
177
178
179
    }
    return JsonResponse(rep)

180
181
182
183
def supprEntry(request):
    mot = request.GET.get('mot', None)
    ipCli = get_client_ip(request)
    rep = {
Sylvain Coulange's avatar
Sylvain Coulange committed
184
        'reponse': txtphono.delEntry(ipCli,mot,'fr')
185
186
187
    }
    return JsonResponse(rep)

188
def getLog(request):
Sylvain Coulange's avatar
Sylvain Coulange committed
189
    rep = txtphono.sendMeLogPlease('fr')
190
    return JsonResponse(rep)