How many times have i used each word

You following code:

>>> from collections import defaultdict
>>> dict = defaultdict(int)
>>> for word in test.split():
...     dict[word] += 1
... 
>>> print dict #defaultdict(<type 'int'>, {'and': 2, 'already': 1, 'help': 1, 'just': 1, 'when': 1, 'is': 2, 'some': 1, 'back': 1, "they've": 1, 'really': 1, 'say': 1, 'customer': 1, 'have': 1, 'impossible': 1, 'trust': 1, '(which': 1, 'quite': 1, 'out': 1, 'even': 1, 'information': 1, 'confirmed': 1, 'court': 1, 'takes': 1, 'for': 1, 'also': 1, 'with': 2, '-': 1, 'been': 1, 'any': 2, 'to': 4, 'take': 1, 'They': 1, 'which': 1, 'taken': 1, 'you': 4, 'has': 1, 'cases.': 1, 'Nor': 1, 'gives': 1, 'do': 1, 'good': 1, 'week),': 1, 'that': 1, 'company': 1, 'after': 1, 'paid': 1, 'it': 1, 'abroad.': 1, 'but': 1, 'they': 2, 'not': 1, 'such': 1, 'bit': 1, 'chargebacks': 1, 'come': 1, 'payment': 1, 'a': 3, 'refuse': 1, "wasn't.": 1, 'of': 1, 'It': 1, 'responsibility': 1, 'can': 1, 'suggestion': 1, 'small': 1, 'claims': 1, 'the': 3, 'refer': 1})

I hope this will help.. :)

The freeCodeCamp Forum

Loading

One common requirement when working with large datasets is counting how many times a particular word or value appears.

You may need to know how many times the word appears in a cell, a column, or an entire worksheet.

This problem can be of two types. You might need to count how many cells contain a given word, or how many times a given word appears in a cell.

In this tutorial, we will see both types of problems and different ways to solve them, including the use of VBA.

Count a Specific Word in a Range using COUNTIF

The COUNTIF function’s main task is to count the number of times a condition is met.

One of the most common uses of this function is to match a particular value to cell values in a range. This value can be a string or a number.

The syntax for the COUNTIF function is:

COUNTIF(range,condition)

Here,

  • range is the range of cells you want to match and count from
  • condition is the condition that must be met in order to count a cell as a match

What the above function will do is search for all the cells in the given range and count all the cells that match the given condition. Once it is done searching through all cells in the range, it will return the number of matched cells.

Let us apply this function to a simple example. Say we want to count the number of times the name ‘Peter’ appears in the range A2:A10 as shown below:

Dataset to count names

To apply the COUNTIF function for the above problem, here are the steps to follow:

  1. Select the cell that you want to write the count in (cell D3 in our case).
  2. In this cell, type the formula:
     =COUNTIF(A2:A10,"Peter")
  3. Press the return key.

This will give the number of times the word “Peter” appears in the range of cells A2:A10.

Countif funftion to count names

Note: This method only counts the number of cells that contain exactly the word “Peter”. If one of the cells, for example, had the words “Peter Peter” in one cell, the COUNTIF function would not consider it as a match, and so would not count the cell.

Issue with countif formula

Count a Specific Word in a Cell using LEN and SUBSTITUTE

The above method works fine if you’re looking to count cells that exactly match a given word. However, it does not work if you want to find the number of times a word occurs inside the string of a cell.

For example, say you have the following string in a cell (A2) and you want to know how many times the word “happy” appears:

Counting the word in the same cell

To solve this problem, there are a number of methods that you can use.

For example, you can use a formula that comprises a combination of Excel functions, like SUBSTITUTE and LEN. You can also use VBA code to get the job done quickly.

The LEN Function

The LEN function is a very commonly used Excel function. It is used to find the length of a string (number of characters in a string). The syntax for the function is:

=LEN(string)

Where string can be a text or reference to a cell containing text for which you want to find the length.

The SUBSTITUTE Function

The SUBSTITUTE function is used to remove a specific word from a string. The general syntax for this function is:

=SUBSTITUTE(original_string, old_text,new_text)

