Advertisement
Guest User

Untitled

a guest
Aug 1st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.72 KB | None | 0 0
  1. @task(bind=True)
  2. def get_words_to_translate_whole_text_logic(self, user_id, request_ids, request_text, request_first_lang, request_second_lang, request_text_id):
  3.     found_words = []
  4.     temp_user = get_object_or_404(User, pk=user_id)
  5.     admin_user = get_object_or_404(User, pk=1)
  6.     temp_first_lang=get_object_or_404(Language, abbrlanguage=request_first_lang)
  7.     temp_second_lang=get_object_or_404(Language, abbrlanguage=request_second_lang)
  8.     text = request_text
  9.     id_text = request_text_id
  10.     #print(id_text)
  11.     regex2 = r"([^\.\?\!\,\;\:\(\)\"\'\ \/\“\”]+|[\.\?\!\,\;\:\(\)\"\'\/\“\”]+)"
  12.     regex3 = r"[^\.\?\!\,\;\:\(\)\"\'\ \/\“\”\[\]\d]+"
  13.     ids = request_ids
  14.     cnx = mysql.connector.connect(user=settings.DATABASES['default']['USER'], database=settings.DATABASES['default']['NAME'], password=settings.DATABASES['default']['PASSWORD'])
  15.     cursor = cnx.cursor(buffered=True)
  16.     if user_id not in ids:
  17.         ids.append(str(user_id))
  18.     ids = ",".join(ids)
  19.     #textLC = text.lower()
  20.     combine_text=""
  21.     words_for_sql=""
  22.     simbols = {' ':"", '\\':"", '/':"", '.':"", ',':"", ':':"", ';':"", '!':"", '?':"", '\'':"", '\"':"", '[':"", ']':"", '{':"", '}':"", '(':"", ')':""}
  23.     authors_translation = AuthorsTranslation.objects.filter(text_id=id_text)
  24.     for tr in authors_translation:
  25.         text = text.replace(tr.sentence, tr.translation)
  26.     text = re.sub('\[img .*?\]', '', text)
  27.     #arr_words_for_find=""
  28.     i=0
  29.     t1 = time.time()
  30.     while i < len(text):
  31.         #x1=time.time()
  32.         self.update_state(state='PROGRESS', meta={'current': i, 'total': len(text)})
  33.         arr_words_for_find=[]
  34.         x=0
  35.         while i+x <len(text) and len(arr_words_for_find) != 4:
  36.             while i+x< len(text) and text[i+x] not in simbols:#" \/.,:;!?\'\"[]{}()".find(text[i+x]) == -1:
  37.                 words_for_sql += text[i+x]#считать индекс а потом брать подстроку
  38.                 x += 1
  39.             arr_words_for_find.append(words_for_sql)
  40.             if i+x<len(text):
  41.                 words_for_sql += text[i+x]
  42.             x+=1
  43.         words_for_sql=""
  44.         for c in arr_words_for_find:
  45.             words_for_sql+="'"+c.lower()+"',"
  46.         #x2=time.time()
  47.         #print("make combinations: ", x2-x1)
  48.         #c1=time.time()
  49.         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")
  50.         #print(query2)
  51.         cursor.execute(query2)
  52.         #c2=time.time()
  53.         #print("execute sql: ", c2-c1)
  54.         #v1=time.time()
  55.         if cursor.rowcount==0:
  56.             if i+len(arr_words_for_find[0]) < len(text):
  57.                 combine_text+=arr_words_for_find[0]+text[i+len(arr_words_for_find[0])]
  58.             else:
  59.                 combine_text+=arr_words_for_find[0]
  60.             #print(arr_words_for_find[0])
  61.             i+=len(arr_words_for_find[0])+1
  62.             #print(combine_text)
  63.         else:
  64.             for word in cursor:
  65.                 if i+len(word[0]) < len(text):
  66.                     combine_text+=word[1]+text[i+len(word[0])]
  67.                 else:
  68.                     combine_text+=word[1]
  69.                 i+=len(word[0])+1
  70.                 #print(combine_text)
  71.         words_for_sql=""
  72.         #v2=time.time()
  73.         #print("combine text: ",v2-v1)
  74.     t2 = time.time()
  75.     print("common time: ", t2-t1)
  76.     return combine_text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement