我目前正在尝试建立一个函数,它可以从文本文件中读取一个短语,该短语由2到3个相邻的单词组成。目前,我的功能做到了这一点,但它最终连接成整篇文章,而不是每个短语只有2到3个单词。
这是我目前的代码:
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;
}例如:
输入文件文本:“游戏玩人工智能的下一个前沿”。
其目标是输出文字,如下所示:
这个
下一个
下一个边疆
下一首
下一边疆
下一个边疆
边疆
边疆
游戏的前沿
..。
等。
发布于 2016-05-18 11:55:30
就像这样(我没有运行它):
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;
}https://stackoverflow.com/questions/37298620
复制相似问题