Word Count Threshold Check

  • Share this:

Code introduction


The function reads the content of a file at the specified path and counts the number of words in the file. If the number of words is less than the specified threshold, it returns False, otherwise it returns True.


Technology Stack : os, re

Code Type : Function

Code Difficulty : Intermediate


                
                    
import os
import re

def read_file(file_path, encoding='utf-8'):
    with open(file_path, 'r', encoding=encoding) as file:
        return file.read()

def count_occurrences(text):
    return len(re.findall(r'\w+', text))

def xxx(arg1, arg2):
    content = read_file(arg1)
    word_count = count_occurrences(content)
    if word_count < arg2:
        return False
    return True                
              
Tags: