Skip to content
the poor coder

Determine the most common word in a book

Poor Man
Published
Reading time
2 min read
On this page

Determine the most common word in a book#

As a book lover, I have always been fascinated by the idea of analyzing a book's text to find out the most common word used in it. It's a great way to understand the author's writing style and the themes present in the book. Here are a few ways to determine the most common word:

1. Counting Words Manually#

The most time-consuming but straightforward method is to read the book and count how many times each word appears. You can keep a tally on a piece of paper or use a spreadsheet to keep track of the counts.

Text

let bookText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod vel lacus sed mollis. Donec pharetra vestibulum turpis eu convallis.";
let wordCounts = {};
let words = bookText.split(" ");

for (let i = 0; i < words.length; i++) {
  let currentWord = words[i].toLowerCase();
  if (wordCounts[currentWord] === undefined) {
    wordCounts[currentWord] = 1;
  } else {
    wordCounts[currentWord]++;
  }
}

let mostCommonWord = "";
let highestCount = 0;

for (let word in wordCounts) {
  if (wordCounts.hasOwnProperty(word)) {
    if (wordCounts[word] > highestCount) {
      mostCommonWord = word;
      highestCount = wordCounts[word];
    }
  }
}

console.log("The most common word is: " + mostCommonWord + " (" + highestCount + " times)");

2. Using Python#

If you have some programming experience, you can use Python to count the words for you. Python has many libraries that can help you analyze text data.

Text

import re

def count_words(filename):
    with open(filename, 'r') as file:
        text = file.read().lower()
    words = re.findall('\w+', text)
    word_counts = {}
    for word in words:
        if word not in word_counts:
            word_counts[word] = 0
        word_counts[word] += 1
    return word_counts

def most_common_word(word_counts):
    return max(word_counts, key=word_counts.get)

word_counts = count_words('book.txt')
most_common = most_common_word(word_counts)
print(f"The most common word is {most_common}")

3. Using Online Tools#

If you don't want to do any coding, you can use online tools like WordCounter or Online-Utility to analyze the book's text and find the most common word. These tools will do all the counting for you and present the result in a neat format.

Overall, determining the most common word in a book can be done using various methods. If you have some programming experience, I recommend using Python as it can save you a lot of time. However, if you're not comfortable with coding, online tools are an easy and convenient option.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC