2017-11-27 6 views
0

です。AGENDA ITEMあたりのページ数を数えなければなりません。私はpdf文書からテキストをデータフレームに抽出しました。このデータフレームの本質的に1つの行には、1ページのテキストが含まれています。これは私のデータがどのように見えるかです:件数AGENDAテキストマイニング1件あたりのページ数は

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 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

申し訳ありません!私はここで新しいので、あなたの答えを「答えた」とマークする方法がわかりませんでした。私はちょうどそれをgoogledとそれはちょうど目をつぶすの問題だった。はい、私はそれがそのままのテキストを保つが、ありがとう:) – 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

私はそれを投票するために数回試しました。それは1秒間で0に1を変更し、次のメッセージが表示されます。評判が15未満の人の投票が記録されますが、公開されている投稿のスコアは変更されません。 – Syed