-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_project_adder.py
More file actions
executable file
·165 lines (143 loc) · 5.98 KB
/
Copy pathmath_project_adder.py
File metadata and controls
executable file
·165 lines (143 loc) · 5.98 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import re
import toolforge
import pywikibot as pw
import helpers
enwiki = pw.Site('en', 'wikipedia')
hywiki = pw.Site('hy', 'wikipedia')
enconn = toolforge.connect('enwiki')
hyconn = toolforge.connect('hywiki')
en_query = '''WITH MathTitle AS (
SELECT p.page_title
FROM page p
JOIN categorylinks cl ON cl.cl_from = p.page_id
JOIN linktarget lt ON lt.lt_id = cl.cl_target_id
WHERE lt.lt_title IN (
'High-priority_mathematics_articles',
'Low-priority_mathematics_articles',
'Mid-priority_mathematics_articles',
'NA-priority_mathematics_articles',
'Top-priority_mathematics_articles',
'Unknown-priority_mathematics_articles'
)
AND lt.lt_namespace = 14
LIMIT 30000
)
SELECT DISTINCT ll.ll_title, p1.page_title
FROM page p1
JOIN langlinks ll ON ll.ll_from = p1.page_id
WHERE p1.page_namespace = 0
AND ll.ll_lang = 'hy'
AND EXISTS (
SELECT 1
FROM MathTitle mt
WHERE mt.page_title = p1.page_title
);'''
hy_query = '''SELECT p.page_title
FROM page p
JOIN categorylinks cl ON cl.cl_from = p.page_id
JOIN linktarget lt ON lt.lt_id = cl.cl_target_id
WHERE lt.lt_title = 'Մաթեմատիկական_հոդվածներ'
AND lt.lt_namespace = 14
LIMIT 30000;'''
fields = {
'algebra': 'հանրահաշիվ',
'analysis': 'անալիզ',
'applied': 'կիրառական',
'applied mathematics': 'կիրառական',
'basics': 'տարրական',
'discrete': 'դիսկրետ',
'discrete mathematics': 'դիսկրետ',
'foundations': 'հիմքեր',
'foundations, logic, and set theory': 'հիմքեր, տրամաբանություն և բազմությունների տեսություն',
'logic': 'բազմությունների տեսություն',
'set theory': 'տրամաբանություն',
'general': 'ընդհանուր',
'geometry': 'երկրաչափություն',
'history': 'պատմություն',
'mathematical physics': 'ֆիզիկա',
'mathematician': 'մաթեմատիկոս',
'mathematicians': 'մաթեմատիկոս',
'number theory': 'թվերի տեսություն',
'probability': 'հավանականություն',
'probability and statistics': 'հավանականություն և վիճակագրություն',
'statistics': 'վիճակագրություն',
'topology': 'տոպոլոգիա'
}
priorities = {
'top': 'բարձրագույն',
'high': 'բարձր',
'mid': 'միջին',
'low': 'ցածր'
}
def create_hy_template(text):
templates = r'(Maths rating|Math rating|Maths rating small|Mathrating|WikiProject Maths|WikiProject Math|WP Maths|WPMath|WikiProject Mathematics rating|WPMATHEMATICS)'
m = re.search(r'(\{\{' + templates + r'[^}]+\}\})', text, re.IGNORECASE)
if m:
template = m.group(0).replace('\n', '')
priority_reg = r'\{\{' + templates + r'.*\| *priority *= *([^|}]+).*\}\}'
priority = re.sub(priority_reg, r'\2', template, flags=re.IGNORECASE)
if priority == template:
priority = ''
field_reg = r'\{\{' + templates + r'.*\| *field *= *([^|}]+).*\}\}'
field = re.sub(field_reg, r'\2', template, flags=re.IGNORECASE)
if field == template:
field = ''
vital_reg = r'\{\{' + templates + r'.*\| *vital *= *([^|}]+).*\}\}'
vital = re.sub(vital_reg, r'\2', template, flags=re.IGNORECASE)
if vital == template:
vital = ''
importance_reg = r'\{\{' + templates + r'.*\| *importance *= *([^|}]+).*\}\}'
importance = re.sub(importance_reg, r'\2', template, flags=re.IGNORECASE)
if importance == template:
importance = ''
fieldhy = ''
if field.lower().strip() in fields:
fieldhy = ' | բնագավառ = ' + fields[field.lower().strip()]
priorityhy = ''
if importance.lower().strip() in priorities:
priorityhy = ' | կարևորություն = ' + priorities[importance.lower().strip()]
if priority.lower().strip() in priorities:
priorityhy = ' | կարևորություն = ' + priorities[priority.lower().strip()]
vitalhy = ''
if vital:
vitalhy = ' | կարևորագույն = ' + 'այո'
hytemplate = '{{Վիքինախագիծ Մաթեմատիկա' + fieldhy + priorityhy + vitalhy + '}}'
return hytemplate
return None
hy_en_map = {}
with enconn.cursor() as cur:
cur.execute(en_query)
results = cur.fetchall()
for r in results:
hy_title = helpers.get_cell_txt(r[0])
en_title = helpers.get_cell_txt(r[1])
hy_en_map[hy_title] = en_title
already_has = set()
with hyconn.cursor() as cur:
cur.execute(hy_query)
results = cur.fetchall()
for r in results:
already_has.add(helpers.get_cell_txt(r[0]))
for hy_title in hy_en_map:
if hy_title not in already_has:
hy_page = pw.Page(hywiki, hy_title)
en_talk_page = pw.Page(enwiki, 'Talk:' + hy_en_map[hy_title])
if not en_talk_page or not en_talk_page.exists():
continue
hy_template = create_hy_template(en_talk_page.text)
if not hy_template:
continue
hy_page_talk = hy_page.toggleTalkPage()
hy_page_talk.text = re.sub(r'\{\{Վիքինախագիծ Մաթեմատիկա}}\n?', '', hy_page_talk.text)
if '{{Վիքինախագիծ Մաթեմատիկա' not in hy_page_talk.text:
hy_page_talk.text = hy_template + '\n' + hy_page_talk.text
hy_page_talk.save(
summary='+' + hy_template + ', ըստ [[Special:PermaLink/7367323#{{Վիքինախագիծ_Մաթեմատիկա}}_կաղապարը_քննարկման_էջերում]]')
watchlist = pw.Page(hywiki, 'Վիքինախագիծ:Մաթեմատիկա/Հոդվածներ')
cat = pw.Category(hywiki, 'Կատեգորիա:Մաթեմատիկական հոդվածներ')
watchlist.text = ''
for talk in cat.members():
page = talk.toggleTalkPage()
if page.namespace() == 0:
watchlist.text += f'[[{page.title()}]]-[[{talk.title()}]] '
watchlist.save('թարմացում')