-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL_regular.py
More file actions
72 lines (54 loc) · 2.09 KB
/
SQL_regular.py
File metadata and controls
72 lines (54 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import re
import pymysql
db = pymysql.Connect(host="localhost", user="user", passwd="pass", db="db", charset="utf8")
select_rus = 'SELECT id, `header` FROM page_translation WHERE localize_id=40'
select_tur = 'SELECT id, `header` FROM page_translation WHERE localize_id=38'
select_eng = 'SELECT id, `header` FROM page_translation WHERE localize_id=39'
update = "UPDATE news_article_translation SET body='{0}' WHERE id={1}"
cur = db.cursor()
cur.execute(select_rus)
txt_rus = [row for row in cur.fetchall()]
cur.execute(select_tur)
txt_tur = [row for row in cur.fetchall()]
cur.execute(select_eng)
txt_eng = [row for row in cur.fetchall()]
space = u"\w+\s+"
end_of_word = u"\w+"
rus_search_pattern = re.compile(u"regular".format(space, end_of_word), re.UNICODE + re.I)
tur_search_pattern = re.compile(u"regular".format(space, end_of_word), re.UNICODE + re.I)
eng_search_pattern = re.compile(u"regular".format(space, end_of_word), re.UNICODE + re.I)
def Get_Repl_Strings(found_str):
old_str = found_str
old = old_str.split()[1:]
new_str = list(" ".join(old))
new_str[0] = new_str[0].upper()
return old_str, "".join(new_str)
def ShowReplaces(search_res):
for i in set(search_res):
old_str, new_str = Get_Repl_Strings(i)
print( old_str + " => " + new_str)
def MakeReplaces(search_res, txt):
for i in set(search_res):
old_str, new_str = Get_Repl_Strings(i)
txt =txt.replace(old_str, new_str)
return txt
def main(txt_lang, lang_search_pattern):
for i in txt_lang:
try:
lang_search_res = re.findall(lang_search_pattern, i[1])
except TypeError:
pass
if len(lang_search_res) != 0:
txt = MakeReplaces(lang_search_res, i[1])
print(i[0])
cur.execute(update.format(db.escape_string(txt), i[0]))
db.commit()
def Show(txt_lang, lang_search_pattern):
for i in txt_lang:
try:
lang_search_res = re.findall(lang_search_pattern, i[1])
except TypeError: pass
ShowReplaces(lang_search_res)
main(txt_rus, rus_search_pattern)
main(txt_eng, eng_search_pattern)
main(txt_tur, tur_search_pattern)