Here,

  • original_string is the text or cell reference that you want to work on.
  • old_text is the word or substring that you want to replace
  • new_text is the word or substring that you want to replace old_text with.

Combining both Functions

Individually, both the above functions don’t seem to have anything to do with counting how many times a word appears in a cell. But when they are cleverly combined into a formula, they accomplish the task.

To count how many times a word appears in a cell, we can use the formula:

=(LEN(cell_reference)-LEN(SUBSTITUTE(cell_reference,word,"")))/LEN(word)

Here, word is the word that you want to count and cell_reference is the reference to the cell you want to count from.

Here’s how you can apply the above formula to the problem:

  1. Select the cell that you want to write the count in (cell B5 in this case).
  2. In this cell, type the formula:
    =(LEN(A2)-LEN(SUBSTITUTE(A2, “happy”,"")))/LEN(“happy”). Instead of specifying the word to replace, you can also specify the reference to the cell containing the word, like this: =(LEN(A2)-LEN(SUBSTITUTE(A2, A5,"")))/LEN(A5).
  3. Press the return key.

LEN function Count to count How many Times a Word appears in a Cell

This will give the number of times the word “happy” appears in cell A2.

If you also want to copy the same formula to count the number of occurrences of other words in cell A2, you can fix the reference to cell A2 by adding a $ sign:

=(LEN($A$2)-LEN(SUBSTITUTE($A$2, A5,"")))/LEN(A5)

Now, you can easily copy the formula for other words by dragging down the fill handle.

Copy and drag the formula for all cells

How did this Formula Work?

To understand how this formula worked, we need to break it down:

  1. First, we used the SUBSTITUTE function to remove the word “happy” from the original string (by replacing it with a blank): SUBSTITUTE(A2, “happy”,””)
  2. Next, we used the LEN function to find the length of the original string without any occurrence of the word “happy” in it: LEN(SUBSTITUTE(A2, “happy”,””))
  3. After this, we subtracted this length from the length of the original string (with the words “happy” in it): LEN(A2)- LEN(SUBSTITUTE(A2, “happy”,””))
  4. What this gives is the total number of characters in all occurrences of the word “happy”. It’s a 5 letter word, so if it is present 4 times, the above operation will return 20.
  5. Finally, this value is divided by the length of the word “happy”: =(LEN($A$2)-LEN(SUBSTITUTE($A$2, A5,””)))/LEN(A5).

This should give you the number of times the word “happy” appears in the original text! So we get 20 / 5 = 4. The word “happy” appears 4 times in cell A2.

Note: The SUBSTITUTE function is case-sensitive. As a result, this formula will only count the number of times “happy” appears in all small letters. If you were to search for the word in all caps, you would not get the correct answer.

A good way to work around this is to ensure that everything is converted to small letters. So to make sure our formula is not case-sensitive, it can be converted to:

=(LEN(cell_reference)-LEN(SUBSTITUTE(LOWER(cell_reference), LOWER(word),"")))/LEN(word)

Here, the function, LOWER is used to convert both the original text and new text to lowercase.

The above method only works when you want to find the number of times a word appears in a single cell. But what if you needed to find out how many times the same word appears in strings in multiple cells?

For this, you will need to wrap the SUMPRODUCT function around the above formula. So, if you have a range of cells to work on, your formula should be:

=SUMPRODUCT(LEN(cell_range)-LEN(SUBSTITUTE(cell_range, word,"")))/LEN(word))

The SUMPRODUCT function ensures that you get an array containing the count for each cell in the range.

Say you have the following set of text strings (A2:A3) and you want to find out how many times the word “happy” appears in all of them:

Multiple lines in multiple cells

Simply follow these steps:

  1. Select the cell that you want to write the count in (cell B6 in this case).
  2. In this cell, type the formula:
    =SUMPRODUCT((LEN(A2:A3)-LEN(SUBSTITUTE(A2:A3,”happy”,"")))/LEN(“happy”)). Instead of specifying the word to replace, you can also specify the reference to the cell containing the word, like this: =SUMPRODUCT((LEN(A2:A3)-LEN(SUBSTITUTE(A2:A3,A6,"")))/LEN(A6))
  3. Press the return key.

