Skip to content

Commit

Permalink
gggg
Browse files Browse the repository at this point in the history
  • Loading branch information
AthulyaMS committed Jan 8, 2024
1 parent b26122d commit bfd1445
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 114 deletions.
30 changes: 14 additions & 16 deletions app/crud/nlp_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def get_project_tokens(db_:Session, project_id, books, sentence_id_range, senten
raise NotAvailableException(f"Project with id, {project_id}, not found")
sentences = projects_crud.obtain_project_source(db_, project_id, books, sentence_id_range,
sentence_id_list)
# sentences = sentences['db_content']
sentences = sentences['db_content']
args = {"db_":db_, "src_language":project_row.sourceLanguage, "sentence_list":sentences,
'trg_language':project_row.targetLanguage,
"use_translation_memory":use_translation_memory, "include_phrases":include_phrases,
Expand All @@ -93,12 +93,12 @@ def get_project_tokens(db_:Session, project_id, books, sentence_id_range, senten
args['stopwords'] = project_row.metaData['stopwords']
if "punctuations" in project_row.metaData:
args['punctuations'] = project_row.metaData['punctuations']
return get_generic_tokens( **args)
# response = {
# 'db_content':get_generic_tokens( **args),
# 'project_content':project_row
# }
# return response
# return get_generic_tokens( **args)
response = {
'db_content':get_generic_tokens( **args),
'project_content':project_row
}
return response

###################### Token replacement translation ######################
def replace_bulk_tokens_gloss_list(token_translations, updated_sentences):
Expand Down Expand Up @@ -793,7 +793,7 @@ def project_suggest_translations(db_:Session, project_id, books, #pylint: disabl
raise NotAvailableException(f"Project with id, {project_id}, not found")
draft_rows = projects_crud.obtain_project_source(db_, project_id, books, sentence_id_range,
sentence_id_list, with_draft=True)
# draft_rows = draft_rows['db_content']
draft_rows = draft_rows['db_content']
if confirm_all:
for row in draft_rows:
for i, meta in enumerate(row.draftMeta):
Expand All @@ -812,14 +812,12 @@ def project_suggest_translations(db_:Session, project_id, books, #pylint: disabl
updated_drafts = auto_translate(**args)
db_.add_all(updated_drafts)
project_row.updatedUser = user_id
db_.commit()
# project_row.updateTime = datetime.now(ist_timezone).strftime('%Y-%m-%d %H:%M:%S')
# response = {
# 'db_content':updated_drafts,
# 'project_content':project_row
# }
# return response
return updated_drafts
project_row.updateTime = datetime.now(ist_timezone).strftime('%Y-%m-%d %H:%M:%S')
response = {
'db_content':updated_drafts,
'project_content':project_row
}
return response

def edit_glossary(db_: Session,token_info):
'''updates the given information of a gloss in db'''
Expand Down
185 changes: 87 additions & 98 deletions app/crud/projects_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import re
import json
# import datetime
import datetime
from functools import wraps
from pytz import timezone
from sqlalchemy import or_
Expand Down Expand Up @@ -169,45 +169,6 @@ def update_translation_project_uploaded_book(db_,project_obj,project_id,new_book
draftMeta=item['draftMeta'],
updatedUser=user_id))

def update_translation_project(db_:Session, project_obj, project_id, user_id=None):
'''Either activate or deactivate a project or Add more books to a project,
adding all new verses to the drafts table'''
project_row = db_.query(db_models.TranslationProject).get(project_id)
if not project_row:
raise NotAvailableException(f"Project with id, {project_id}, not found")
new_books = []
if project_obj.sentenceList:
update_translation_project_sentences(db_, project_obj,project_id, new_books, user_id)
if project_obj.uploadedUSFMs:
#uploaded usfm book add to project
update_translation_project_uploaded_book(db_,project_obj,project_id,new_books,user_id)
db_.commit()
# db_.expire_all()
if project_obj.projectName:
project_row.projectName = project_obj.projectName
if project_obj.active is not None:
project_row.active = project_obj.active
if project_obj.useDataForLearning is not None:
project_row.metaData['useDataForLearning'] = project_obj.useDataForLearning
flag_modified(project_row, "metaData")
if project_obj.stopwords:
project_row.metaData['stopwords'] = project_obj.stopwords.__dict__
flag_modified(project_row, "metaData")
if project_obj.punctuations:
project_row.metaData['punctuations'] = project_obj.punctuations
flag_modified(project_row, "metaData")
if project_obj.compatibleWith:
project_row.compatibleWith= project_obj.compatibleWith
project_row.updatedUser = user_id

if len(new_books) > 0:
project_row.metaData['books'] += new_books
flag_modified(project_row, "metaData")
db_.add(project_row)
db_.commit()
db_.refresh(project_row)
return project_row

# def update_translation_project(db_:Session, project_obj, project_id, user_id=None):
# '''Either activate or deactivate a project or Add more books to a project,
# adding all new verses to the drafts table'''
Expand Down Expand Up @@ -247,6 +208,45 @@ def update_translation_project(db_:Session, project_obj, project_id, user_id=Non
# db_.refresh(project_row)
# return project_row

def update_translation_project(db_:Session, project_obj, project_id, user_id=None):
'''Either activate or deactivate a project or Add more books to a project,
adding all new verses to the drafts table'''
project_row = db_.query(db_models.TranslationProject).get(project_id)
if not project_row:
raise NotAvailableException(f"Project with id, {project_id}, not found")
new_books = []
if project_obj.sentenceList:
update_translation_project_sentences(db_, project_obj,project_id, new_books, user_id)
if project_obj.uploadedUSFMs:
#uploaded usfm book add to project
update_translation_project_uploaded_book(db_,project_obj,project_id,new_books,user_id)
# db_.commit()
# db_.expire_all()
if project_obj.projectName:
project_row.projectName = project_obj.projectName
if project_obj.active is not None:
project_row.active = project_obj.active
if project_obj.useDataForLearning is not None:
project_row.metaData['useDataForLearning'] = project_obj.useDataForLearning
flag_modified(project_row, "metaData")
if project_obj.stopwords:
project_row.metaData['stopwords'] = project_obj.stopwords.__dict__
flag_modified(project_row, "metaData")
if project_obj.punctuations:
project_row.metaData['punctuations'] = project_obj.punctuations
flag_modified(project_row, "metaData")
if project_obj.compatibleWith:
project_row.compatibleWith= project_obj.compatibleWith
project_row.updatedUser = user_id

if len(new_books) > 0:
project_row.metaData['books'] += new_books
flag_modified(project_row, "metaData")
db_.add(project_row)
db_.commit()
db_.refresh(project_row)
return project_row



# pylint: disable=duplicate-code
Expand Down Expand Up @@ -406,33 +406,29 @@ def obtain_project_draft(db_:Session, project_id, books, sentence_id_list, sente
raise NotAvailableException(f"Project with id, {project_id}, not found")
draft_rows = obtain_project_source(db_, project_id, books, sentence_id_list,
sentence_id_range, with_draft=True)
# draft_rows = draft_rows['db_content']
draft_rows = draft_rows['db_content']
if output_format == schemas_nlp.DraftFormats.USFM :
draft_out = nlp_crud.create_usfm(draft_rows)
elif output_format == schemas_nlp.DraftFormats.JSON:
draft_out = nlp_crud.export_to_json(project_row.sourceLanguage,
project_row.targetLanguage, draft_rows, None)
# elif output_format == schemas_nlp.DraftFormats.JSON:
# draft_out = nlp_crud.export_to_json(project_row.sourceLanguage,
# project_row.targetLanguage, draft_rows, None)
elif output_format == schemas_nlp.DraftFormats.PRINT:
draft_out = nlp_crud.export_to_print(draft_rows)
else:
raise TypeException(f"Unsupported output format: {output_format}")
# response = {
# 'db_content':draft_out,
# 'project_content':project_row
# }
return draft_out
response = {
'db_content':draft_out,
'project_content':project_row
}
return response

def update_project_draft(db_:Session, project_id, sentence_list, user_id):
'''Directly write to the draft and draftMeta fields of project sentences'''
sentence_id_list = [sent.sentenceId for sent in sentence_list]
source_resp = obtain_project_source(db_, project_id,
sentence_id_list=sentence_id_list, with_draft=True)
# project_row = source_resp['project_content']
project_row = db_.query(db_models.TranslationProject).get(project_id)
if not project_row:
raise NotAvailableException(f"Project with id, {project_id}, not found")
sentences = source_resp
# sentences = source_resp['db_content']
project_row = source_resp['project_content']
sentences = source_resp['db_content']
for input_sent in sentence_list:
sent = None
for read_sent in sentences:
Expand All @@ -448,11 +444,11 @@ def update_project_draft(db_:Session, project_id, sentence_list, user_id):
sent.draftMeta = input_sent.draftMeta
sent.updatedUser = user_id
project_row.updatedUser = user_id
# project_row.updateTime = datetime.datetime.now(ist_timezone).strftime('%Y-%m-%d %H:%M:%S')
# response_result = {
# 'db_content':sentences,
# 'project_content':project_row
# }
project_row.updateTime = datetime.datetime.now(ist_timezone).strftime('%Y-%m-%d %H:%M:%S')
response_result = {
'db_content':sentences,
'project_content':project_row
}
#Also add any new confirmed translations to translation memory
gloss_list = []
for sent in sentences:
Expand All @@ -466,10 +462,7 @@ def update_project_draft(db_:Session, project_id, sentence_list, user_id):
nlp_crud.add_to_translation_memory(db_,
project_row.sourceLanguage.code,
project_row.targetLanguage.code, gloss_list, default_val=1)
# return response_result
db_.commit()
return sentences

return response_result

def obtain_project_progress(db_, project_id, books, sentence_id_list, sentence_id_range):#pylint: disable=too-many-locals
'''Calculate project translation progress in terms of how much of draft is translated'''
Expand All @@ -478,7 +471,7 @@ def obtain_project_progress(db_, project_id, books, sentence_id_list, sentence_i
raise NotAvailableException(f"Project with id, {project_id}, not found")
draft_rows = obtain_project_source(db_, project_id, books, sentence_id_list,
sentence_id_range, with_draft=True)
# draft_rows = draft_rows["db_content"]
draft_rows = draft_rows["db_content"]
confirmed_length = 0
suggestions_length = 0
untranslated_length = 0
Expand All @@ -500,12 +493,11 @@ def obtain_project_progress(db_, project_id, books, sentence_id_list, sentence_i
"suggestion": suggestions_length/total_length,
"untranslated": untranslated_length/total_length}
# return result
# response_result = {
# 'db_content':result,
# 'project_content':project_row
# }
# return response_result
return result
response_result = {
'db_content':result,
'project_content':project_row
}
return response_result

def obtain_project_token_translation(db_, project_id, token, occurrences): # pylint: disable=unused-argument
'''Get the current translation for specific tokens providing their occurence in source'''
Expand All @@ -524,7 +516,6 @@ def obtain_project_token_translation(db_, project_id, token, occurrences): # pyl
with_draft=True)
# draft_rows = draft_rows["db_content"]
translations = pin_point_token_in_draft(occurrences, draft_rows)
print("translations",translations)
# return translations
# response = {
# 'db_content':translations[0],
Expand Down Expand Up @@ -577,12 +568,12 @@ def get_project_source_versification(db_, project_id):
versification_check(row, prev_book_code, versification, prev_verse, prev_chapter)
if prev_book_code is not None:
versification['maxVerses'][prev_book_code].append(prev_verse)
return versification
# response = {
# 'db_content':versification,
# 'project_content':project_row
# }
# return response
# return versification
response = {
'db_content':versification,
'project_content':project_row
}
return response

def get_project_source_per_token(db_:Session, project_id, token, occurrences): #pylint: disable=unused-argument
'''get sentences and drafts for the token, which splits the token & translation in metadraft
Expand All @@ -593,7 +584,7 @@ def get_project_source_per_token(db_:Session, project_id, token, occurrences): #
sent_ids = [occur.sentenceId for occur in occurrences]
draft_rows = obtain_project_source(db_, project_id,
sentence_id_list=sent_ids, with_draft=True)
# draft_rows = draft_rows['db_content']
draft_rows = draft_rows['db_content']
occur_list = []
for occur in occurrences:
occur_list.append(occur.__dict__)
Expand All @@ -605,11 +596,11 @@ def get_project_source_per_token(db_:Session, project_id, token, occurrences): #
draft.draftMeta.append(mta)
# return draft_rows
draft_dicts = [item.__dict__ for item in draft_rows]
# response = {
# 'db_content':draft_dicts,
# 'project_content':project_row
# }
return draft_dicts
response = {
'db_content':draft_dicts,
'project_content':project_row
}
return response

def pin_point_token_in_draft(occurrences, draft_rows):#pylint: disable=too-many-locals,too-many-branches
'''find out token's aligned portion in draft'''
Expand Down Expand Up @@ -773,12 +764,11 @@ def obtain_project_source(db_:Session, project_id, books=None, sentence_id_range
"surrogateId":row.surrogateId,"sentence":row.sentence}
result.append(obj)

# response = {
# 'db_content':result,
# 'project_content':project_row
# }
# return response
return result
response = {
'db_content':result,
'project_content':project_row
}
return response

def remove_project_sentence(db_, project_id, sentence_id,user_id):
'''To remove a sentence'''
Expand All @@ -794,11 +784,10 @@ def remove_project_sentence(db_, project_id, sentence_id,user_id):
# raise PermissionException("A user cannot remove oneself from a project.")
db_.delete(sentence_row)
project_row.updatedUser = user_id
# project_row.updateTime = datetime.datetime.now(ist_timezone).strftime('%Y-%m-%d %H:%M:%S')
db_.commit()
# response = {
# "db_content": sentence_row,
# "project_content": project_row
# }
# return response
return sentence_row
project_row.updateTime = datetime.datetime.now(ist_timezone).strftime('%Y-%m-%d %H:%M:%S')
# db_.commit()
response = {
"db_content": sentence_row,
"project_content": project_row
}
return response

0 comments on commit bfd1445

Please sign in to comment.