Commit 6e236c50 authored by Sylvain Coulange's avatar Sylvain Coulange
Browse files

finalisation v5.0

parent a4b9c825
# -*- encoding:utf8 -*- # -*- encoding:utf8 -*-
import re, sys, os, datetime, json, tempfile, csv, locale, pinyin import re, sys, os, datetime, json, tempfile, csv, locale, pinyin, pymongo
from bson import json_util
from colorapp.models import Entree, DicEntry, LogStat from colorapp.models import Entree, DicEntry, LogStat
from collections import OrderedDict from collections import OrderedDict
...@@ -26,8 +27,6 @@ api2classFile = "../phon2graph/data/api2class.json" # MANDARIN ...@@ -26,8 +27,6 @@ api2classFile = "../phon2graph/data/api2class.json" # MANDARIN
######################################## ########################################
########## CONNEXION MONGODB ########### ########## CONNEXION MONGODB ###########
######################################## ########################################
import pymongo
def connexion(): def connexion():
mdp = "" mdp = ""
with open('../private/dbmdp','r') as infile: with open('../private/dbmdp','r') as infile:
...@@ -210,10 +209,19 @@ def traitement(mot, lang, liaison): # LIAISON : avec le caractere liaison en arg ...@@ -210,10 +209,19 @@ def traitement(mot, lang, liaison): # LIAISON : avec le caractere liaison en arg
print("'", mot, "' trouvé dans le dico !",word2trans[mot]) print("'", mot, "' trouvé dans le dico !",word2trans[mot])
if lang in ["fr","en"]: if lang in ["fr","en"]:
transList = [] transList = []
for trans, infos in word2trans[mot]['t'].items(): shortmem = [] # pour pas afficher plusieurs fois un même alignement (avec et sans syllabation typiquement; le visuel est identique)
transList.append((trans.replace("_",""),infos['r'])) # SUPPRESSION SYMBOLE SYLLABE ICI '_' for trans in word2trans[mot]['t']:
print("TRANS",trans, trans.replace("_","")) newT = trans['tt'].replace('.','')
print("LOCS", infos['r']) if newT not in shortmem:
transList.append((newT,trans['tr']))
shortmem.append(newT)
else:
for r in trans['tr']:
# pour chaque région du trans courrant: si elle n'est pas dans les régions de la trans identique déjà présente dans transList: on l'ajoute
# ci-dessous : alreadyPushedTuple correspond au tuple (newT,trans['r']) déjà présent dans transList, auquel on veut ajouter r
if r not in [alreadyPushedTuple for alreadyPushedTuple in transList if alreadyPushedTuple[0]==newT][0][1]:
[alreadyPushedTuple for alreadyPushedTuple in transList if alreadyPushedTuple[0]==newT][0][1].append(r)
print("TRANS",trans)
result = [] result = []
############ partie d'appel de la fonction denasalisation ############ partie d'appel de la fonction denasalisation
...@@ -262,7 +270,7 @@ def traitementzh(mot): ...@@ -262,7 +270,7 @@ def traitementzh(mot):
result = [] # liste type : [[car, api, phonlist, ton], [car, api, phonlist, ton]...] result = [] # liste type : [[car, api, phonlist, ton], [car, api, phonlist, ton]...]
if mot in word2transZh.keys(): if mot in word2transZh.keys():
pinyinOutput = list(word2transZh[mot]["t"].keys())[0].lower() pinyinOutput = word2transZh[mot]["t"][0]["tt"].lower()
print("Mot trouvé dans le dictionnaire :", mot, pinyinOutput) print("Mot trouvé dans le dictionnaire :", mot, pinyinOutput)
else: else:
...@@ -295,9 +303,14 @@ def getEntryByWord(m,mc,t,tc,lang): ...@@ -295,9 +303,14 @@ def getEntryByWord(m,mc,t,tc,lang):
if lang == "fr": if lang == "fr":
word2trans = word2transFr word2trans = word2transFr
m = m.replace("'",'’') m = m.replace("'",'’')
dico = dicoFr
elif lang == "en": elif lang == "en":
word2trans = word2transEn word2trans = word2transEn
m = m.replace('’',"'") m = m.replace('’',"'")
dico = dicoEn
elif lang == "zh":
word2trans = word2transZh
dico = dicoZh
m = m.lower() m = m.lower()
t = t.replace('.','') t = t.replace('.','')
...@@ -321,137 +334,149 @@ def getEntryByWord(m,mc,t,tc,lang): ...@@ -321,137 +334,149 @@ def getEntryByWord(m,mc,t,tc,lang):
if tc == 'finit par' or tc == 'est égal à': if tc == 'finit par' or tc == 'est égal à':
tc2 = r'$' tc2 = r'$'
for entree,transList in word2trans.items(): # for entree, infos in word2trans.items():
if re.match(mc1+m+mc2,entree): # if re.match(mc1+m+mc2,entree):
transOK = False # transOK = False
for i,trans in enumerate(list(transList['t'].keys())): # for i,trans in enumerate(infos['t']):
if re.match(tc1+t+tc2,trans.replace('.','')): # if re.match(tc1+t+tc2,trans['t'].replace('.','')):
transOK = True # transOK = True
if transOK: result[entree] = transList # if transOK: result[entree] = infos['t']
# Requêter directement MongoDB et envoyer l'ensemble des résultats (cétégories, traductions... compris)
for entree in dico.find({'w': {"$regex": mc1+m+mc2}}):
transOK = False
for i,trans in enumerate(entree['t']):
if re.match(tc1+t+tc2,trans['tt'].replace('.','')):
transOK = True
if transOK:
result[entree['w']] = {}
for i, j in entree.items():
if i != "_id":
result[entree['w']][i] = j
return sorted(result.items(), key=lambda M: locale.strxfrm(M[0])) # mise dans l'ordre alphabétique français (ex. é après e, et pas après z) return sorted(result.items(), key=lambda M: locale.strxfrm(M[0])) # mise dans l'ordre alphabétique français (ex. é après e, et pas après z)
def modifEntry(user,mot,newTransList,lang): def modifEntry(user,mot,infos,lang):
if lang == "fr": if lang == "fr":
word2trans = word2transFr word2trans = word2transFr
dico = dicoFr
if lang == "en":
word2trans = word2transEn
dico = dicoEn
if lang == "zh":
word2trans = word2transZh
dico = dicoZh
newTransList = newTransList.split(',') oldInfos = dico.find_one({'w':mot})
for ind,i in enumerate(newTransList):
newTransList[ind] = i.strip() dico.update({"w":mot}, {"$set": infos})
oldTransList = word2trans[mot] word2trans[mot]['t'] = infos['t']
if newTransList != oldTransList:
word2trans[mot] = newTransList
rewriteDico(lang)
writeLog(Type="MODIF", user=user, mot=mot, oldTransList=oldTransList, newTransList=newTransList, lang=lang)
print("Modification du dictionnaire par",user,": mot =",mot,"; oldTransList =",oldTransList,"; newTransList = ",newTransList)
return "Modification effectuée avec succès !"
elif lang == "en": newlog = writeLog(Type="MODIF", user=user, mot=mot, oldMot=oldInfos, newMot=infos, lang=lang)
word2trans = word2transEn print("Modification du dictionnaire par",user,": lang =",lang,"; mot =",mot,"; oldMot =",oldInfos,"; newMot =",infos)
return "La modification du dictionnaire anglais n'est pas encore possible." print("ICIIII newlog:",newlog)
return {"msg":"Modification effectuée avec succès !", "newlog":newlog}
def addEntry(user,mot,trans,lang): def addEntry(user,mot,trans,lang):
if lang == "fr": if lang == "fr":
word2trans = word2transFr word2trans = word2transFr
dico = dicoFr
trans = trans.replace('g','ɡ') trans = trans.replace('g','ɡ')
mot = mot.lower() trans = trans.replace('r','ʁ')
mot = mot.strip() if lang == "en":
transList = trans.split(',')
for ind,i in enumerate(transList):
transList[ind] = i.strip()
if mot not in word2trans.keys():
word2trans[mot] = transList
rewriteDico("fr")
writeLog(Type="ADD", user=user, mot=mot, oldTransList="", newTransList=transList, lang="fr")
print("Modification du dictionnaire par",user,": ajout de mot =",mot,"; transList = ",transList)
return "Ajout effectué avec succès ! Merci pour votre participation !"
else:
return "Ce mot est déjà dans le dictionnaire !"
elif lang == "en":
word2trans = word2transEn word2trans = word2transEn
return "La modification du dictionnaire anglais n'est pas encore possible." dico = dicoEn
trans = trans.replace('g','ɡ')
if lang == "zh":
word2trans = word2transZh
dico = dicoZh
mot = mot.lower()
mot = mot.strip()
transList = trans.split(',')
for ind,i in enumerate(transList):
transList[ind] = i.strip()
if mot not in word2trans.keys():
newMot = {'t': []}
for t in transList:
if len(t)>0:
if lang =="fr":
newMot['t'].append({ 'tt':t, 'tr':['FR']})
elif lang =="zh":
newMot['t'].append({ 'tt':t, 'tr':['CN']})
else:
newMot['t'].append({ 'tt':t, 'tr':[]})
if len(newMot['t'])>0:
word2trans[mot] = newMot
dico.insert_one({ 'w':mot, 't':newMot['t']})
newlog = writeLog(Type="ADD", user=user, mot=mot, oldMot="", newMot=transList, lang=lang)
return {"msg":"Ajout effectué avec succès ! Merci pour votre participation !", "newlog":newlog}
else:
return {"msg":"Transcription vide. Veuillez indiquer au moins une transcriptions phonétiques. Ajout annulé."}
else:
return {"msg":"Ce mot est déjà dans le dictionnaire !"}
def delEntry(user,mot,lang): def delEntry(user,mot,lang):
if lang == "fr": if lang == "fr":
word2trans = word2transFr word2trans = word2transFr
dico = dicoFr
if mot in word2trans.keys(): if lang == "en":
word2trans.pop(mot)
if lang == "fr":
rewriteDico('fr')
writeLog(Type="DEL", user=user, mot=mot, oldTransList="", newTransList="",lang="fr")
if lang == "en":
rewriteDico('en')
writeLog(Type="DEL", user=user, mot=mot, oldTransList="", newTransList="",lang="en")
print("Modification du dictionnaire par",user,": suppression de mot =",mot)
return "Suppression effectuée avec succès."
elif lang == "en":
word2trans = word2transEn word2trans = word2transEn
return "La modification du dictionnaire anglais n'est pas encore possible." dico = dicoEn
if lang == "zh":
word2trans = word2transZh
dico = dicoZh
if mot in word2trans.keys():
oldmot = word2trans[mot]
word2trans.pop(mot)
dico.delete_one({'w':mot})
newlog = writeLog(Type="DEL", user=user, mot=mot, oldMot=oldmot, newMot="",lang=lang)
print("Modification du dictionnaire par",user,": suppression de mot =",mot)
return {"msg":"Suppression effectuée avec succès.", "newlog":newlog}
def rewriteDico(lang): def sendMeLogPlease(lang):
if lang == "fr": if lang == "fr":
word2trans = word2transFr logDic = dicoFrLogs
print("REWRITE DICO FR")
'''
print('Rewritting dictionary...')
with tempfile.NamedTemporaryFile(dir='.', delete=False, mode="w") as tmp:
json.dump(word2trans, tmp, ensure_ascii=False)
os.replace(tmp.name, dicFileFr)
print('Dico rewritten.')
'''
elif lang == "en": elif lang == "en":
word2trans = word2transEn logDic = dicoEnLogs
return "La modification du dictionnaire anglais n'est pas encore possible." elif lang == "zh":
logDic = dicoZhLogs
rep = []
for log in logDic.find():
rep.append(noIdDict(log))
def sendMeLogPlease(lang): return rep
if lang == "fr":
return logDicFr
elif lang == "en":
return logDicEn
def writeLog(Type,user,mot,oldTransList,newTransList,lang): def noIdDict(dic):
if type(dic) is dict:
# renvoit un dic sans la clé "_id"
return { i:j for i,j in dic.items() if i!='_id' }
else:
return dic
def writeLog(Type,user,mot,oldMot,newMot,lang):
if lang == "fr": if lang == "fr":
logDic = logDicFr logDic = dicoFrLogs
elif lang == "en": elif lang == "en":
logDic = logDicEn logDic = dicoEnLogs
elif lang == "zh":
logDic = dicoZhLogs
now = datetime.datetime.now() newlog = {
date = "{}-{}-{} {}:{}:{}".format(now.year, now.month, now.day, now.hour, now.minute, now.second) "date":datetime.datetime.now(),
if mot not in logDic.keys(): logDic[mot] = []
logDic[mot].append({
"date":date,
"user":user, "user":user,
"oldTransList":oldTransList, "word":mot,
"newTransList":newTransList, "old":noIdDict(oldMot),
"Type":Type "new":noIdDict(newMot),
}) "type":Type
}
with tempfile.NamedTemporaryFile(dir='.', delete=False, mode="w") as tmp: logDic.insert_one(newlog)
json.dump(logDic, tmp, ensure_ascii=False, indent=4)
if lang == "fr":
os.replace(tmp.name, logFile)
elif lang == "en":
os.replace(tmp.name, logFileEn)
print("Log file updated.")
def writeLogBugs(mot, lang):
now = datetime.datetime.now()
date = "{}-{}-{} {}:{}:{}".format(now.year, now.month, now.day, now.hour, now.minute, now.second)
logBug[mot] = date print("New log: lang={} ; user={} ; word={} ; type={}".format(lang,user,mot,Type))
with tempfile.NamedTemporaryFile(dir='.', delete=False, mode="w") as tmp: return noIdDict(newlog)
json.dump(logBug, tmp, ensure_ascii=False, indent=4) \ No newline at end of file
if lang == "fr":
os.replace(tmp.name, logBugFile)
elif lang == "en":
os.replace(tmp.name, logBugFileEn)
print("Log file updated.")
\ No newline at end of file
...@@ -214,7 +214,7 @@ def getPhonoOf(request): ...@@ -214,7 +214,7 @@ def getPhonoOf(request):
def updateTime(): def updateTime():
upd = str(subprocess.check_output(["git", "log", "-1", "--format=%cd", "--date=short"])) upd = str(subprocess.check_output(["git", "log", "-1", "--format=%cd", "--date=short"]))
#ver = str(subprocess.check_output(["git", "rev-list", "--all", "--count"])) #ver = str(subprocess.check_output(["git", "rev-list", "--all", "--count"]))
return 'Version 4.0 (mis à jour le '+upd[2:-3]+')' return 'Version 5.0 (mis à jour le '+upd[2:-3]+')'
def main0(request): def main0(request):
data = Data() data = Data()
...@@ -250,6 +250,14 @@ def dicoViewEn(request): ...@@ -250,6 +250,14 @@ def dicoViewEn(request):
data.dicoLang = "en" data.dicoLang = "en"
return render(request, 'editDico.html', {'data': data}) return render(request, 'editDico.html', {'data': data})
def dicoViewZh(request):
data = Entree()
data.update = updateTime()
data.lenDic = txtphono.getLenDicZh()
data.logStat = txtphono.getLogStat('zh')
data.dicoLang = "zh"
return render(request, 'editDico.html', {'data': data})
def dicoReq(request): def dicoReq(request):
dicoLang = request.GET.get('dicoLang', None) dicoLang = request.GET.get('dicoLang', None)
mot = request.GET.get('mot', None) mot = request.GET.get('mot', None)
...@@ -271,34 +279,31 @@ def get_client_ip(request): ...@@ -271,34 +279,31 @@ def get_client_ip(request):
return ip return ip
def editEntry(request): def editEntry(request):
mot = request.GET.get('mot', None) colis = json.loads(request.body)
lang = request.GET.get('lang', None) mot = colis['mot']
transList = request.GET.get('transList', None) lang = colis['lang']
infos = colis['infos']
print(infos)
ipCli = get_client_ip(request) ipCli = get_client_ip(request)
rep = {
'reponse': txtphono.modifEntry(ipCli,mot,transList,lang) return JsonResponse(txtphono.modifEntry(ipCli,mot,infos,lang))
}
return JsonResponse(rep)
def newEntry(request): def newEntry(request):
mot = request.GET.get('mot', None) mot = request.GET.get('mot', None)
trans = request.GET.get('trans', None) trans = request.GET.get('trans', None)
lang = request.GET.get('lang', None) lang = request.GET.get('lang', None)
ipCli = get_client_ip(request) ipCli = get_client_ip(request)
rep = {
'reponse': txtphono.addEntry(ipCli,mot,trans,lang) return JsonResponse(txtphono.addEntry(ipCli,mot,trans,lang))
}
return JsonResponse(rep)
def supprEntry(request): def supprEntry(request):
mot = request.GET.get('mot', None) mot = request.GET.get('mot', None)
lang = request.GET.get('lang', None) lang = request.GET.get('lang', None)
ipCli = get_client_ip(request) ipCli = get_client_ip(request)
rep = {
'reponse': txtphono.delEntry(ipCli,mot,lang) return JsonResponse(txtphono.delEntry(ipCli,mot,lang))
}
return JsonResponse(rep)
def getLog(request): def getLog(request):
rep = txtphono.sendMeLogPlease('fr') lang = request.GET.get('lang', None)
return JsonResponse(rep) rep = txtphono.sendMeLogPlease(lang)
\ No newline at end of file return JsonResponse({"logs":rep})
\ No newline at end of file
...@@ -31,8 +31,9 @@ urlpatterns = [ ...@@ -31,8 +31,9 @@ urlpatterns = [
path('app/dico/', colorapp_views.dicoView), path('app/dico/', colorapp_views.dicoView),
path('app/dico/fr', colorapp_views.dicoViewFr), path('app/dico/fr', colorapp_views.dicoViewFr),
path('app/dico/en', colorapp_views.dicoViewEn), path('app/dico/en', colorapp_views.dicoViewEn),
path('app/dico/zh', colorapp_views.dicoViewZh),
path('dicoSearch/', colorapp_views.dicoReq), path('dicoSearch/', colorapp_views.dicoReq),
path('editEntry/', colorapp_views.editEntry), path('editEntry/', csrf_exempt(colorapp_views.editEntry)),
path('addEntry/', colorapp_views.newEntry), path('addEntry/', colorapp_views.newEntry),
path('delEntry/', colorapp_views.supprEntry), path('delEntry/', colorapp_views.supprEntry),
path('getLog/', colorapp_views.getLog) path('getLog/', colorapp_views.getLog)
......
...@@ -96,9 +96,9 @@ var langJson = { ...@@ -96,9 +96,9 @@ var langJson = {
"zh": "" "zh": ""
}, },
"sp_infoWiki": { "sp_infoWiki": {
"fr": "WikiColor est un coloriseur automatique de texte basé sur les transcriptions phonétiques du Wiktionnaire et de la collaboration de nombreux utilisateurs. Vous pouvez consulter et participer à l'amélioration du dictionnaire en cliquant sur le bouton ci-dessous. L'alignement phono-graphémique reste encore expérimental, toute suggestion de votre part est bienvenue ! N'hésitez pas à visiter <a href='https://alem.hypotheses.org/outils-alem-app/wikicolor'>notre site</a>, et participez aux discussions sur <a href='https://groups.google.com/forum/#!forum/alem-app'>le forum</a>.", "fr": "WikiColor est un coloriseur automatique de texte basé sur les transcriptions phonétiques du Wiktionnaire, du <a href='https://repository.ortolang.fr/api/content/morphalou/2/LISEZ_MOI.html' target='_blank'>lexique Morphalou3.1</a> et de la collaboration de nombreux utilisateurs. Vous pouvez consulter et participer à l'amélioration du dictionnaire en cliquant sur le bouton ci-dessous. L'alignement phono-graphémique reste encore expérimental, toute suggestion de votre part est bienvenue ! N'hésitez pas à visiter <a href='https://alem.hypotheses.org/outils-alem-app/wikicolor'>notre site</a>, et participez aux discussions sur <a href='https://groups.google.com/forum/#!forum/alem-app'>le forum</a>.",
"en": "WikiColor is an automatic text coloriser based on Wiktionary's phonetic transcriptions, CMU Pronunciation Dictionary and collaboration of our users community. English dictionary isn't editable yet, but it will be soon! Phono-graphemic alignment still is experimental, especially for English, any suggestion from you will be welcome! We are still working on the phonological representation of English, which is a big challenge. Please let us know your suggestions in <a href='https://groups.google.com/forum/#!forum/alem-app'>our Forum</a>. We also have <a href='https://alem.hypotheses.org/outils-alem-app/wikicolor'>a website</a> (English content will come soon!).", "en": "WikiColor is an automatic text coloriser based on Wiktionary's phonetic transcriptions, <a href='http://www.speech.cs.cmu.edu/cgi-bin/cmudict/?in=pronunciation&stress=-s' target='_blank'>CMU Pronouncing Dictionary</a>, <a href='https://github.com/JoseLlarena/Britfone' target='_blank'>Britfone Pronunciation Dictionary</a> and collaboration of our users community. Please consider to add words and spellings to our word base! Phono-graphemic alignment still is experimental, especially for English, any suggestion from you will be welcome! We are still working on the phonological representation of English, which is a big challenge. Please let us know your suggestions in <a href='https://groups.google.com/forum/#!forum/alem-app'>our Forum</a>. We also have <a href='https://alem.hypotheses.org/outils-alem-app/wikicolor'>a website</a> (English content will come soon!).",
"zh": "WikiColor is an automatic text coloriser based on Wiktionary's phonetic transcriptions, CMU Pronunciation Dictionary and collaboration of our users community. English dictionary isn't editable yet, but it will be soon! Phono-graphemic alignment still is experimental, especially for English, any suggestion from you will be welcome! We are still working on the phonological representation of English, which is a big challenge. Please let us know your suggestions in <a href='https://groups.google.com/forum/#!forum/alem-app'>our Forum</a>. We also have <a href='https://alem.hypotheses.org/outils-alem-app/wikicolor'>a website</a> (English content will come soon!)." "zh": "WikiColor is an automatic text coloriser based on <a href='https://cc-cedict.org/wiki/' target='_blank'>the CC-CEdict Dictionnary</a>, the Python phonetiser <a href='https://pypi.org/project/pinyin/' target='_blank'>Pinyin0.4.0</a> and collaboration of our users community. Please consider to add words and spellings to our word base! Please let us know your suggestions in <a href='https://groups.google.com/forum/#!forum/alem-app'>our Forum</a>. We also have <a href='https://alem.hypotheses.org/outils-alem-app/wikicolor'>a website</a> (English content will come soon!)."
}, },
"sp_btnEditDico": { "sp_btnEditDico": {
"fr": "Consulter/éditer le dictionnaire", "fr": "Consulter/éditer le dictionnaire",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment