[英]Removing stop words from String
class MyClass {
public static void remove_stopwords(String[] query, String[] stopwords) {
A: for (int i = 0; i < query.length; i++) {
B: for (int j = 0; j < stopwords.length; j++) {
C: if (query[i].equals(stopwords[j])) {
break B;
}
else {
System.out.println(query[i]);
break B;
}
}
}
}
}
由於某種原因,此代碼只能在問題的一半左右正常工作。 它從查詢中取出第一個停用詞,但忽略其余詞。 任何幫助,將不勝感激。
class MyClass
{
public static void remove_stopwords(String[] query, String[] stopwords) {
A: for (int i = 0; i < query.length; i++) {
//iterate through all stopwords
B: for (int j = 0; j < stopwords.length; j++) {
//if stopwords found break
C: if (query[i].equals(stopwords[j])) {
break B;
}
else {
// if this is the last stopword print it
// it means query[i] does not equals with all stopwords
if(j==stopwords.length-1)
{
System.out.println(query[i]);
}
}
}
}
}
}
我嘗試在arraylist中添加停用詞,並嘗試與stringarray進行比較以刪除是否發現了停用詞。 但是我在循環中發現了一些問題。
public static void main(String[] args) {
ArrayList<String> stopWords = new ArrayList<String>();
stopWords.add("that");
stopWords.add("at");
String sentence = "I am not that good at coder";
String[] SentSplit = sentence.split(" ");
System.out.println(SentSplit.length);
StringBuffer finalSentence = new StringBuffer();
boolean b = false;
for(int i=0; i<stopWords.size();i++){
String stopWord = stopWords.get(i);
for(int j = 0; j<SentSplit.length;j++){
String word = SentSplit[j];
if(!stopWord.equalsIgnoreCase(word)){
finalSentence.append(SentSplit[j] + " ");
}
}
}
System.out.println(finalSentence);
}
預期結果是: I am not good coder
但是我的結果是: I am not good at coder I am not that good coder
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.