2017-05-16 11 views
-1

HashSet에 저장중인 파일에서 정지 단어를 읽습니다. 난 StringHashSet을 말하면 정지 단어를 확인합니다. 문자열이 중지 단어에 대해 올바르게 검사되지 않음

나는 등 "은"의 String -variable에, 내 출력이 "예"와 같이, 하나의 중지 단어를 넣어합니다. 내가 좋아하는 뭔가를 넣어 경우, 또는 두 가지 모두 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"); 
    } 
} 
+0

디버거를 사용하여이 경우에 할 수있는 좋은 적절한 것 같은 소리 공간에 – Jens

답변

1

:

여기에 두 개의 파일을 읽는 방법, 다른 하나는 중지 단어를 제거하기위한 하나를 포함, 전체 프로그램입니다. 그러나 여기에 간다.

가정 :

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"); 
} 
+0

분할 그것을 알아,하지만 난 예를 들어, 어렵고 미묘한 문제가 될 수있는 토큰 화를 추가하고 싶었 : HTTPS : //www.tutorialspoint.com/opennlp/opennlp_tokenization.htm – hugh