Skip to content

Commit cd1e158

Browse files
Generate word_count and display in index.html (#81)
Added a script for calculating the total word count of translatable content (msgids) in a directory. Use script in generate.py/index.html to display Python Documentation current word count.
1 parent ab465de commit cd1e158

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

generate.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import contribute
2626
from completion import branches_from_devguide, get_completion, TranslatorsData
2727
from repositories import Language, get_languages_and_repos
28+
from word_count import get_word_count
2829

2930
generation_time = datetime.now(timezone.utc)
3031

@@ -107,6 +108,7 @@ class LanguageProjectData:
107108
completion_progress=(completion_progress := list(get_completion_progress())),
108109
generation_time=generation_time,
109110
duration=(datetime.now(timezone.utc) - generation_time).seconds,
111+
word_count=get_word_count(Path('clones', 'cpython', 'Doc', 'build', 'gettext')),
110112
)
111113

112114
Path('index.html').write_text(output)

template.html.jinja

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ main | <a href="metadata.html" target="_self">meta</a>
5353
</tbody>
5454
</table>
5555
<p>* the number in parentheses shows change in the last 30 days, included in the total completion</p>
56+
<p>The Python documentation currently has a word count of {{ '{:,}'.format(word_count) }}.</p>
5657
<p>For more information about translations, see the <a href="https://devguide.python.org/documentation/translating/">Python Developer’s Guide</a>.</p>
5758
<p>Last updated at {{ generation_time.strftime('%A, %-d %B %Y, %-H:%M:%S %Z') }} (in {{ duration // 60 }}:{{ "{:02}".format(duration % 60) }} minutes).</p>
5859
</body>

word_count.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os
2+
from polib import pofile
3+
4+
5+
def _count_words(pot) -> int:
6+
pot = pofile(pot)
7+
word_count = 0
8+
for entry in pot:
9+
word_count += len(entry.msgid.split())
10+
return word_count
11+
12+
13+
def get_word_count(dir) -> int:
14+
total_word_count = 0
15+
for root, dirs, files in os.walk(dir):
16+
for file in files:
17+
if file.endswith('.pot'):
18+
pot = os.path.join(root, file)
19+
total_word_count += _count_words(pot)
20+
return total_word_count

0 commit comments

Comments
 (0)