word-count 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #! /usr/bin/env perl
  2. # Copyright (C) 2015 Alex Schroeder <alex@gnu.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. use strict;
  17. use warnings;
  18. sub ParseData {
  19. my $data = shift;
  20. my %result;
  21. while ($data =~ /(\S+?): (.*?)(?=\n[^ \t]|\Z)/sg) {
  22. my ($key, $value) = ($1, $2);
  23. $value =~ s/\n\t/\n/g;
  24. $result{$key} = $value;
  25. }
  26. return %result;
  27. }
  28. sub main {
  29. my ($match, $search, $PageDir) = @_;
  30. my $pages = 0;
  31. my $words = 0;
  32. # include dotfiles!
  33. local $/ = undef; # Read complete files
  34. foreach my $file (glob("$PageDir/*.pg $PageDir/.*.pg")) {
  35. next unless $file =~ m|.*/(.+)\.pg$|;
  36. my $id = $1;
  37. next if $match && $id !~ /$match/;
  38. open(F, $file) or die "Cannot read $id file: $!";
  39. my $data = <F>;
  40. close(F);
  41. my %Page = ParseData($data);
  42. next if $search && $Page{text} !~ /$search/;
  43. my $n = () = $Page{text} =~ /\w+/g;
  44. $words += $n;
  45. $pages++;
  46. warn "$id: $n\n" if $ENV{DEBUG};
  47. }
  48. die "No pages found in $PageDir\n" unless $pages;
  49. printf "%d %d %.2f\n", $words, $pages, $words /$pages;
  50. }
  51. use Getopt::Long;
  52. my $match = undef;
  53. my $search = undef;
  54. my $dir = 'page';
  55. my $help = undef;
  56. GetOptions ("match=s" => \$match,
  57. "search=s" => \$search,
  58. "dir=s" => \$dir,
  59. "help" => \$help);
  60. if ($help or not -d $dir) {
  61. print qq{Usage: $0 [--match REGEXP] [--search REGEXP] [--dir DIR]
  62. Counts the words for your pages.
  63. --match selects a subsets of pages whose names match the regular
  64. expression. Note that spaces have been translated to underscores.
  65. --search selects a subsets of pages whose content matches the regular
  66. expression.
  67. --page designates the page directory. By default this is 'page' in the
  68. current directory. If you run this script in your data directory,
  69. the default should be fine.
  70. If the environment variable DEBUG is set, the page count for each page
  71. will be printed to stderr.
  72. Examples
  73. --------
  74. Journal pages only:
  75. $0 --match '^\\d{4}-\\d{2}-\\d{2}'
  76. Skip comment pages:
  77. $0 --match '^(?!Comments_on_)'
  78. Pages tagged RPG:
  79. $0 --search '\[\[tag:RPG\]\]'
  80. Debug output:
  81. DEBUG=1 $0
  82. }
  83. } else {
  84. main ($match, $search, $dir);
  85. }