Skip to content
🤔prompts chat🧠
🔍
question:Generate a Python script that can analyze a directory of text files and output the frequency of each word across all files. The script should ignore common stop words like "the", "and", "is", etc. It should also handle case insensitivity and punctuation. You can assume the directory path is passed as a command line argument.

answer:import os import sys from collections import Counter import string # List of common stop words to ignore STOP_WORDS = set([ "the", "and", "is", "in", "it", "you", "that", "he", "was", "for", "on", "are", "with", "as", "I", "his", "they", "be", "at", "one", "have", "this", "from", "or", "had", "by", "not", "word", "but", "what", "some", "we", "can", "out", "other", "were", "all", "there", "when", "up", "use", "your", "how", "said", "an", "each", "she" ]) def clean_word(word): Clean a word by removing punctuation and converting it to lower case. return word.strip(string.punctuation).lower() def count_words_in_directory(directory_path): Analyze a directory of text files and output the frequency of each word across all files. word_counter = Counter() # Iterate over all files in the given directory for filename in os.listdir(directory_path): if filename.endswith('.txt'): file_path = os.path.join(directory_path, filename) with open(file_path, 'r', encoding='utf-8') as file: for line in file: words = line.split() for word in words: cleaned_word = clean_word(word) if cleaned_word and cleaned_word not in STOP_WORDS: word_counter[cleaned_word] += 1 return word_counter if __name__ == '__main__': if len(sys.argv) != 2: print("Usage: python word_frequency.py <directory_path>") sys.exit(1) directory_path = sys.argv[1] word_freq = count_words_in_directory(directory_path) for word, freq in word_freq.most_common(): print(f"{word}: {freq}")

question:Please write a Python function that takes a list of strings and returns a list containing the length of each string in the input list.

answer:def string_lengths(strings): Returns a list containing the length of each string in the input list. Parameters: strings (list): List of strings. Returns: list: List of lengths of each string in the input list. return [len(s) for s in strings]

question:How can I determine the idle time of processes in a Linux system using Python?

answer:def get_idle_time(): Returns the CPU idle time in jiffies. with open('/proc/stat', 'r') as file: for line in file: if line.startswith('cpu '): fields = line.split() idle_time = int(fields[4]) break return idle_time

question:Write a Python function to check if a given string is a palindrome. A string is considered a palindrome if it reads the same forwards and backwards, ignoring spaces, punctuation, and case sensitivity.

answer:import re def is_palindrome(s): Check if the given string is a palindrome, ignoring spaces, punctuation, and case sensitivity. # Remove non-alphanumeric characters and convert to lowercase cleaned_s = re.sub(r'[^A-Za-z0-9]', '', s).lower() # Compare the string with its reverse return cleaned_s == cleaned_s[::-1]

Released under the chat License.

has loaded