-
-
Notifications
You must be signed in to change notification settings - Fork 347
Added a count-words feature #447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -65,6 +65,24 @@ def convert_pdf_to_txt(pdf_path, save_to_file=True, output_folder="output_texts" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
print(f"Error processing {pdf_path}: {e}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def count_words_in_pdf(pdf_path): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
with open(pdf_path, 'rb') as pdf_file: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pdf_reader = PyPDF2.PdfReader(pdf_file) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
text = "" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for page_num in range(len(pdf_reader.pages)): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
page = pdf_reader.pages[page_num] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
text += page.extract_text() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Remove extra whitespaces and split into words | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
words = re.findall(r'\b\w+\b', text.lower()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return len(words) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+71
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function duplicates text extraction logic already present in the existing
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
except FileNotFoundError: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return "Error: PDF file not found." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return f"An error occurred: {e}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+70
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistent indentation: the function uses 7 spaces instead of the standard 4 spaces used elsewhere in the file.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback
Comment on lines
+82
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function returns different types (integer for success, string for errors). This inconsistent return type makes error handling difficult. Consider raising exceptions or returning a consistent data structure.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Example usage: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#example pdf from internet | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -75,3 +93,5 @@ def convert_pdf_to_txt(pdf_path, save_to_file=True, output_folder="output_texts" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Convert PDF to text and save the cleaned text to a file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
convert_pdf_to_txt(pdf) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
word_count = count_words_in_pdf(pdf) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
print(f"Total word count in the PDF: {word_count}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
re
module is used but not imported. This will cause a NameError at runtime.Copilot uses AI. Check for mistakes.