কম্পিউটার টিউটোরিয়াল

পাইথন ৩-এ টেক্সট অ্যানালিসিস (ফাইল প্রসেসিং)


ভূমিকা

টেক্সট ফাইল থেকে 다양한 সাংখ্যিক তথ্য বের করা: শব্দ সংখ্যা, ক্যারেক্টার সংখ্যা, গড় শব্দ দৈর্ঘ্য, স্টপ ওয়ার্ড, স্পেশাল ক্যারেক্টার, সংখ্যা, আপারকেস শব্দ।

প্রয়োজনীয় লাইব্রেরি

pip install nltk
# প্রথমবার রান করলে:
# import nltk; nltk.download('stopwords'); nltk.download('punkt')

১. মোট শব্দ সংখ্যা


filename = "C:/Users/TP/Desktop/css3.txt"
try:
    with open(filename, 'r', encoding='utf-8') as f:
        contents = f.read()
except FileNotFoundError:
    print(f"File not found: {filename}")
else:
    words = contents.split()
    print(f"Total words: {len(words)}")
# আউটপুট: Total words: 3574

২. মোট ক্যারেক্টার সংখ্যা


words_list = contents.split()
characters = sum(len(word) for word in words_list)
print(f"Total characters: {characters}")
# আউটপুট: Total characters: 17783

৩. গড় শব্দ দৈর্ঘ্য


avg_length = sum(len(word) for word in words_list) / len(words_list)
print(f"Average word length: {avg_length:.2f}")
# আউটপুট: Average word length: 4.97

৪. স্টপ ওয়ার্ড সংখ্যা (NLTK)


from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize

example = "This is a sample sentence"
stop_words = set(stopwords.words('english'))
tokens = word_tokenize(example)
filtered = [w for w in tokens if w.lower() not in stop_words]

print("Original:", tokens)
print("Filtered:", filtered)
# আউটপুট: ['This', 'sample', 'sentence']

৫. স্পেশাল ক্যারেক্টার গণনা (যেমন #, @)


import collections as ct

special_chars = "#@"  # খুঁজতে চাওয়া ক্যারেক্টারগুলো
words = contents.split()
count = sum(v for k, v in ct.Counter(words).items() if k in special_chars)
print(f"Special characters count: {count}")

৬. সংখ্যাসূচক শব্দ গণনা


numeric_count = sum(1 for word in contents.split() if word.isdigit())
print(f"Numeric words: {numeric_count}")
# আউটপুট: Numeric words: 2

৭. আপারকেস শব্দ গণনা


uppercase_count = sum(1 for word in contents.split() if word.isupper())
print(f"Uppercase words: {uppercase_count}")
# আউটপুট: Uppercase words: 121

সম্পূর্ণ ফাংশন আকারে


def analyze_text_file(filepath):
    import collections as ct
    from nltk.corpus import stopwords
    from nltk.tokenize import word_tokenize
    
    try:
        with open(filepath, 'r', encoding='utf-8') as f:
            text = f.read()
    except FileNotFoundError:
        return {"error": "File not found"}
    
    words = text.split()
    total_words = len(words)
    total_chars = sum(len(w) for w in words)
    avg_len = total_chars / total_words if total_words else 0
    numeric = sum(1 for w in words if w.isdigit())
    uppercase = sum(1 for w in words if w.isupper())
    
    # স্পেশাল ক্যারেক্টার (শব্দ হিসেবে আলাদা হলে)
    special_set = {"#", "@", "!"}
    special = sum(v for k, v in ct.Counter(words).items() if k in special_set)
    
    # স্টপ ওয়ার্ড
    stop_words = set(stopwords.words('english'))
    tokens = word_tokenize(text)
    stop_count = sum(1 for w in tokens if w.lower() in stop_words)
    
    return {
        "total_words": total_words,
        "total_characters": total_chars,
        "avg_word_length": round(avg_len, 2),
        "numeric_words": numeric,
        "uppercase_words": uppercase,
        "special_char_words": special,
        "stop_words": stop_count
    }
  1. Matplotlib লেজেন্ডে টেক্সট এলাইনমেন্ট কীভাবে করবেন?

  2. অ্যান্ড্রয়েডে স্প্যাম টেক্সট মেসেজ ব্লক করার সহজ গাইড

  3. সহজে নম্বরকে টেক্সটে রূপান্তর করুন এবং লিডিং জিরো সংরক্ষণ করুন এক্সেলে

  4. এক্সেলে নম্বরকে শব্দে রূপান্তর: ৪টি প্রুভেন মেথড