2014-04-13 43 views
0

我正在讀取文本文件並將其解析爲映射,以計算每行上每個單詞的出現次數。我需要忽略除撇號以外的所有非字母字符(標點,數字,空格等)。我可以找出如何使用下面的代碼刪除所有這些字符,但這會導致不正確的單詞,如「one-two」作爲「onetwo」出現,應該是兩個單詞「one」和「two」。C++用字符串中的空格替換非alpha /撇號

相反,我想現在用空格替換所有這些值而不是簡單刪除,但無法弄清楚如何做到這一點。我認爲replace-if算法是一個很好的算法,但是無法弄清楚實現這一點的正確語法。 C++ 11很好。有什麼建議麼?

樣本輸出將是如下:

"first second" = "first" and "second" 
"one-two" = "one" and "two" 
"last.First" = "last" and "first" 
"you're" = "you're" 
"great! A" = "great" and "A" 

// What I initially used to delete non-alpha and white space (apostrophe's not working currently, though) 

// Read file one line at a time 
while (getline(text, line)){ 
    istringstream iss(line);    
    // Parse line on white space, storing values into tokens map 
    while (iss >> word){ 
     word.erase(remove_if(word.begin(), word.end(), my_predicate), word.end()); 
     ++tokens[word][linenum]; 
    } 
    ++linenum; 
} 

bool my_predicate(char c){ 
    return c == '\'' || !isalpha(c); // This line's not working properly for apostrophe's yet 
} 

回答

1
bool my_predicate(char c){ 
    return c == '\'' || !isalpha(c); 
} 

這裏你寫,你要刪除的字符,如果和撇號,或者如果它不是字母字符。

既然你要替換這些,你應該使用std::replace_if()

std::replace_if(std::begin(word), std::end(word), my_predicate, ' '); 

而且你也應該糾正你的斷言:

return !isalpha(c) && c != '\''; 
1

你可以使用std::replace_if預先處理的輸入線將它發送到istringstream之前。這也將簡化你的內部循環。

while (getline(text, line)){ 
    replace_if(line.begin(), line.end(), my_predicate, ' '); 
    istringstream iss(line);    
    // Parse line on white space, storing values into tokens map 
    while (iss >> word){ 
     ++tokens[word][linenum]; 
    } 
    ++linenum; 
}