Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @task(bind=True)
- def get_words_to_translate_whole_text_logic(self, user_id, request_ids, request_text, request_first_lang, request_second_lang, request_text_id):
- found_words = []
- temp_user = get_object_or_404(User, pk=user_id)
- admin_user = get_object_or_404(User, pk=1)
- temp_first_lang=get_object_or_404(Language, abbrlanguage=request_first_lang)
- temp_second_lang=get_object_or_404(Language, abbrlanguage=request_second_lang)
- text = request_text
- id_text = request_text_id
- #print(id_text)
- regex2 = r"([^\.\?\!\,\;\:\(\)\"\'\ \/\“\”]+|[\.\?\!\,\;\:\(\)\"\'\/\“\”]+)"
- regex3 = r"[^\.\?\!\,\;\:\(\)\"\'\ \/\“\”\[\]\d]+"
- ids = request_ids
- cnx = mysql.connector.connect(user=settings.DATABASES['default']['USER'], database=settings.DATABASES['default']['NAME'], password=settings.DATABASES['default']['PASSWORD'])
- cursor = cnx.cursor(buffered=True)
- if user_id not in ids:
- ids.append(str(user_id))
- ids = ",".join(ids)
- #textLC = text.lower()
- combine_text=""
- words_for_sql=""
- simbols = {' ':"", '\\':"", '/':"", '.':"", ',':"", ':':"", ';':"", '!':"", '?':"", '\'':"", '\"':"", '[':"", ']':"", '{':"", '}':"", '(':"", ')':""}
- authors_translation = AuthorsTranslation.objects.filter(text_id=id_text)
- for tr in authors_translation:
- text = text.replace(tr.sentence, tr.translation)
- text = re.sub('\[img .*?\]', '', text)
- #arr_words_for_find=""
- i=0
- t1 = time.time()
- while i < len(text):
- #x1=time.time()
- self.update_state(state='PROGRESS', meta={'current': i, 'total': len(text)})
- arr_words_for_find=[]
- x=0
- while i+x <len(text) and len(arr_words_for_find) != 4:
- while i+x< len(text) and text[i+x] not in simbols:#" \/.,:;!?\'\"[]{}()".find(text[i+x]) == -1:
- words_for_sql += text[i+x]#считать индекс а потом брать подстроку
- x += 1
- arr_words_for_find.append(words_for_sql)
- if i+x<len(text):
- words_for_sql += text[i+x]
- x+=1
- words_for_sql=""
- for c in arr_words_for_find:
- words_for_sql+="'"+c.lower()+"',"
- #x2=time.time()
- #print("make combinations: ", x2-x1)
- #c1=time.time()
- query2 = ("select dic.word, dic.translation tr_prim from mainapp_dictionary dic where dic.word in ("+words_for_sql.strip(',')+") and dic.first_lang_id="+str(temp_first_lang.id)+" and dic.second_lang_id="+str(temp_second_lang.id)+" and dic.user_id in ("+ids+") order by CHAR_LENGTH(replace(dic.word,' ',''))-CHAR_LENGTH(dic.word), CHAR_LENGTH(replace(dic.word,'-',''))-CHAR_LENGTH(dic.word), -CHAR_LENGTH(dic.word), dic.word, -dic.is_primary limit 1")
- #print(query2)
- cursor.execute(query2)
- #c2=time.time()
- #print("execute sql: ", c2-c1)
- #v1=time.time()
- if cursor.rowcount==0:
- if i+len(arr_words_for_find[0]) < len(text):
- combine_text+=arr_words_for_find[0]+text[i+len(arr_words_for_find[0])]
- else:
- combine_text+=arr_words_for_find[0]
- #print(arr_words_for_find[0])
- i+=len(arr_words_for_find[0])+1
- #print(combine_text)
- else:
- for word in cursor:
- if i+len(word[0]) < len(text):
- combine_text+=word[1]+text[i+len(word[0])]
- else:
- combine_text+=word[1]
- i+=len(word[0])+1
- #print(combine_text)
- words_for_sql=""
- #v2=time.time()
- #print("combine text: ",v2-v1)
- t2 = time.time()
- print("common time: ", t2-t1)
- return combine_text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement