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

finalisation v5.0

parent a4b9c825
# -*- 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 collections import OrderedDict
......@@ -26,8 +27,6 @@ api2classFile = "../phon2graph/data/api2class.json" # MANDARIN
########################################
########## CONNEXION MONGODB ###########
########################################
import pymongo
def connexion():
mdp = ""
with open('../private/dbmdp','r') as infile:
......@@ -210,10 +209,19 @@ def traitement(mot, lang, liaison): # LIAISON : avec le caractere liaison en arg
print("'", mot, "' trouvé dans le dico !",word2trans[mot])
if lang in ["fr","en"]:
transList = []
for trans, infos in word2trans[mot]['t'].items():
transList.append((trans.replace("_",""),infos['r'])) # SUPPRESSION SYMBOLE SYLLABE ICI '_'
print("TRANS",trans, trans.replace("_",""))
print("LOCS", infos['r'])
shortmem = [] # pour pas afficher plusieurs fois un même alignement (avec et sans syllabation typiquement; le visuel est identique)
for trans in word2trans[mot]['t']:
newT = trans['tt'].replace('.','')
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 = []
############ partie d'appel de la fonction denasalisation
......@@ -262,7 +270,7 @@ def traitementzh(mot):
result = [] # liste type : [[car, api, phonlist, ton], [car, api, phonlist, ton]...]
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)
else:
......@@ -295,9 +303,14 @@ def getEntryByWord(m,mc,t,tc,lang):
if lang == "fr":
word2trans = word2transFr
m = m.replace("'",'’')
dico = dicoFr
elif lang == "en":
word2trans = word2transEn
m = m.replace('’',"'")
dico = dicoEn
elif lang == "zh":
word2trans = word2transZh
dico = dicoZh
m = m.lower()
t = t.replace('.','')
......@@ -321,39 +334,62 @@ def getEntryByWord(m,mc,t,tc,lang):
if tc == 'finit par' or tc == 'est égal à':
tc2 = r'$'
for entree,transList in word2trans.items():
if re.match(mc1+m+mc2,entree):
# for entree, infos in word2trans.items():
# if re.match(mc1+m+mc2,entree):
# transOK = False
# for i,trans in enumerate(infos['t']):
# if re.match(tc1+t+tc2,trans['t'].replace('.','')):
# transOK = True
# 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(list(transList['t'].keys())):
if re.match(tc1+t+tc2,trans.replace('.','')):
for i,trans in enumerate(entree['t']):
if re.match(tc1+t+tc2,trans['tt'].replace('.','')):
transOK = True
if transOK: result[entree] = transList
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)
def modifEntry(user,mot,newTransList,lang):
def modifEntry(user,mot,infos,lang):
if lang == "fr":
word2trans = word2transFr
dico = dicoFr
if lang == "en":
word2trans = word2transEn
dico = dicoEn
if lang == "zh":
word2trans = word2transZh
dico = dicoZh
newTransList = newTransList.split(',')
for ind,i in enumerate(newTransList):
newTransList[ind] = i.strip()
oldTransList = word2trans[mot]
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 !"
oldInfos = dico.find_one({'w':mot})
elif lang == "en":
word2trans = word2transEn
return "La modification du dictionnaire anglais n'est pas encore possible."
dico.update({"w":mot}, {"$set": infos})
word2trans[mot]['t'] = infos['t']
newlog = writeLog(Type="MODIF", user=user, mot=mot, oldMot=oldInfos, newMot=infos, lang=lang)
print("Modification du dictionnaire par",user,": lang =",lang,"; mot =",mot,"; oldMot =",oldInfos,"; newMot =",infos)
print("ICIIII newlog:",newlog)
return {"msg":"Modification effectuée avec succès !", "newlog":newlog}
def addEntry(user,mot,trans,lang):
if lang == "fr":
word2trans = word2transFr
dico = dicoFr
trans = trans.replace('g','ɡ')
trans = trans.replace('r','ʁ')
if lang == "en":
word2trans = word2transEn
dico = dicoEn
trans = trans.replace('g','ɡ')
if lang == "zh":
word2trans = word2transZh
dico = dicoZh
mot = mot.lower()
mot = mot.strip()
transList = trans.split(',')
......@@ -362,96 +398,85 @@ def addEntry(user,mot,trans,lang):
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 !"
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:
return "Ce mot est déjà dans le dictionnaire !"
newMot['t'].append({ 'tt':t, 'tr':[]})
elif lang == "en":
word2trans = word2transEn
return "La modification du dictionnaire anglais n'est pas encore possible."
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):
if lang == "fr":
word2trans = word2transFr
dico = dicoFr
if lang == "en":
word2trans = word2transEn
dico = dicoEn
if lang == "zh":
word2trans = word2transZh
dico = dicoZh
if mot in word2trans.keys():
oldmot = word2trans[mot]
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")
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 "Suppression effectuée avec succès."
elif lang == "en":
word2trans = word2transEn
return "La modification du dictionnaire anglais n'est pas encore possible."
def rewriteDico(lang):
if lang == "fr":
word2trans = word2transFr
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":
word2trans = word2transEn
return "La modification du dictionnaire anglais n'est pas encore possible."
return {"msg":"Suppression effectuée avec succès.", "newlog":newlog}
def sendMeLogPlease(lang):
if lang == "fr":
return logDicFr
logDic = dicoFrLogs
elif lang == "en":
return logDicEn
logDic = dicoEnLogs
elif lang == "zh":
logDic = dicoZhLogs
def writeLog(Type,user,mot,oldTransList,newTransList,lang):
if lang == "fr":
logDic = logDicFr
elif lang == "en":
logDic = logDicEn
rep = []
for log in logDic.find():
rep.append(noIdDict(log))
now = datetime.datetime.now()
date = "{}-{}-{} {}:{}:{}".format(now.year, now.month, now.day, now.hour, now.minute, now.second)
return rep
if mot not in logDic.keys(): logDic[mot] = []
logDic[mot].append({
"date":date,
"user":user,
"oldTransList":oldTransList,
"newTransList":newTransList,
"Type":Type
})
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
with tempfile.NamedTemporaryFile(dir='.', delete=False, mode="w") as tmp:
json.dump(logDic, tmp, ensure_ascii=False, indent=4)
def writeLog(Type,user,mot,oldMot,newMot,lang):
if lang == "fr":
os.replace(tmp.name, logFile)
logDic = dicoFrLogs
elif lang == "en":
os.replace(tmp.name, logFileEn)
print("Log file updated.")
logDic = dicoEnLogs
elif lang == "zh":
logDic = dicoZhLogs
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
with tempfile.NamedTemporaryFile(dir='.', delete=False, mode="w") as tmp:
json.dump(logBug, tmp, ensure_ascii=False, indent=4)
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
newlog = {
"date":datetime.datetime.now(),
"user":user,
"word":mot,
"old":noIdDict(oldMot),
"new":noIdDict(newMot),
"type":Type
}
logDic.insert_one(newlog)
print("New log: lang={} ; user={} ; word={} ; type={}".format(lang,user,mot,Type))
return noIdDict(newlog)
\ No newline at end of file
......@@ -214,7 +214,7 @@ def getPhonoOf(request):
def updateTime():
upd = str(subprocess.check_output(["git", "log", "-1", "--format=%cd", "--date=short"]))
#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):
data = Data()
......@@ -250,6 +250,14 @@ def dicoViewEn(request):
data.dicoLang = "en"
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):
dicoLang = request.GET.get('dicoLang', None)
mot = request.GET.get('mot', None)
......@@ -271,34 +279,31 @@ def get_client_ip(request):
return ip
def editEntry(request):
mot = request.GET.get('mot', None)
lang = request.GET.get('lang', None)
transList = request.GET.get('transList', None)
colis = json.loads(request.body)
mot = colis['mot']
lang = colis['lang']
infos = colis['infos']
print(infos)
ipCli = get_client_ip(request)
rep = {
'reponse': txtphono.modifEntry(ipCli,mot,transList,lang)
}
return JsonResponse(rep)
return JsonResponse(txtphono.modifEntry(ipCli,mot,infos,lang))
def newEntry(request):
mot = request.GET.get('mot', None)
trans = request.GET.get('trans', None)
lang = request.GET.get('lang', None)
ipCli = get_client_ip(request)
rep = {
'reponse': txtphono.addEntry(ipCli,mot,trans,lang)
}
return JsonResponse(rep)
return JsonResponse(txtphono.addEntry(ipCli,mot,trans,lang))
def supprEntry(request):
mot = request.GET.get('mot', None)
lang = request.GET.get('lang', None)
ipCli = get_client_ip(request)
rep = {
'reponse': txtphono.delEntry(ipCli,mot,lang)
}
return JsonResponse(rep)
return JsonResponse(txtphono.delEntry(ipCli,mot,lang))
def getLog(request):
rep = txtphono.sendMeLogPlease('fr')
return JsonResponse(rep)
\ No newline at end of file
lang = request.GET.get('lang', None)
rep = txtphono.sendMeLogPlease(lang)
return JsonResponse({"logs":rep})
\ No newline at end of file
......@@ -31,8 +31,9 @@ urlpatterns = [
path('app/dico/', colorapp_views.dicoView),
path('app/dico/fr', colorapp_views.dicoViewFr),
path('app/dico/en', colorapp_views.dicoViewEn),
path('app/dico/zh', colorapp_views.dicoViewZh),
path('dicoSearch/', colorapp_views.dicoReq),
path('editEntry/', colorapp_views.editEntry),
path('editEntry/', csrf_exempt(colorapp_views.editEntry)),
path('addEntry/', colorapp_views.newEntry),
path('delEntry/', colorapp_views.supprEntry),
path('getLog/', colorapp_views.getLog)
......
......@@ -96,9 +96,9 @@ var langJson = {
"zh": ""
},
"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>.",
"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!).",
"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!)."
"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, <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 <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": {
"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