Mam ciąg znaków z dużą ilością słów i mam plik tekstowy zawierający kilka słów kluczowych, które muszę usunąć z mojego ciągu. Powiedzmy mam StringUsuwanie stoperw z ciągu znaków w Javie
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."
po usunięciu stopwords, łańcuch powinien być jak:
"love phone, super fast much cool jelly bean....but recently bugs."
udało mi się to osiągnąć, ale problem jestem stoi to, że whenver istnieją sąsiadujące stopwords w ciągu jego usunięcie tylko pierwszy i jestem coraz wynik:
"love phone, super fast there's much and cool with jelly bean....but recently seen bugs"
Oto mój plik stopwordslist.txt: Stopwords
Jak mogę rozwiązać ten problem. Oto co zrobiłem do tej pory:
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);
}
byłoby dzielenie ciąg pierwszej pomocy? coś takiego jak "phrase.split (delims);" możesz odfiltrować niepożądane części przed ponownym łączeniem ich z powrotem. może to rozwiązać problem "ten" i "jego". –
[Więcej szczegółowych pytań tutaj] (http://stackoverflow.com/questions/22257598/best-way-to-remove-stop-words-from-files) – jsroyal