1
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;
                }
            }
        } 
    }
}

何らかの理由で、このコードは問題の途中でしか正しく機能しません。クエリから最初のストップワードを取り出しますが、残りは無視します。どんな助けでも大歓迎です。

4

2 に答える 2

1
 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]);
                        }
                    }
                }
            } 
        }
    }
于 2013-06-02T01:04:18.143 に答える
0

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

于 2014-04-15T00:05:15.597 に答える