How to remove stop words from a string in Java

1 Answer

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

import java.util.Set;
import java.util.List;
import java.util.Arrays;
import java.util.HashSet;
import java.util.stream.Collectors;

public class StopwordRemover {

    private static final Set<String> stopWords = new HashSet<>(Arrays.asList(
        "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",
        "so", "than", "too", "very", "can", "will", "just", "don", "should", "now"
    ));

    public static String removeStopWords(List<String> words) {
        return words.stream()
                    .filter(word -> !stopWords.contains(word))
                    .collect(Collectors.joining(" "));
    }

    public static List<String> splitWords(String input) {
        return Arrays.asList(input.trim().split("\\s+"));
    }

    public static void main(String[] args) {
        String input = "a c++ and java to python a we if c# then a and aa";
        System.out.println("Original: " + input);
        
        List<String> words = splitWords(input);
        
        String filtered = removeStopWords(words);
        System.out.println("Filtered: " + filtered);
    }
}


 
/*
run:

Original: a c++ and java to python a we if c# then a and aa
Filtered: c++ java python c# aa
 
*/

 



answered Jul 17 by avibootz
edited Jul 17 by avibootz
...