字符串未正确检查停用词
String not checked correctly for stop words
我正在从保存在 HashSet
中的文件中读取停用词。我将所说的 HashSet
与 String
进行比较以检查停用词。
如果我在 String
变量中放置一个停用词,例如 "the",我的输出是 "Yes"。但是,如果我输入 "Apple is it" 或 "it is an apple" 之类的内容,输出是 "No",尽管 String
变量都包含停用词。
这是整个程序,包含两种方法,一种用于读取文件,一种用于删除停用词:
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");
}
}
我觉得我没有正确阅读你的问题。但是这里是。
假设:
String search = "it is an apple";
那么你应该拆分字符串并单独检查每个单词。
String[] split = search.split(" ");
for (String s : split) {
if (hset.contains(s.toLowerCase()) {
System.out.println("Yes");
break; //no need to continue if a stop word is found
} else {
System.out.println("No");
}
我正在从保存在 HashSet
中的文件中读取停用词。我将所说的 HashSet
与 String
进行比较以检查停用词。
如果我在 String
变量中放置一个停用词,例如 "the",我的输出是 "Yes"。但是,如果我输入 "Apple is it" 或 "it is an apple" 之类的内容,输出是 "No",尽管 String
变量都包含停用词。
这是整个程序,包含两种方法,一种用于读取文件,一种用于删除停用词:
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");
}
}
我觉得我没有正确阅读你的问题。但是这里是。
假设:
String search = "it is an apple";
那么你应该拆分字符串并单独检查每个单词。
String[] split = search.split(" ");
for (String s : split) {
if (hset.contains(s.toLowerCase()) {
System.out.println("Yes");
break; //no need to continue if a stop word is found
} else {
System.out.println("No");
}