-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdumpquery.py
More file actions
144 lines (123 loc) · 4.16 KB
/
Copy pathdumpquery.py
File metadata and controls
144 lines (123 loc) · 4.16 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
import csv
import sys
import re
from itertools import islice
from collections import defaultdict, Counter
from pywikibot.xmlreader import XmlDump
import mwparserfromhell
def main():
arg = sys.argv[1]
# search_report(iter_pages(arg), sys.argv[2])
# comments_report(iter_pages(arg))
# mixed_names_report(iter_pages(arg))
# sections_report(iter_pages(arg))
words_report(iter_pages(arg))
def words_report(pages):
words_counts = Counter()
for page in pages:
if (page.ns != '0') or page.isredirect:
continue
try:
text = mwparserfromhell.parse(page.text).strip_code()
except Exception as e:
print(page.title, e)
continue
# Ukrainian:
# text = text.replace('\u0301', '') # remove accents
# page_words = re.findall(
# r'[абвгґдеєжзиіїйклмнопрстуфхцчшщьюя'
# r'АБВГҐДЕЄЖЗИІЇЙКЛМНОПРСТУФХЦЧШЩЬЮЯ’\'-]+',
# text
# )
# Any language:
page_words = re.findall(r'\b[^\W\d]+\b', text)
words_counts.update(x.lower() for x in page_words)
csvwriter = csv.writer(sys.stdout)
for item in words_counts.most_common():
csvwriter.writerow(item)
def sections_report(pages):
sections = defaultdict(list)
existing_pages = set()
links = Counter()
for page in pages:
if page.ns != '0':
continue
existing_pages.add(page.title)
code = mwparserfromhell.parse(page.text)
for h in code.filter_headings():
sections[normalize(h.title)].append(page.title)
for l in code.filter_wikilinks():
links[normalize(l.title.split('#')[0])] += 1
for link, frequency in links.most_common():
if link in existing_pages:
continue
if link not in sections:
continue
if frequency < 2:
continue
if len(sections[link]) > 5:
continue
print(f'* [[{link}]] ({frequency})')
for page in sections[link]:
print(f'** [[{page}#{link}]]')
def normalize(title):
s = title.strip()
if not s:
return ''
return s[0].upper() + s[1:]
def search_report(pages, substring):
for page in pages:
for line in page.text.splitlines():
if substring in line:
print(f'[[{page.title}]]:', line)
def mixed_names_report(pages):
print('{| class="wikitable sortable"')
print('|-')
print('! Назва статті !! Варіант перейменування')
from fix_layouts_mix import fix_page_text, find_mixes
for page in pages:
if page.ns == '0' or page.ns == '2':
continue
if not find_mixes(page.title):
continue
fixed_title = fix_page_text(page.title)
if fixed_title == page.title:
fixed_title = '???'
print('|-')
print(f'| [[{page.title}]] || [[{fixed_title}]]')
print('|}')
def comments_report(pages):
print('{| class="wikitable sortable"')
print('|-')
print('! Назва статті !! Закоментовано % !! Закоментовано символів ')
for p in pages:
c = comments(p)
if not c:
continue
print('|-')
print(f'| [[{c["title"]}]] || {c["commented_percent"]:.1f}% || {c["commented_len"]}')
print('|}')
def comments(page):
if page.ns != '0':
return
if len(page.text) < 10:
return
not_commented = re.sub('<!--.*?-->', '', page.text, flags=re.DOTALL)
commented_len = len(page.text) - len(not_commented)
commented_percent = 100.0 * commented_len / len(page.text)
if not (commented_len >= 5000 or commented_percent >= 50.0):
return
return dict(
title=page.title,
commented_len=commented_len,
commented_percent=commented_percent,
)
def iter_pages(dump_filename):
i = 0
for page in XmlDump(dump_filename).parse():
i += 1
if i % 123 == 0:
print('\033[K\r', i, page.title, file=sys.stderr, end='')
yield page
if __name__ == '__main__':
main()