views.py 4.63 KB
Newer Older
Sylvain Coulange's avatar
Sylvain Coulange committed
1
2
from django.shortcuts import render, HttpResponseRedirect
from static.scripts import textPhonographer as txtphono
3
from .models import Phonograph, Data, Entree, commitData
Sylvain Coulange's avatar
Sylvain Coulange committed
4
import spacy
5
import subprocess
6
import re
Sylvain Coulange's avatar
Sylvain Coulange committed
7
8
9
10
11
12
13
14
15
16
17
nlp = spacy.load('fr')

def redirApp(request):
    return HttpResponseRedirect('/app/')

def main(request):
    if request.POST.get('inText'):
        text = request.POST['inText']
        nlpText = nlp(text)
        data = Data()
        data.text = text
18
        data.update = updateTime()
Sylvain Coulange's avatar
Sylvain Coulange committed
19
        data.colorType = request.POST.get('colorType')
20
        data.defaultBg = request.POST.get('defaultBg')
Sylvain Coulange's avatar
Sylvain Coulange committed
21
22
        data.pphh = []

23
24
        punctuation = [',','.'] # ')',']','}','%','>',"'",'(','[','{'

Sylvain Coulange's avatar
Sylvain Coulange committed
25
        for token in nlpText:
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
            sdl = re.findall(r'\r\n',token.text)
            print("sdl =",sdl)
            if len(sdl) > 0:
                for s in sdl:
                    print("Saut de ligne.")
                    data.pphh.append('§')
            elif token.text in punctuation:
                data.pphh.append(token.text)
            else:
                print("Mot en entrée :",token.text)
                result = txtphono.mimi(token.text)
                print(result)
            
                phonographieList = []
                for r in result:
                    phonographie = []
                    for i in r:
                        ph = Phonograph()
                        ph.phon = i[0]
                        ph.graph = i[1]
                        phonographie.append(ph)
                    phonographieList.append(phonographie)
                data.pphh.append(phonographieList)
Sylvain Coulange's avatar
Sylvain Coulange committed
49
50
51
    else:
        data = Data()
        data.text = ''
Sylvain Coulange's avatar
Sylvain Coulange committed
52
        data.pphh = [[[]]]
53
        data.update = updateTime()
Sylvain Coulange's avatar
Sylvain Coulange committed
54
        data.colorType = 'sw'
55
        data.defaultBg = 'black'
Sylvain Coulange's avatar
Sylvain Coulange committed
56
57
    return render(request, 'coloriseur.html', {'data': data})

58
59
def updateTime():
    upd = str(subprocess.check_output(["git", "log", "-1", "--format=%cd", "--date=short"]))
60
    #ver = str(subprocess.check_output(["git", "rev-list", "--all", "--count"]))
61
    return 'Version 3.0 (mis à jour le '+upd[2:-3]+')'
Sylvain Coulange's avatar
Sylvain Coulange committed
62

63
64
65
def main0(request):
    data = Data()
    data.text = ''
Sylvain Coulange's avatar
Sylvain Coulange committed
66
    data.pphh = [[[]]]
67
    data.update = updateTime()
Sylvain Coulange's avatar
Sylvain Coulange committed
68
    data.colorType = 'sw'
69
    data.defaultBg = 'black'
70
71
72
    return render(request, 'coloriseur.html', {'data': data})


Sylvain Coulange's avatar
Sylvain Coulange committed
73
74
def dicoView(request):
    data = Entree()
75
    data.update = updateTime()
Sylvain Coulange's avatar
Sylvain Coulange committed
76
77
78
79
80
81
82
83
84
85
86
    if request.POST.get('inMot') or request.POST.get('inTrans'):
        data.mot = request.POST['inMot'] if request.POST.get('inMot') else ''
        data.motcond = request.POST['condition-mot'] if request.POST.get('condition-mot') else 'contient'
        data.trans = request.POST['inTrans'] if request.POST.get('inTrans') else ''
        data.transcond = request.POST['condition-trans'] if request.POST.get('condition-trans') else 'contient'
        data.result = txtphono.getEntryByWord(m=data.mot, mc=data.motcond, t=data.trans, tc=data.transcond)
        data.nb = len(data.result)
    else:
        data.mot = ''
    return render(request, 'editDico.html', {'data': data})

87
def editEntry(request,Type):
88
89
90
91
92
    mot = request.POST['entreeMot']# if request.POST.get('mot') else ''
    trans = request.POST['entreeTrans']# if request.POST.get('trans') else ''
    motOrigin = request.POST['motOrigin'] if request.POST.get('motOrigin') else ''
    searchMot = request.POST['inMot'] if request.POST.get('inMot') else mot
    searchTrans = request.POST['inTrans'] if request.POST.get('inTrans') else trans
Sylvain Coulange's avatar
Sylvain Coulange committed
93
94
95
    condition_mot = request.POST['condition-mot'] if request.POST.get('inMot') else 'contient'
    condition_trans = request.POST['condition-trans'] if request.POST.get('inTrans') else 'contient'

96
97
98
    if Type == 'MODIF':
        message = txtphono.modifEntry(motOrigin,mot,trans)
    elif Type == 'ADD':
99
100
101
102
103
        if mot == '' or trans == '':
            message = 'Entrée incomplète ! Indiquez un mot et sa transcription.'
        else:
            message = txtphono.addEntry(mot,trans)
    elif Type == 'DELETE':
104
105
        message = txtphono.delEntry(mot,trans)

Sylvain Coulange's avatar
Sylvain Coulange committed
106
    data = Entree()
107
    data.update = updateTime()
Sylvain Coulange's avatar
Sylvain Coulange committed
108
109
110
111
112
113
    data.mot = searchMot
    data.trans = searchTrans
    data.motcond = condition_mot
    data.transcond = condition_trans
    data.result = txtphono.getEntryByWord(m=data.mot, mc=data.motcond, t=data.trans, tc=data.transcond)
    data.nb = len(data.result)
114
    data.message = message
Sylvain Coulange's avatar
Sylvain Coulange committed
115
116
117
118
    
    return render(request, 'editDico.html', {'data': data})


119
120
121
122
""" def commits(request):
    data = commitData()
    data.update = updateTime()
    data.listCommits = subprocess.check_output(["git", "log", "--pretty=full"]).decode()
Sylvain Coulange's avatar
Sylvain Coulange committed
123
    return render(request, 'commits.html', {'data': data}) """