skip to Main Content

I am currently trying to make a function that can read a phrase, that consist of combining 2 to 3 adjacent words, from a text file. At the moment my function does that but it eventually concatenates to form the whole article instead of just 2 to 3 words per phrase.

This is my current code:

int Dictionary::processFilePhrases(string file) {
    vector<string> wordList;
    string word;
    string phrase;
    ifstream fin;
    fin.open(file.c_str());

    while (fin >> word) {
        wordList.push_back(word);
    }
    fin.close();

    for (int i=0; i<wordList.size(); i++){
        phrase +=  wordList[i] + " ";
        cout << phrase << endl;

    }
    return wordCount;
}

for example:

input file text: “The next frontier for game-playing artificial intelligence”

The goal is to output words so its like:

the

the next

the next frontier

next

next frontier

next frontier for

frontier

frontier for

frontier for game-playing

etc.

2

Answers


  1. Well the “concatenating” loop runs once over all the words you got, so that’s why you get all the words.

    The way you describe the wanted output, it seems like you want two loops, nested inside each other, where the inner loops from one to n, and where n is the current value from the outer loop.

    Perhaps something like

    for (unsigned i = 0; i < wordList.size(); ++i)
    {
        std::string phrase;
    
        for (unsigned j = 0; j <= i; ++j)
        {
            phrase += wordList[j];
            phrase += ' ';  // Spacing between words
        }
    
        std::cout << phrase << 'n';
    }
    

    If we now step through the code above, in the first iteration of the outer loop, i is equal to zero. In the inner loop we loop while j is less than or equal to i (which now is zero), which means the inner loop will iterate only once, making phrase equal to the string "The " (with the input you have given in the question).

    Once the inner loop finishes the phrase is printed, and the outer loop iterates, which makes i equal to one. The inner loop now iterates two times (for j being zero and one) constructing the phrase "The next ".


    Of course this doesn’t exactly do what you want, printing only up to three words at a time, and starting with the next word after the three first words have been printed. You probably need yet another loop, to handle the one to three counting. The above is a starting point though, and I suggest you experiment with that, and other loops outside the outer, in between, and as a new inner loop. Experimentation and failure is how you learn.

    Login or Signup to reply.
  2. Something like this(I didn’t run it):

    int Dictionary::processFilePhrases(string file) {
        vector<string> wordList;
        string word;
        ifstream fin;
        fin.open(file.c_str());
    
        while (fin >> word) {
            wordList.push_back(word);
        }
        fin.close();
    
        for (int i=0; i<wordList.size(); i++){
            string phrase;
            for (int j = i; j < i + 3 && j < wordList.size(); ++j) {
                phrase += wordList[j] + " ";
                cout << phrase << endl;
            }
            cout << phrase << endl;
    
        }
        return wordCount;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search