2017-01-31 3 views
-2

단어의 ArrayList를 통과하고 문장의 평균 길이를 찾는 함수를 작성해야하는 할당이 있습니다. 내 함수는 3 개의 테스트 중 2 개를 통과합니다. 검색중인 문장 부호는 ArrayList 슬롯에서 자체적으로 표시되기 때문에 제거되고 마지막에 cleanUp이라는 구두점 제거 함수를 사용할 때 제거됩니다. 설명하기가 조금 어렵 기 때문에 코드의 모든 관련 부분을 보여줄 것입니다.arrayLists를 사용하여 문장의 평균 길이를 찾는 함수

static String cleanUp(String str) { 
    Pattern p = Pattern.compile("(\\W*)(.*?)(\\W*)"); 
    Matcher m = p.matcher(str); 
    m.matches(); 
    return str.substring(m.end(1), m.end(2)).toLowerCase();  
} 

static double averageSentenceLength(ArrayList<String> text) { 
    double sentences = 0; 
    double realLength = 0; 
    boolean doublePunctuation = false; 

    for(int i = 0; i < text.size(); i++){ 
     if(cleanUp(text.get(i)).length() != 0) { 
     realLength++; 
     if(text.get(i).substring(text.get(i).length()-1).equals(".") || 
      text.get(i).substring(text.get(i).length()-1).equals("!") || 
      text.get(i).substring(text.get(i).length()-1).equals("?")) 
      sentences++; 
     } 
    } 
    return realLength/sentences; 
} 

가 통과하지 않는 것이 시험 :

public void testaverageSentenceLength2() { 
    String[] textArray = {"The", "time", "has", "**********************","come,", "the", "Walrus", "said", 
     "To", "talk", "of", "many", "thi-ngs:", "of", "shoes", "-", "and", "ships", "-", "and", "sealing", "wax", ",", 
     "Of", "cabbages;", "and","!#[email protected]", "kings","?", 
     "And", "why", "the", "sea", "is", "boi.ling", "hot;", 
     "and", "whe;ther", "pigs", "have", "win.gs!"}; 
    ArrayList<String> text = new ArrayList<String>(); 
    for (String str : textArray) { 
     text.add(str); 
    }  
    double avg = FindAuthor.averageSentenceLength(text); 
    assertTrue("Average sentence length of the sample should be 17.5 but was "+avg,approx(avg,17.5)); 
} 

문제가 있기 때문에 계산되지 않는 "왕", 따라서 평균 문장 길이 후 물음표 발생 더블 무엇은해야 있다. 당신의 cleanup() 방법에서

+0

평균 길이를? –

+0

제목의 의미는 무엇입니까? – EJP

+0

@ cricket_007 문장 당 평균 단어 수 –

답변

0

str.substring(m.end(1), m.end(2)).toLowerCase()? 문자열을 처리 할 때 빈 문자열을 반환하므로 if의 몸은 실행되지 않습니다 : 문장 또는 단어의

if(cleanUp(text.get(i)).length() != 0) { 
    realLength++; 
    if(text.get(i).substring(text.get(i).length()-1).equals(".") || 
     text.get(i).substring(text.get(i).length()-1).equals("!") || 
     text.get(i).substring(text.get(i).length()-1).equals("?")) 
     sentences++; 
} 
관련 문제