I have a line with a lot of words, and I have a text file containing some Stopwords that I need to remove from my line. Say I have a line
s="I love this phone, its super fast and there so much new and cool things with jelly bean....but of recently I've seen some bugs."
After removing the stop words, the line should look like:
"love phone, super fast much cool jelly bean....but recently bugs."
I managed to achieve this, but the problem I am facing is that when there are adjacent stop words in the String, only the first one is deleted, and I get the result as:
"love phone, super fast there much and cool with jelly bean....but recently seen bugs"
Here is my stopwordslist.txt file: Stopwords
How can I solve this problem. Here is what I have done so far:
int k=0,i,j; ArrayList<String> wordsList = new ArrayList<String>(); String sCurrentLine; String[] stopwords = new String[2000]; try{ FileReader fr=new FileReader("F:\\stopwordslist.txt"); BufferedReader br= new BufferedReader(fr); while ((sCurrentLine = br.readLine()) != null){ stopwords[k]=sCurrentLine; k++; } String s="I love this phone, its super fast and there so much new and cool things with jelly bean....but of recently I've seen some bugs."; StringBuilder builder = new StringBuilder(s); String[] words = builder.toString().split("\\s"); for (String word : words){ wordsList.add(word); } for(int ii = 0; ii < wordsList.size(); ii++){ for(int jj = 0; jj < k; jj++){ if(stopwords[jj].contains(wordsList.get(ii).toLowerCase())){ wordsList.remove(ii); break; } } } for (String str : wordsList){ System.out.print(str+" "); } }catch(Exception ex){ System.out.println(ex); }
source share