user3178285
user3178285

Reputation: 151

Program isn't properly extracting info from text files

I could use some help figuring out where the bug is. I have 2 text files from which I need to extract info.

The first is of the form

  1. word1
  2. word2
  3. word3

etc.

and I just want the words put into a std::vector. There are 5000 words in the text file. When I put a little tester line in my code and ran it, I see that it only got 729 words.

The second text file is of the form

a a 0 a b 5 a c 3

etcetera

and I want to put those into a std::map that maps pairs of characters to integers. When I put a little tester line in my code and ran it, I see that it added zero elements to the map.

Here is the relevant code:

class AutoCorrector
{ 

    public: 
    AutoCorrector(std::ifstream&, std::ifstream&);
    ~AutoCorrector();
    void suggest(std::string);

    private: 
    std::vector<std::string> wdvec;
    std::map<std::pair<char,char>,int> kdmap;


};

AutoCorrector::AutoCorrector(std::ifstream& wdfile, std::ifstream& kdfile) 
{
    /* Insert 5000 most commond English words into a vector. 
       The file that is read was edit-copied copied from 
       http://www.englishclub.com/vocabulary/common-words-5000.htm
       and so the numberings must be ignored on each line in order
       to properly extract the words.
    */
    if (wdfile.is_open()) { 
        std::string line;
        while (std::getline(kdfile, line))
        {
            std::istringstream ss(line);
            std::string nb, thisWord;
            ss >> nb >> thisWord;
            wdvec.push_back(thisWord);
        }
        // test --- 
        std::cout << "wdvec size = " << wdvec.size() << std::endl;
        // -------
    }
    else
    {
        throw("Was not able to open key distance file.\n");
    }   

    /* Insert keyboard pairwise distances into a map.
       The file that is read from must have lines of the form
            a a 0
            a b 5
            a c 3
       etcetera, 
       indicating the distances between characters on a standard keyboard, 
       all lower-case letters and the apostrophe for a total of 27x27=729
       lines in the file.
    */
    if (kdfile.is_open()) { 
        std::string line;
        while (std::getline(kdfile, line))
        {
            std::istringstream ss(line);
            char c1, c2; 
            int thisInt;
            ss >> c1 >> c2 >> thisInt;
            std::pair<char,char> thisPair(c1, c2);
            kdmap.insert(std::pair<std::pair<char,char>, int> (thisPair, thisInt));
        }
        // test --
        std::cout << "kdmap size  = " << kdmap.size() << std::endl;
        // end test
    }
    else
    {
        throw("Was not able to open key distance file.\n");
    }


}

Any help from the StackOverflow C++ purists is greatly appreciated. I'm open to suggestions on how I can simplify and elegantfy my code. Ultimately I'm trying to make an autocorrector that takes a word and searches for the most similar words from a list of the 5000 most common words.

Upvotes: 0

Views: 56

Answers (1)

Joseph Mansfield
Joseph Mansfield

Reputation: 110668

27 * 27 = 729. So your first vector has got the same number of lines as the second file does. Why? Because you're reading from kdfile when you meant to read from wdfile.

while (std::getline(kdfile, line))
                    ^^^^^^

That means you're reading everything out of the pairwise distance file and then the second loop has nothing left to extract.

Upvotes: 1

Related Questions