This will give the number of times the word “happy” appears in range A2:A3.

SUMPRODUCT formula to count words

If you also want to copy the same formula to count the number of occurrences of other words in the same range, you can fix the reference to the range cell A2:A3 by adding a $ sign:

=SUMPRODUCT((LEN($A$2:$A$3)-LEN(SUBSTITUTE($A$2:$A$3,A6,"")))/LEN(A6))

Now, you can easily copy the formula for other words by dragging down the fill handle.

Copy and drag SUMPRODUCT formula for all cells

Also read: How to Generate Random Names in Excel

Using VBA to Count the Number of Times  a Word Appears in any Range

You can accomplish the same task as above by using VB Script.

Here’s the VBA code that we will be using to count the number of times the word “happy” appears in the code. Feel free to select and copy it.

Sub count_word_occurrences()
Count = 0
search_word = "happy"
Dim rng As Range
Dim cell As Range
Set rng = Application.Selection
For Each cell In rng
Count = Count + ((Len(cell) - Len(Replace$(cell, search_word, ""))) / Len(search_word))
Next cell
MsgBox ("The string " & search_word & " occurred " & Count & " times")
End Sub

Follow these steps:

  1. From the Developer Menu Ribbon, select Visual Basic.
  2. Once your VBA window opens, Click Insert->Module. Now you can start coding. Type or copy-paste the above lines of code into the module window. Your code is now ready to run.VBA code to count how many times a word appears
  3. Select a single cell or cells containing the text you want to count from.
  4. Navigate to Developer->Macros->count_word_occurrences->Run.Macro dialogue box
  5. You will now see a message box telling you how many times the word “happy” appears in your selected range.Showing the count in a message box

Note: If you can’t see the Developer ribbon, from the File menu, go to Options. Select Customize Ribbon and check the Developer option from the Main Tabs. Finally, Click OK.

The great thing about this method is that it does not matter whether you select one cell or a range of cells. It works both ways.

Here are a few ways you can tweak the above code to suit your requirements:

  • You can customize this code by changing the search_word value in line 3 to the word that you want to count. So if you want to count the number of times the word “because” appears, you can replace this line with: search_word = ”because”
  • You can use a cell reference instead of specifying the actual word to search in the code itself. So, if you want to use a cell reference, say A6, instead of specifying the search word “happy”, you can replace line 3 with: search_word = Cells(6,”A”).Value
  • Similarly, you can display the result in a cell instead of a message box. So, if you want to display the count in a cell, say B6, instead of displaying it in a message box, you can replace line 10 with: Cells(6,”B”).Value = Count
  • If you want the search to be case-insensitive, you can use the LCase function. So, you will need to change line 8 to: Count = Count + ((Len(cell) – Len(Replace$(LCase(cell), LCase(search_word), “”))) / Len(search_word))

In this tutorial, we demonstrated how you can count the number of times a word appears in Excel. We showed how to count the exact occurrences of a word in a range of cells using COUNTIF.

We then showed you how you can count how many times a word appears inside the string of a single cell, using SUBSTITUTE and LEN functions.

After that, we demonstrated how you can apply this to a whole range of cells containing strings, by just wrapping the same formula in a SUMPRODUCT function.

Finally, we provided a VBA code snippet to let you get this task done quickly.

We made this tutorial keeping in mind the different kinds of issues you might face when trying to count occurrences of a word in your worksheet. We do hope this was helpful.

Other Excel tutorials you may like:

  • How to Generate Random Numbers in Excel (Without Duplicates)
  • How to Reverse a Text String in Excel
  • How to Split One Column into Multiple Columns in Excel
  • SUMPRODUCT vs SUMIFS Function in Excel
  • How to Remove Text after a Specific Character in Excel
  • How to Add Text to the Beginning or End of all Cells in Excel
  • How to Count Negative Numbers in Excel
  • How to Remove Duplicate Rows based on one Column in Excel?
  • Count the Number of Yes in Excel (Using COUNTIF)
  • Forum
  • Beginners
  • how many times each word was seen

how many times each word was seen

