Advertisement
maya000

青空文庫形式テキスト分割ツール

Jun 28th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.17 KB | None | 0 0
  1. #!/usr/bin/perl
  2. # last updated : 2017/06/19 17:37:47 JST
  3. #
  4. # 青空文庫スプリッター
  5. # 青空文庫形式のテキストを任意サイズで分割する。デフォルトは512k
  6. # Copyright (c) 2017 ◆.nITGbUipI
  7. # license GPLv2
  8. #
  9. # Usage.
  10. # ./aozora_splitter.pl [OPTION]...-i  [INPUT -p [PREFIX]]
  11. # /aozora_splitter.p --help
  12. # 詳細は参照。
  13. #
  14. # 例.
  15. # test.txtを 512kbのサイズでファイル名「aaa-」の頭文字を付けて保存する。
  16. # aozora_splitter.pl -s 512k -i test.txt -p aaa-
  17. #
  18.  
  19.  
  20. use strict;
  21. use warnings;
  22. use utf8;
  23. use Encode;
  24. use File::Basename;
  25. use Encode::Guess qw/cp932 euc-jp 7bit-jis/;
  26. use Getopt::Long qw(:config posix_default no_ignore_case gnu_compat);
  27.  
  28. my $splitsize = 512000;
  29. my $ver_number = "Ver.0.01";
  30. my ($split_size, $input_name, $show_help, $prefix_name, $print_verbose, $show_version);
  31. my $charcode = 'utf8';
  32.  
  33. if ($^O =~ m/MSWin32/) {
  34.   $charcode = "cp932";
  35. }
  36.  
  37. #文字コード判定
  38. sub guess  {
  39.   my $file = shift;
  40.   my $fh;
  41.   open ($fh, '<:raw', $file) or die "OPEN FAILED: $file, $!";
  42.   my ($temp, $i);
  43.   while ($temp .= <$fh> and ++$i < 50) { #50行読み込む
  44.     eof and last;
  45.   }
  46.   close ($fh);
  47.   return  guess_encoding($temp);
  48. }
  49.  
  50. #コマンドラインの取得
  51. sub getopt() {
  52.   GetOptions(
  53.     "size|s=s"   => \$split_size,
  54.     "input|i=s"  => \$input_name,
  55.     "help|h"     => \$show_help,
  56.     "prefix|p=s" => \$prefix_name,
  57.     "verbose|v"  => \$print_verbose,
  58.     "version|V"  => \$show_version
  59.   );
  60. }
  61.  
  62. sub help {
  63.   print encode($charcode,
  64.         "Usage: aozora_spliter [options] -i [INPUT_FILE] -p [PREFIX]\n".
  65.         "\t青空文庫形式のテキストを任意サイズで分割するsplitコマンド\n".
  66.         "\t改ページを一つのブロックとして、指定したサイズの近似値で\n".
  67.         "\tPREFIXの末尾に三桁の連番を付けて保存する。\n".
  68.         "\t拡張子は[.txt]固定。\n".
  69.         "\n".
  70.         "\tOption:\n".
  71.         "\t\t-s|--size\n".
  72.         "\t\t\t分割するサイズを指定。デフォルトは512k\n".
  73.         "\t\t\t例:-s 512k、-s 1M などと指定する。\n".
  74.         "\t\t-i|--input\n".
  75.         "\t\t\t分割するファイルを指定する。\n".
  76.         "\t\t-h|--help\n".
  77.         "\t\t\tこのテキストを表示する。\n".
  78.         "\t\t-V|--version\n".
  79.         "\t\t\t現在のバージョンを表示する。\n".
  80.         "\t\t-p|--prefix\n".
  81.         "\t\t\t分割するファイル名の接頭辞を指定する。\n".
  82.         "\t\t\t接頭辞の後に連番が付与される。\n"
  83.       );
  84.   exit 0;
  85. }
  86.  
  87. #version
  88. sub version() {
  89.   print encode($charcode,
  90.         "aozora_spliter.pl ". $ver_number. " (c) 2017 ◆.nITGbUipI \n"
  91.               );
  92.   exit 0;
  93. }
  94.  
  95. sub size_convert {
  96.   my $i =shift;
  97.   if ($i =~ m/([0-9]+)(\D)/) {
  98.     my $size = $1;
  99.     my $tani = $2;
  100.     if ($tani =~ m/k|K/) { $size = $size * 1000 }
  101.     if ($tani =~ m/m|M/) { $size = $size * 1000 * 1000 }
  102.     if ($tani =~ m/g|G/) { $size = $size * 1000 * 1000 * 1000 }
  103.     return $size;
  104.   } elsif ($i < 100000) {
  105.     return 100000; # 最小サイズを100kに固定
  106.   } else {
  107.     return $i;
  108.   }
  109. }
  110.  
  111. sub set_enccode {
  112.   my $i = shift;
  113.   my $x = &guess($i);
  114.   return $x->name;
  115. }
  116.  
  117. sub book_split {
  118.   my $fname = shift;
  119.   my ($IN, $OUT);
  120.   my $count = 1;
  121.   my $temp;
  122.   my $outname;
  123.   $charcode = &set_enccode($input_name);
  124.   if ($print_verbose) { print STDERR encode($charcode, "character encode:: $charcode \n"); }
  125.   open ( $IN, "<:encoding($charcode)" ,"$fname") or die "$!";
  126.   while (my $line = <$IN>) {
  127.     if ($line =~ m/^[#改ページ]/) {
  128.       $outname = $prefix_name . sprintf("%03d", $count) . ".txt";
  129.       if ( defined($OUT) ) {
  130.         print $OUT $temp;
  131.       }
  132.       else {
  133.         open ( $OUT, ">>:encoding($charcode)" ,"$outname") or die "$!";
  134.         print $OUT $temp;
  135.         if ($print_verbose) {
  136.           print STDERR encode($charcode, "output:: $outname  \n");
  137.         }
  138.       }
  139.       $temp = "";
  140.       $temp = $temp . $line;
  141.       my $size = (-s $outname);
  142.       if ($size > $splitsize) {
  143.         close( $OUT );
  144.         $OUT = undef;
  145.         $count++;
  146.       }
  147.     }
  148.     else {
  149.       $temp = $temp . $line;
  150.     }
  151.   }
  152.   close($IN);
  153.   close($OUT);
  154. }
  155.  
  156. # main
  157. {
  158.   &getopt;
  159.  
  160.   if ($split_size) {
  161.     $splitsize = &size_convert($split_size);
  162.   }
  163.  
  164.   if ($input_name) {
  165.     if ( -f $input_name ) {
  166.       unless ($prefix_name) {
  167.         my ($basename, $dirname, $ext) = fileparse($input_name, qr/\..+$/);
  168.         $prefix_name = $basename;
  169.         if ($print_verbose) { print STDERR encode($charcode, "PREFIX word :: $prefix_name . \n") };
  170.       }
  171.       my $name = $prefix_name .  "001.txt";
  172.       if (-f $name) {
  173.         print encode($charcode, "$name が存在するため処理を中止します。\n");
  174.         exit 0;
  175.       }
  176.       if ($print_verbose) {
  177.         if ($print_verbose) { print STDERR encode($charcode, "input file:: $input_name \n") };
  178.       }
  179.       if ($print_verbose) { print STDERR encode($charcode, "split size $splitsize byte.\n") };
  180.       &book_split($input_name);
  181.     }
  182.     else {
  183.       print encode($charcode, "ファイルが存在しません。\n");
  184.       exit 0;
  185.     }
  186.   }
  187.   elsif ($show_help) {
  188.     &help();
  189.     exit 0;
  190.   }
  191.   elsif ($show_version) {
  192.    &version();
  193.    exit 0;
  194.   }
  195.   elsif ($print_verbose) {
  196.   }
  197.   else {
  198.     &help();
  199.     exit 0;
  200.   }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement