0% found this document useful (0 votes)
3 views2 pages

Project

bdkakakw

Uploaded by

areeg.elgohary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Project

bdkakakw

Uploaded by

areeg.elgohary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import nltk

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")

input_text = tk.Text(popup_window, height=10, width=50)


input_text.pack(padx=10, pady=10)

start_button = tk.Button(popup_window, text="Start", command=start_processing)


start_button.pack(pady=10)

# Create the GUI window


window = tk.Tk()
window.title("Text Analysis")

# Create the file upload button


upload_button = tk.Button(window, text="Upload File", command=upload_file)
upload_button.pack(pady=10)

# Create the input popup button


input_button = tk.Button(window, text="Enter Text", command=open_input_popup)
input_button.pack(pady=10)

# Create the text field for displaying the results


result_text = tk.Text(window, height=10, width=50)
result_text.pack(padx=10, pady=10)

window.mainloop()

You might also like