Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.7 kB
3
Indexable
Never
import re
# Function to sort sentences based on the number of characters
# Function to count the total number of sentences
def count_sentences(file_path):
    # Read the entire file content
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()

    # Split the content into sentences based on punctuation marks
    sentences = re.split(r'[।.?]', content)

    # Remove empty strings and whitespaces
    sentences = [sentence.strip() for sentence in sentences if sentence.strip()]

    # Count the total number of sentences
    total_sentences = len(sentences)

    # Print the total number of sentences
    print("Total number of sentences:", total_sentences)

def sort_sentences_by_length(file_path):
    # Read sentences from the file
    with open(file_path, 'r', encoding='utf-8') as file:
        sentences = file.readlines()
    num_sentences_before_sorting = len(sentences)

    # Sort the sentences by length
    sorted_sentences = sorted(sentences, key=len)
    

    # Write sorted sentences to a new file
    with open('sorted_sentences_by_length.txt', 'w', encoding='utf-8') as file:
        file.writelines(sorted_sentences)
    num_sentences_after_sorting = len(sorted_sentences)
    print("Total number of sentences before sorting:", num_sentences_before_sorting)
    print("Total number of sentences after sorting:", num_sentences_after_sorting)
# Example usage
if __name__ == "__main__":
    file_path = 'Data.txt'  # Replace with your file path
    sort_sentences_by_length(file_path)
    count_sentences(file_path)
    print("Sentences sorted by length successfully.")
Leave a Comment