How to remove stop words from a string in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <unordered_set>
#include <iterator>

// A stopwords list is a collection of commonly used words in a language
// that are often removed during text processing tasks. 

std::string removeStopWords(const std::vector<std::string>& words) {
    std::unordered_set<std::string> stopWords = {
        "i", "me", "my", "myself", "we", "our", "ours", "ourselves", "you", "your", 
        "yours", "yourself", "yourselves", "he", "him", "his", "himself", "she", "her", 
        "hers", "herself", "it", "its", "itself", "they", "them", "their", "theirs", 
        "themselves", "what", "which", "who", "whom", "this", "that", "these", "those", 
        "am", "is", "are", "was", "were", "be", "been", "being", "have", "has", "had", 
        "having", "do", "does", "did", "doing", "a", "an", "the", "and", "but", "if", "or", 
        "because", "as", "until", "while", "of", "at", "by", "for", "with", "about", "against", 
        "between", "into", "through", "to", "from", "in", "out", "on", "off", "over", "further", 
        "then", "here", "there", "when", "where", "why", "how", "all", "any", "both", "each", 
        "few", "more", "most", "other", "some", "such", "no", "nor", "not", "only", "own", 
        "same", "so", "than", "too", "very", "can", "will", "just", "don", "should", "now"
    };

    std::ostringstream result;
    for (const auto& word : words) {
        if (stopWords.find(word) == stopWords.end()) {
            result << word << " ";
        }
    }

    std::string trimmedstr = result.str();
    if (!trimmedstr.empty() && trimmedstr.back() == ' ')
        trimmedstr.pop_back();

    return trimmedstr;
}

std::vector<std::string> splitWords(const std::string& s) {
    std::istringstream iss(s);
    
    return {std::istream_iterator<std::string>{iss}, std::istream_iterator<std::string>{}};
}

int main() {
    std::string input = "a c++ and java to python a we if c# then a and aa";
    std::vector<std::string> words = splitWords(input);

    std::cout << input << "\n";

    std::string filtered = removeStopWords(words);
    std::cout << filtered << std::endl;
}


  
/*
run:

a c++ and java to python a we if c# then a and aa
c++ java python c# aa

*/


 



answered Jul 17, 2025 by avibootz
...