C ++ Determining the frequency of words found in a sentence

What is the best STL to use for this task? I used Mapand I could not get it to work. I'm not sure how I should check the number of the same words that appear in a sentence, for example:

I love him, I love her, he loves her.

So, I want the program to prompt the user to enter an integer, let's say I enter 3, the output will be love, because the same word occurs 3 times in a sentence. But what method should I use if I want to make such a program?

Currently, my program prompts the user to enter a word, and then it will return how many times this word occurs, which is 3. for the love of the word, but now I want it to be the other way around. It can be done? Using which STL would be better?

+3
source share
4 answers

I assume that you are using a map to store the number of occurrences. Well, first you need to understand this, since you are using a card, the key is unique, while the stored data may not be unique. Consider a mapping x with content

x["I"]=3
x["Love"]=3
x["C"]=5

There is a unique mapping from key to value, and not vice versa, if you need this one to one mapping, I would suggest a different data structure. If you want to use the map and still search for the item using the STL search function or your own. Or you can write your search function. search () .

map<string,int>::iterator ser;
cin>>check;
for(ser=x.begin();ser!=x.end();++ser)
{
    if(ser->second==check)
    {
       cout<<"Word"<<ser->first<<endl;
       break;
    }
}
+3
source

, -. , , :

#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <utility>

int main()
{
    std::string str("I love him, I love her, he love her");
    std::istringstream ss(str);
    std::istream_iterator<std::string> begin(ss);
    std::istream_iterator<std::string> end;

    std::map<std::string, int> word_count;
    std::for_each(begin, end, [&](const std::string& s)
    {
        ++word_count[s];
    });

    std::multimap<int, std::string> count_words;
    std::for_each(word_count.begin(), word_count.end(),
                  [&](const std::pair<std::string, int>& p)
    {
        count_words.insert(std::make_pair(p.second, p.first));
    });

    auto its = count_words.equal_range(3);
    std::for_each(its.first, its.second,
                  [](const std::pair<int, std::string>& p)
    {
        std::cout << p.second << std::endl;
    });
}
+3
/******************************************************************
Name  :  Paul Rodgers
Source : HW1.CPP
Compiler :  Visual C++ .NET
Action : Program will read in from standard input and determine the
         frequency of word lengths found in input.  An appropriate
         table is also displayed.  Maximum word length is 15 characters
         words greater then 15 are counted as length 15. 
         Average word length also displayed.

Note   : Words include hyphenated and ones with apostrophes.  Words with
         apostrophes, i.e. Jim's, will count the apostrophe as part of the
         word length. Hyphen is counted if word on same line, else not.

         Also an int array is used to hold the number of words with
         length associated with matching subscript, with subscript 0
         not being used.  So subscript 1 corresponds to word length of 1,
         subscript 2 to word length of 2 and so on.
------------------------------------------------------------------------*/
#include <iostream>
#include <ctype.h>
#include <iomanip>
using namespace std;

int NextWordLength(void);                    // function prototypes
void DisplayFrequencyTable(const int Words[]);

const int WORD_LENGTH = 16;                // global constant for array

void main()
{
  int WordLength;                         // actual length of word 0 to X
  int NumOfWords[WORD_LENGTH] = {0};     // array holds # of lengths of words

  WordLength = NextWordLength();
  while (WordLength)                   // continue to loop until no word, i.e. 0
    {                                 // increment length counter
      (WordLength <= 14) ? (++NumOfWords[WordLength]) : (++NumOfWords[15]);
      WordLength = NextWordLength();
    }

  DisplayFrequencyTable(NumOfWords);
}

/**********************  NextWordLength  ********************************
Action  : Will determine the length of the next word. Hyphenated words and
          words with apostrophes are counted as one word accordingly
Parameters : none
Returns   : the length of word, 0 if none, i.e. end of file
-----------------------------------------------------------------------*/
int NextWordLength(void)
{
  char Ch;
  int EndOfWord = 0,       //tells when we have read in one word
      LengthOfWord = 0;

  Ch = cin.get();                           // get first character
  while (!cin.eof() && !EndOfWord)
   {
     while (isspace(Ch) || ispunct(Ch))      // Skips leading white spaces
        Ch = cin.get();                      // and leading punctation marks

     if (isalnum(Ch))          // if character is a letter or number
        ++LengthOfWord;        // then increment word length

     Ch = cin.get();           // get next character

     if ((Ch == '-') && (cin.peek() == '\n')) //check for hyphenated word over two lines
       {
         Ch = cin.get();       // don't count hyphen and remove the newline char
         Ch = cin.get();       // get next character then on next line
       }

     if ((Ch == '-') && (isalpha(cin.peek()))) //check for hyphenated word in one line
     {
         ++LengthOfWord;       // count the hyphen as part of word
         Ch = cin.get();       // get next character
     }

     if ((Ch == '\'') && (isalpha(cin.peek()))) // check for apostrophe in word
      {
        ++LengthOfWord;        // count apostrophe in word length
        Ch = cin.get();        // and get next letter
      }

     if (isspace(Ch) || ispunct(Ch) || cin.eof())  // is it end of word
       EndOfWord++;
   }

  return LengthOfWord;
}

/***********************  DisplayFrequencyTable  **************************
Action      :  Will display the frequency of length of words along with the
               average word length
Parameters
  IN        : Pointer to array holding the frequency of the lengths
Returns     : Nothing
Precondition: for loop does not go beyond WORD_LENGTH
------------------------------------------------------------------------*/
void DisplayFrequencyTable(const int Words[])
{
  int TotalWords = 0, TotalLength = 0;

  cout << "\nWord Length      Frequency\n";
  cout << "------------     ----------\n";

  for (int i = 1; i <= WORD_LENGTH-1; i++)
    {
     cout << setw(4) << i << setw(18) << Words[i] << endl;
     TotalLength += (i*Words[i]);
     TotalWords += Words[i];
    }

  cout << "\nAverage word length is ";

  if (TotalLength)
     cout << float(TotalLength)/TotalWords << endl;
  else
    cout << 0 << endl;
}
+2
source
#include<iostream>
#include<string>
#include<vector>
#include<cstddef>
#include<map>

using std::cout;
using std::cin;
using std::string;
using std::endl;
using std::vector;
using std::map;

int main() {

    cout << "Please enter a string: " << endl;
    string str;
    getline(cin, str, '\n');

    size_t str_len = str.size();
    cout << endl << endl;

    size_t i = 0, j = 0;
    bool pop = false;

    map<string, int> myMap;

    for (size_t k = 0; k < str_len-1; k++) {
        if (((k == 0) && isalpha(str[0])) || (!(isalpha(str[k-1])) && isalpha(str[k])))
            i = k;
        if ( isalpha(str[k]) && !(isalpha(str[k+1])) ) {
            j = k;
            pop = true;
        }
        if ( (k == str_len-2) && isalpha(str[k+1]) ) {
            j = k+1;
            pop = true;
        }

        if ( (i <= j) && pop ) {
            string tmp = str.substr(i, j-i+1);
            cout << tmp << '\t';
            myMap[tmp]++;
            pop = false;
        }
    }
    cout << endl << endl;

    map<string, int>::iterator itr, end = myMap.end();
    for (itr = myMap.begin(); itr != end; itr++)
        cout << itr->first << "\t - - - - - \t" << itr->second << endl;

    cout << endl;

    return 0;
}
-1
source

Source: https://habr.com/ru/post/1773368/


All Articles