从String中删除停用词

时间:2013-06-02 00:55:28

标签: java words

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

由于某些原因,此代码仅在问题的中途正常工作。它从查询中取出第一个禁用词,但它忽略了其余的。任何帮助,将不胜感激。

2 个答案:

答案 0 :(得分: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]);
                        }
                    }
                }
            } 
        }
    }

答案 1 :(得分: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