Project
Project
nltk.download("all")
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.tokenize import sent_tokenize
import tkinter as tk
from tkinter import filedialog
import re
import nltk
def count_words(text):
# Convert text to lowercase
result = text.lower()
# Remove punctuations
result = re.sub('[^A-Za-z]', " ", result)
result = re.sub('\s+', " ", result)
# Tokenize text into words
words = nltk.word_tokenize(result)
# Remove stop words
words = [w for w in words if w not in nltk.corpus.stopwords.words('english')]
# Join words into a sentence
sentence = " ".join(words)
# Count the number of words without spaces
num_words_without_spaces = len(sentence.split())
# Count the number of words with spaces
num_words_with_spaces = len(sentence.split()) + sentence.count(' ')
return num_words_with_spaces, num_words_without_spaces
def count_sentences(text):
# Tokenize text into sentences
sentences = sent_tokenize(text)
# Count the number of sentences
num_sentences = len(sentences)
return num_sentences
def count_paragraphs(text):
# Split text into paragraphs based on empty lines
paragraphs = re.split('\n\s*\n', text)
# Count the number of paragraphs
num_paragraphs = len(paragraphs)
return num_paragraphs
def upload_file():
file_path = filedialog.askopenfilename(filetypes=[("Text Files", "*.txt")])
if file_path:
with open(file_path, "r") as file:
text = file.read()
process_text(text)
def process_text(text):
num_words_with_spaces, num_words_without_spaces = count_words(text)
num_sentences = count_sentences(text)
num_paragraphs = count_paragraphs(text)
result_text.delete("1.0", tk.END)
result_text.insert(tk.END, "Number of words with spaces: {}\
n".format(num_words_with_spaces))
result_text.insert(tk.END, "Number of words without spaces: {}\
n".format(num_words_without_spaces))
result_text.insert(tk.END, "Number of sentences: {}\n".format(num_sentences))
result_text.insert(tk.END, "Number of paragraphs: {}".format(num_paragraphs))
def open_input_popup():
def start_processing():
text = input_text.get("1.0", tk.END).strip()
popup_window.destroy()
process_text(text)
popup_window = tk.Toplevel(window)
popup_window.title("Enter Text")
window.mainloop()