Domanda

I am printing duplicates and want to get rid of code for reading until the end of file for sentences and removing extra spaces.

I used this code to split each sentence into words.

vector <string> oneWordPhrase;
vector <string> twoWordPhrase;

vector<string>::iterator it1;
vector<string>::iterator it2;

  string split = str;
  string word;
  stringstream stream(split);
  while( getline(stream, word, ' ') )
  {
    cout<<word<<endl;
    oneWordPhrase.push_back(word);
  }//split the sentence into words

  for(it1=oneWordPhrase.begin(); it1!=oneWordPhrase.end(); it1++) /* the problem 
  {                                                    is here. */
    if(it1+1 == oneWordPhrase.end())
      break; //signal break if we are near the end of a sentence
    twoWordPhrase.push_back(*it1 + ' ' + *(it1+1));
  }

for(int i=0; i<twoWordPhrase.size(); i++)
  cout<<twoWordPhrase[i]<<endl

This code works for one one sentence. For example if my string is "Hello my name is bob. i am a student." I want to print

"hello my"

"my name"

"name is"

"is bob"

/* new sentence*/

"i am"

"am a"

"a student"

However my output is

"Hello my"

"my name"

"name is"

"is bob"

/* problem here. it goes back to the beginning of the sentence */

"Hello my"

"my name"

"name is"

"is bob"

"bob i"

/* it also doesnt recognize the new sentence */

"i am"

"am a"

"a student"

Is there a way for me to use my iterator to point to where it left off rather than the beginning. This code works fine for one sentence but it creates duplicates with more than 1 string

Nessuna soluzione corretta

Altri suggerimenti

You're not detecting the end of a sentence properly.

if(it1+1 == oneWordPhrase.end())
  break; //signal break if we are near the end of a sentence

The line above will only be triggered when you reached the end of the entire phrase. Not just a sentence. To detect a sentence you'll need to detect the period. Here's one possible method:

for (it1 = oneWordPhrase.begin(); it1 + 1 != oneWordPhrase.end(); it1++) {
    if (it1[0][(it1[0].size() - 1)] == '.') {
      continue;
    }
    twoWordPhrase.push_back(*it1 + ' ' + *(it1 + 1));
  }

I changed it1 != oneWordPhrase.end() to it1 + 1 != oneWordPhrase.end() to simulate the behavior of your break statement.

it1[0][(it1[0].size() - 1)]

Extracts the last char of the a word you stored.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top