CommentFilter

   

This perl program allows you to strip unwanted comments from an SGF file downloaded from KGS.

Usage

Install perl, copy the program below into a text file and call it CommentFilter.pl. On your computer from within a DOS (PC) or shell prompt (UNIX/Linux) type: -

 perl -w CommentFilter.pl oldsgffile.sgf newsgffile.sgf options

where options are as follows: -

 -pl         - Keep comments from the players of the game.
 -u username - Keep comments from username.

Examples

 perl -w CommentFilter.pl bvsa.sgf newbvsa.sgf -pl

Keep only comments from the players of the game.

 perl -w CommentFilter.pl bvsa.sgf newbvsa.sgf -u Datakuru

Keep comments by Datakuru only.

 perl -w CommentFilter.pl bvsa.sgf newbvsa.sgf -pl Datakuru -pl chrisg

Keep comments by Datakuru and chrisg only.

Improvements

If anyone feels inclined to make these improvements please add an in progress tag to the end of the improvement and have a go. Please remember to update this page when you've finished :-), thanks.

Author

Chris Goldsmith

The Program

 #!/bin/perl -w
 #
 # CommentFilter.pl by Chris Goldsmith
 #
 # Usage CommentFilter.pl sgffile userstokeep
 #
 #
 # userstokeep can be
 # -u username         - users with this username are not filtered
 # -pl                 - keep comments from the players of the game
 #
 # For further study
 # -m minimum rank     - minimum rank for comments to keep
 #
 # Examples:
 #    CommentFilter.pl chrisg-bibble.sgf cg-bib-filtered1.sgf  -pl
 #    Keeps all comments by chrisg and bibble in the file.
 #
 #    CommentFilter.pl chrisg-bibble.sgf cg-bib-filtered2.sgf -pl -u Datakuru
 #    Keeps all comments by chrisg, bibble and Datakuru in the file.
 #
 #    CommentFilter.pl chirsg-bibble.sgf cg-bib-filtered3.sgf -u Datakuru
 #    Keeps all comments by Datakuru in the file.
 die "Please specify <fromfile> <tofile> and <users to keep>"  unless
     ($#ARGV >= 1);
 $False = (1==0);
 $True  = (1==1);
 $FromFile = shift;
 $ToFile = shift;
 $KeepPlayers = $False;
 @PlayersToKeep = ();
 # $MinimumRank = "";
 while ( $ARGV = shift )
 {
     if ( $ARGV eq "-u" )
     {
         $PlayersToKeep[$#PlayersToKeep+1] = shift or die
         "-u specified without username";
     }
     # elsif ( $ARGV eq "-m" )
     # {
     #     $MinimumRank = shift or die "-m specified without rank";
     # }
     elsif ( $ARGV eq "-pl" )
     {
         $KeepPlayers = $True;
     }
 }
 open ( SGFFILE, "<$FromFile" ) or die "Unable to open file $FromFile";
 open ( TOFILE, ">$ToFile" ) or die "Unable to open file $ToFile";
 while (<SGFFILE>)
 {
     if ( $KeepPlayers )
     {
         if ( /PW\[([!A-Za-z0-9]+)\]/ )
         {
             $PlayersToKeep[$#PlayersToKeep+1] = $1;
         }
         if ( /PB\[([!A-Za-z0-9]+)\]/ )
         {
             $PlayersToKeep[$#PlayersToKeep+1] = $1;
         }
     }
     if ( /([!A-Za-z0-9]+)\s\[(.+)\\\]:\s+(.*)/ )
     {
         $Name = $1;
  # $Rank = $2;
  # $Comment = $3;
         $KeepComment = $False;
         foreach $User (@PlayersToKeep)
         {
             if ( $User eq $Name )
             {
                 $KeepComment = $True;
             }
         }
         if ( not $KeepComment )
         {
             s/[!A-Za-z0-9]+\s\[.+\\\]:\s.*//;
         }
     } print TOFILE;
 }
 close SGFFILE;
 close TOFILE;

juhtolv: Ahem... What is the license of that script? I propose you use GNU GPL.


This is a copy of the living page "CommentFilter" at Sensei's Library.
(OC) 2004 the Authors, published under the OpenContent License V1.0.
[Welcome to Sensei's Library!]
StartingPoints
ReferenceSection
About