我正在讀取文件中的停止詞,我將它保存在HashSet
中。我比較說HashSet
與String
檢查停用詞。字符串沒有正確檢查停止詞
如果我在String
-變量中放置了一個單詞,例如「the」,那麼我的輸出是「Yes」。但是,如果我將「Apple is it」或「它是一個蘋果」這樣的東西輸出爲「No」,儘管String
-variables包含停用詞。
這裏的整個程序,包含兩種方法,一種用於讀取文件和一個用於去除停止詞:
private static HashSet<String> readFile(){
Scanner x = null;
HashSet<String> hset = new HashSet<String>();
try {
x = new Scanner(new File("StopWordsEnglish"));
while(x.hasNext()){
hset.add(x.next());
}
} catch(Exception e) {
e.printStackTrace();
} finally {
x.close();
}
return hset;
}
public static void removeStopWords(){
HashSet<String> hset = readFile();
System.out.println(hset.size());
System.out.println("Enter a word to search for: ");
String search = "is";
String s = search.toLowerCase();
System.out.println(s);
if (hset.contains(s)) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
使用調試器,並發現它在空間 – Jens