Hi,
Im trying to write a program that I put 2 input files with some words in each one ,convert them into the array, then my program should decide how many times each words used in those 2 files.output should be like this:
Output:
am 2
because 2
don’t 1
happy 2
i 4
sing 2
I just could write this much and i m not sure even this is correct or not!

#include <iostream>
#include<string>
#include<fstream>

using namespace std;

int findTarget(string arr[],int size, string word)
{
for (int i = 0; i < size; i++) {
if (arr[i] == word) {
return i;
}
}

return -1;
}

int main()
{
string word;
int count = 0;
string words[100];
string txt[100];

ifstream fin;
string target = «am»;
fin.open(«words.txt»);

while (fin >> word) {
words[count] = word;
count++;
}

int position = findTarget(words, count,target);

cout << position << endl;

system(«pause»);
return 0;

}

Hi,
my program should decide how many times each words used in those 2 files … i m not sure even this is correct or not!

1
2
3
4
5
6
    int count = 0;
    std::string word;
    while (fin >> word) {
        words[count] = word;
        count++;
    }

I don’t think the logic is correct.
What that loop does, is storing all words into an array. That represents a single piece of information, while you need at least two pieces of information, i.e. the word and its frequency.

Since you use std:.strings, perhaps you want to use a std::map to achieve that purpose. If you don’t feel confident with std::maps, you could use two std::vectors (or even two C-style arrays) which work together to store each word and its frequency at the same index.

For example, you could add another loop inside the above loop, which compare the latest read word with those already stored inside the array. If the comparison is positive, instead of adding that word, it increases its frequency.

Once you’ve correctly read the data, you can proceed to the next step, their output.

@eniozat Thanx for ur respond, But the thing is we havent reach the vectors section and have no idea how that works, thats why i didnt use it

Last edited on

Enoizat wrote:
(or even two C-style arrays)
aliklz92 wrote:
But the thing is we havent reach the vectors

Do try with C-style arrays, then: you have already used them ;-)

Im so sorry, i m so confused right now:), could u tell me how i can combine these 2 arrays with togather?!

I haven’t written the code, but it could be something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <fstream>
#include <iostream>
#include <string>


int main()
{
    std::string words[100];
    int words_freq[100];
    // initialise all 'ints' inside 'words_freq' to 0

    std::ifstream fin("words.txt");
    // check if 'fin' has been correctly opened

    std::string word;
    for (int count = 0; fin >> word; ++count) {
        if(count) {
            // create a variable to store a "word already found" state
            // initialise the above variable to false
            // start a loop with incresaing counter up to 99 {
                // check if "words[counter]" is equal to "word" - if so {
                    // "words_freq[counter]" should be increased by one
                    // set the state "word already found"
                    // skip the rest of the loop
                // }
            // } end loop
        }
        if( ! "word already found" ) {
            words[count] = word;
        }
    }
    return 0;
}

Topic archived. No new replies allowed.


На основании Вашего запроса эти примеры могут содержать грубую лексику.


На основании Вашего запроса эти примеры могут содержать разговорную лексику.


Notice how many times this word appears.


Watch how many times this word shows up.


do count how many times this word occur.


Because we need, we are at the present moment in the middle of a change of consciousness, and you will be surprised if you — I am always surprised when I hear how many times this word «gratefulness» and «gratitude» comes up.



Так как нам это нужно, в данный момент мы находимся в середине процесса перемены сознания, и вы будете удивлены, если вы Я всегда удивляюсь, когда слышу, насколько часто упоминаются слова «благодарность» и «признательность».

Ничего не найдено для этого значения.

Результатов: 3640539. Точных совпадений: 4. Затраченное время: 584 мс

Documents

Корпоративные решения

Спряжение

Синонимы

Корректор

Справка и о нас

Индекс слова: 1-300, 301-600, 601-900

Индекс выражения: 1-400, 401-800, 801-1200

Индекс фразы: 1-400, 401-800, 801-1200

Понравилась статья? Поделить с друзьями:
  • How do you use this word passive voice
  • How do you use this word how used
  • How do you use the word were in a sentence
  • How do you use the word thought in a sentence
  • How do you use the word there in a sentence