简体   繁体   中英

How to iterate position of the words in a file using vectors c++

I have to read dictionary.txt and test.txt . Then, have to check each word in test.txt if it is equal then this word would be added in KnownWords container and the remaining will be in UnknownWords container. I have done so far everything. But, in function DisplayKnownWordStats() I also have to display the position of the words. I tried the following code but it is not giving me correct value of the positions.

Can someone please help me out to get the correct position of the KnownWords from test.txt .

void WordStats::ReadTxtFile()
{
    std::ifstream ifile(Filename);
    if (!ifile)
    {
        std::cerr << "Error Opening file " << Filename << std::endl;
        exit(1);
    }
    for (std::string word; ifile >> word; )
    {
        transform(word.begin(), word.end(), word.begin(), ::tolower);
        WordMap & Words = (Dictionary.count(word) ? KnownWords : UnknownWords);
        Words[word].push_back(ifile.tellg());
    }
    std::cout << KnownWords.size() << " known words read." << std::endl;
    std::cout << UnknownWords.size() << " unknown words read." << std::endl;
}

// Displays stats of words in KnownWords
void WordStats::DisplayKnownWordStats()
{
    cout << "            ";
    cout << "    Word     Count      Position(s)\n";
    for (Paragraph = KnownWords.begin(); Paragraph != KnownWords.end(); ++Paragraph)
    {
        string word = (*Paragraph).first;
        vector<int> vect = (*Paragraph).second;
        int cnt = vect.size();
        cout << setw(15) << word << setw(10) << cnt << "   ";
        for (int i = 0; i<cnt; i++)
            cout << vect[i] << ' ';   // something wrong with this code
        cout << endl;
    }
}
Words[word].push_back(ifile.tellg());

Above ifile.tellg() returns a position after a word. For example if a file contains the word "word", you read a first word from a file, then ifile.tellg() returns 4. It seems you expect the position 0 there. You should do like bellow:

Words[word].push_back(ifile.tellg() - word.size());
void WordStats::ReadTxtFile(){

int pos=-1;
string word;

std::ifstream ifile(Filename);
if(!ifile)
{
    std::cerr << "Error Opening file " << Filename << std::endl;
    exit(1);
}
for (std::string word; ifile >> word;  )
{
    transform (word.begin(), word.end(), word.begin(), ::tolower);
    word.erase(std::remove_if(word.begin(), word.end(), [](char c)
    {
        return (c < 'a' || c > 'z') && c != '\'' ;

    }),  word.end());
    if (word == "") continue;
    pos++;
    if (Dictionary.find(word) != Dictionary.end())
    {
        Paragraph = KnownWords.find(word);            //adding word to the knownWords
        if (Paragraph != KnownWords.end())
        {
            Paragraph->second.push_back(pos);         //adding position of the word
        }         
        else 
        {
            KnownWords[word] = vector<int>(1, pos);   //adding record which is new
        }
    }
    else
    {
        Paragraph = UnknownWords.find(word);            //adding word to the knownWords
        if (Paragraph != UnknownWords.end())
        {
            Paragraph->second.push_back(pos);          //adding position of the word
        }         
        else 
        {
            UnknownWords[word] = vector<int>(1, pos);   //adding record which is new
        }
    }

}
cout << KnownWords.size() << " known words read." << endl;
cout << UnknownWords.size() << " unknown words read." << endl;
}


void WordStats::DisplayKnownWordsStats()
{
vector<int>::iterator v;
cout << "\n";
cout << setw(22) << "Word" << setw(12) << "Count" << setw(20) << "Position(s)"     << endl;
for (Paragraph = KnownWords.begin(); Paragraph != KnownWords.end(); Paragraph++) 
{
    string word = (*Paragraph).first;                   
    vector<int> vect = (*Paragraph).second;
    int count = vect.size();
    cout << setw(22) << word << setw(9) << count ;
    cout << "            ";
    for ( v = vect.begin(); v < vect.end(); v++)
    {
        cout << *v << " ";
    }   
    cout << endl;
}
cout << "\n";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM