2014-06-11 4 views
1

먼저 스톱 어구를 제거하고 스템 팅 알고리즘을 적용하여 텍스트 작업을하고 마침내 단어로 분리하여 파일에 저장하려고합니다. 나는 모든 것을했고, 내가 가지고있는 문제는 다음과 같은 단어가 포함 된 파일의 공백입니다 :파일에서 공백 제거 java

Hi 
teacher 

mother 
sister 
father .... and so on 

문제는 교사와 어머니 사이의 공간입니다. 제거하고 싶습니다. 나는 그 이유를 알 수 없다.

다음은 관련 코드의 약제입니다.

public void parseFiles(String filePath) throws FileNotFoundException, IOException { 
    File[] allfiles = new File(filePath).listFiles(); 
    BufferedReader in = null; 
    for (File f : allfiles) { 
     if (f.getName().endsWith(".txt")) { 
      fileNameList.add(f.getName()); 
      Reader fstream = new InputStreamReader(new FileInputStream(f),"UTF-8"); 
      in = new BufferedReader(fstream); 
      StringBuilder sb = new StringBuilder(); 
      String s=null; 
      String word = null; 
      while ((s = in.readLine()) != null) { 
       s=s.trim().replaceAll("[^A-Za-z0-9]", " ");  //remove all punctuation for English text 
       Scanner input = new Scanner(s); 
        while(input.hasNext()) {    
         word= input.next(); 
         word=word.trim().toLowerCase(); 
       if(stopword.isStopword(word)==true) 
       { 
        word= word.replace(word, ""); 
       } 
       String stemmed=stem.stem (word); 
       sb.append(stemmed+"\t"); 

        } 
        //System.out.print(sb); 

      } 
      String[] tokenizedTerms = sb.toString().replaceAll("[\\W&&[^\\s]]", "").split("\\W+"); //to get individual terms (English) 

      for (String term : tokenizedTerms) { 
       if (!allTerms.contains(term)) { //avoid duplicate entry 
       allTerms.add(term); 
        System.out.print(term+"\t"); 
       } 
      } 
      termsDocsArray.add(tokenizedTerms); 
     } 
    } 
    //System.out.print("file names="+fileNameList); 
} 

도와주세요. 감사

모든 빈 줄을 제거하기위한이 같은

답변

4

왜 줄이 비어있는 경우 경우 확인하기 위해 사용하지? 당신의 while 루프에서

while ((s = in.readLine()) != null) { 
    if (!s.trim().isEmpty()) { 
    ... 
    } 
} 
+2

공백만으로 구성된 문자열을 비워 둘 수 있으므로,'trim()'도 추가 할 것입니다. – BackSlash

+0

맞아, 고마워. – Christian

+1

u isEmpty() 메소드를 사용할 수도 있습니다 –

1

시도 뭔가 :

String yourText = "teacher\nmother etc.."; 
String adjustedText = yourText.replaceAll("(?m)^[ \t]*\r?\n", ""); 
+0

고맙습니다. 문제를 해결했습니다. – Souad

1

도이 조건을 추가

동안 ((S = in.readLine())! = null의 & & (! (StringUtils.isBlank (들)))) {

// 여기에 로직이 있습니다. }

+0

고맙습니다. 문제를 해결했습니다. – Souad