mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-24 14:25:00 +00:00
feat: Implement a script to process Markdown files and count words in each section
This commit is contained in:
parent
53d992aa97
commit
55bca8d9e9
1 changed files with 37 additions and 0 deletions
37
testsr.py
37
testsr.py
|
@ -0,0 +1,37 @@
|
||||||
|
import sys
|
||||||
|
import re
|
||||||
|
|
||||||
|
def wordcount(text):
|
||||||
|
"""Count the number of words in the given text."""
|
||||||
|
return len(text.split())
|
||||||
|
|
||||||
|
def process_markdown(filename):
|
||||||
|
try:
|
||||||
|
with open(filename, 'r') as file:
|
||||||
|
content = file.read()
|
||||||
|
|
||||||
|
# Split the content into sections based on '####' headers
|
||||||
|
sections = re.split(r'(?=####\s)', content)
|
||||||
|
|
||||||
|
for section in sections:
|
||||||
|
if section.strip(): # Ignore empty sections
|
||||||
|
# Extract the header (if present)
|
||||||
|
header = section.split('\n')[0].strip()
|
||||||
|
# Get the content (everything after the header)
|
||||||
|
content = '\n'.join(section.split('\n')[1:]).strip()
|
||||||
|
|
||||||
|
# Count words
|
||||||
|
count = wordcount(content)
|
||||||
|
|
||||||
|
print(f"{header}: {count} words")
|
||||||
|
|
||||||
|
except FileNotFoundError:
|
||||||
|
print(f"Error: File '{filename}' not found.")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"An error occurred: {e}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if len(sys.argv) != 2:
|
||||||
|
print("Usage: python testsr.py <markdown_filename>")
|
||||||
|
else:
|
||||||
|
process_markdown(sys.argv[1])
|
Loading…
Add table
Add a link
Reference in a new issue