Java 未正确检查停止字的字符串
我正在从文件中读取停止字,并将其保存在Java 未正确检查停止字的字符串,java,stop-words,Java,Stop Words,我正在从文件中读取停止字,并将其保存在哈希集中。我将所说的HashSet与String进行比较,以检查停止词 如果我在字符串-变量中输入单个停止字,如“the”,则输出为“Yes”。但是,如果我输入“appleist”或“it is a Apple”之类的内容,则输出为“No”,尽管两个String-变量都包含停止词 以下是整个程序,包含两种方法,一种用于读取文件,另一种用于删除停止字: private static HashSet<String> readFile(){ S
哈希集中。我将所说的HashSet
与String
进行比较,以检查停止词
如果我在字符串
-变量中输入单个停止字,如“the”,则输出为“Yes”。但是,如果我输入“appleist”或“it is a 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");
}
}
private static HashSet readFile(){
扫描器x=null;
HashSet hset=新的HashSet();
试一试{
x=新扫描仪(新文件(“StopWordsEnglish”);
while(x.hasNext()){
hset.add(x.next());
}
}捕获(例外e){
e、 printStackTrace();
}最后{
x、 close();
}
返回hset;
}
公共静态void removeStopWords(){
HashSet hset=readFile();
System.out.println(hset.size());
System.out.println(“输入要搜索的单词:”);
String search=“is”;
字符串s=search.toLowerCase();
系统输出打印项次;
如果(hset.包含){
System.out.println(“是”);
}否则{
系统输出打印项次(“否”);
}
}
我感觉我没有正确理解你的问题。但事情是这样的
假设:
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");
}
在这种情况下,使用调试器并发现它在空间上的扩展听起来是一件很好且合适的事情,但我想补充一点,标记化可能是一个困难而微妙的问题,例如: