Given this code, write one function that gets the top 25 words in the book that are not…
- User
Given this code, write one function that gets the top 25 words in the book that are not within this list: him, his, her, she, they, in, the, it, was, going, be, being, though, said
#!/bin/bash book_word_counter(){ for f in "$@"; do words=( $(< "$f") ) (( num += ${#words[@]} )) done echo "Number of words=$num" } book_word_counter_by_chapter() { echo "Word Count by Chapter:" echo "----------------------------------" local num_words_in_book=0 # Iterate through each act directory for act_dir in ./chapters/act-*; do if [[ -d "$act_dir" ]]; then local total_words_for_act=0 act_num="${act_dir#./chapters/act-}" echo "Act: $(basename "$act_num")" # Iterate through each chapter directory within the act for chapter_dir in "$act_dir"/chapter-*; do if [[ -d "$chapter_dir" ]]; then local chapter_words=0 # Iterate through each markdown file in the chapter for page_file in "$chapter_dir"/*.md; do if [[ -f "$page_file" ]]; then words=( $(< "$page_file") ) (( chapter_words += ${#words[@]} )) fi done chapter_name=$(basename "$chapter_dir") echo " $chapter_name: $chapter_words Words | $(( $chapter_words / 300 )) Pages" (( total_words_for_act += chapter_words )) fi done (( num_words_in_book += $total_words_for_act )) # Print the total word count for the act echo "Total for Act $(basename "$act_num"): $total_words_for_act Words | $(( $total_words_for_act / 300 )) Pages" echo "----------------------------------" fi done echo "Total $num_words_in_book Words" } if [ $# -eq 0 ]; then book_word_counter_by_chapter else book_word_counter $@ fi - Assistant
- User
tr: when translating with complemented character classes, string2 must map all characters in the domain to one Top 25 Words:
- Assistant
- User
Top 25 Words:
- Assistant
- User
I never get into here
while IFS= read -r line; do for word in line; do # Clean the word by removing punctuation clean_word=(echo "$word" | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]')
echo $word # Check for exclusions and if clean_word is not empty if [[ ! "$clean_word" =~ ^($exclude_regex)$ && -n "$clean_word" ]]; then (( word_count["$clean_word"]++ )) fi done done < <(tr '\n' ' ' < "$page_file") - Assistant
- User
run this in parallel
- Assistant
- User
That parallelization doesn't seem any faster
- Assistant
- User
lets try this in js to see if itll be faster
get_top_words() { declare -A word_count exclude_words=("him" "his" "her" "she" "they" "in" "the" "it" "was" "going" "be" "being" "though" "said" "then" "will" "did" "didn't" "wasn't" "not" "but" "and" "can") # Create a regex pattern to match exclude words exclude_regex=$(IFS=\|; echo "${exclude_words[*]}") # Function to process a single markdown file process_file() { local page_file="$1" # Use a temporary associative array for each process declare -A local_word_count=() while IFS= read -r line; do for word in $line; do # Clean the word by removing punctuation clean_word=$(echo "$word" | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]') # Check exclusions and if clean_word is not empty if [[ ! "$clean_word" =~ ^($exclude_regex)$ && -n "$clean_word" ]]; then (( local_word_count["$clean_word"]++ )) fi done done < "$page_file" mkdir -p $(dirname ./output/$page_file) # Write results to the named pipe for word in "${!local_word_count[@]}"; do echo "$word ${local_word_count[$word]}" >> ./output/$page_file done } # Read through the chapters and markdown files and process in parallel for act_dir in ./chapters/act-*; do for chapter_dir in "$act_dir"/chapter-*; do for page_file in "$chapter_dir"/*.md; do if [[ -f "$page_file" ]]; then echo "Processing file: $page_file" # Debug output process_file "$page_file" fi done done done # Display the top 25 words if [ ${#word_count[@]} -eq 0 ]; then echo "No words found that are outside the exclusion list." # Debugging message else echo "Top 25 Words:" for word in "${!word_count[@]}"; do echo "${word_count[$word]}: $word" done | sort -nr | head -n 25 fi } - Assistant
- User
node word_repetition.js Error processing files: TypeError: chapterDirs is not async iterable
- Assistant
- User
write a script that looks for words that have a containing word from a white list, so a new script similar to the one above but to find similiar words based on a white list
- Assistant