2017-11-27 1 views
0

AGENDA ITEM 당 페이지 수를 계산해야합니다. 필자는 pdf 문서의 텍스트를 데이터 프레임으로 추출했습니다. 기본적으로이 데이터 프레임의 한 행에는 한 페이지의 텍스트가 들어 있습니다. 이것은 나의 데이터가 같은 모습입니다 :Count AGENDA- 텍스트 마이닝 당 페이지 수

의제 TEXT (동일 행)에서
mydf <- data.frame(text = c("AGENDA ITEM 1 
     4", "This particular row contains a lot of text, really its all text present in one page", 
     "So ineffect, one page of text per row", "This is another page of text in this row", 
     "lets include another page for agenda 1", "AGENDA ITEM 2 
     9", 
     "now all the text in agenda 2 is included here","the 2nd page text of agenda 2", 
     "AGENDA ITEM 3 
     12", "Now lets just add one row for this agenda, meaning it only has one page inside it")) 

, 수는 페이지 번호이며 같은 행에 있습니다. 일정 별 페이지 수를 계산하려면 다음 AGENDA ITEM이 나타날 때까지 행 수를 계산하면됩니다. 위의 예를 고려하면 대답은

AGENDA ITEM 1 = 4 Pages, AGENDA ITEM 2 = 2 Pages and AGENDA ITEM 3 = 1 Page. 

이어야합니다. 어떻게해야합니까? 나는 텍스트를 분석하는 것에 상당히 익숙하다. 감사합니다

답변

1

"AGENDA ITEM ##"패턴이 일반 텍스트 내에 나타나지 않는 경우 grep()을 사용하여 다음 접근 방식을 사용할 수 있습니다. 나는 이것이 당신을 위해 일하기를 바랍니다.

#get all rownumbers of rows starting with the pattern 
start_rows <- grep("AGENDA ITEM \\d+", mydf$text) 

#get the end of each "AGENDA ITEM chapter" 
#a chapter ends one line before the next chapter starts, hence, 
#-1 and offset -1 from startrows 
#and the final chapter ends with the last line 
end_rows <- c(start_rows[-1]-1 
       ,length(mydf$text)) 

end_rows-start_rows 
#[1] 4 2 1 
+0

같은 grep를 사용할 수 있습니다. 난 그냥 전체 문서에 그것을 테스트하고 놀랍게도 효과. – Syed

+0

다운 스트림 작업에 따라'strsplit()'을 사용하여 원하는 패턴으로 행을 분할하고 (출력이 목록이 될 수 있습니다) 각 요소의 행 수를 계산할 수도 있습니다.하지만 원하는대로 이해했습니다. 당신의 원본 텍스트를 그대로 유지하십시오. 문제가 해결되면 대답을 수락하십시오. –

+0

죄송합니다! 나는 여기서 새로운 대답이기 때문에 대답을 '대답'으로 표시하는 방법을 확신하지 못했습니다. 나는 단지 그것을 봤다. 그리고 그것은 똑딱 똑딱 거리의 문제이었다. 그래, 그대로 텍스트를 유지하고 싶지만 감사합니다 :) – Syed

0

당신은 정말 감사이

mydf <- data.frame(text = c("AGENDA ITEM 1 
          4", "This particular row contains a lot of text, really its all text present in one page", 
          "So ineffect, one page of text per row", "This is another page of text in this row", 
          "lets include another page for agenda 1", "AGENDA ITEM 2 
          9", 
          "now all the text in agenda 2 is included here","the 2nd page text of agenda 2", 
          "AGENDA ITEM 3 
          12", "Now lets just add one row for this agenda, meaning it only has one page inside it")) 

lst <- as.character(mydf$text) 
index <- grep(pattern = "AGENDA ITEM", lst) 
index <- c(index,length(lst)) 

pages <- diff(index) 
pages[1:length(pages)-1] <- pages[1:length(pages)-1] - 1 
pages 

[1] 4 2 1 
+0

감사합니다. Hardik, 그냥 시도해 보니 정말 잘 돌아갔습니다. 정말 고마워요 – Syed

+0

anmswer upvote 수 주시겠습니까? –

+0

나는 몇 번이나 투표를 시도했다. 잠깐 동안 숫자 0을 1로 변경하면 다음 메시지가 나타난다. '의견에 감사드립니다! 평판이 15 명 미만인 사람의 투표는 기록되지만 공개적으로 게시 된 점수는 변경되지 않습니다. – Syed

관련 문제