У меня есть строка с большим количеством слов, и у меня есть текстовый файл, содержащий несколько стоп-слов, которые мне нужно удалить из моей строки. Допустим, у меня есть строка
s="I love this phone, its super fast and there's so much new and cool things with jelly bean....but of recently I've seen some bugs."
После удаления стоп-слов строка должна выглядеть так:
"love phone, super fast much cool jelly bean....but recently bugs."
Мне удалось добиться этого, но проблема, с которой я столкнулся, заключается в том, что всякий раз, когда в строке есть соседние стоп-слова, она удаляет только первое, и я получаю результат как:
"love phone, super fast there's much and cool with jelly bean....but recently seen bugs"
Вот мой файл stopwordslist.txt: Стоп-слова
Как я могу решить эту проблему. Вот что я сделал до сих пор:
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's 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);
}