#!/usr/bin/perl # File : verifydic.pl # Author: Lyndon Hill, http://www.lyndonhill.com # Note : This script is free to use. If you make significant improvements please # send me a copy. # # Script to check dictionary files. It checks: # 1. Empty entries, ie. entry line is blank followed by senses and subsenses # This means there must never be more than one blank line between entries # 2. Opening and closing senses: {s} and {/s} should be on separate lines with # no spaces in front of them. You should not have nested senses. # Usage: # verifyfic.pl dictionary.dic # input dictionary file $dicfile = @ARGV[0]; open(DICFILE, $dicfile) || die "Can't open file $dicfile\n"; $ecount = 0; # total number of entries $lineno = 0; # number of line read from dictionary file $sensecount = 0; # number of senses encountered $nextline = 0; # flag to say end of entry (next line is an entry) $opens = 0; # flag to say {s} is open $checkopenclose = 0; # flag to say entry detected, check sense while() { $line = $_; chop $line; if($checkopenclose == 1) { # check for opening {s}, should be closed before start a new sense if((substr($line, 0, 3)) eq "{s}") { $sensecount = $sensecount+1; if($opens == 1) { print "Double {s} at line $lineno, entry $entry.\n"; } $opens = 1; } elsif((substr($line, 0, 4)) eq "{/s}") { # check for closing {/s}, should be open before closing if($opens == 1) { $opens = 0; } else { print "Double {/s} at line $lineno, entry $entry.\n"; } $checkopenclose = 0; # stop checking for open and closing senses } } # check entry is text if($nextline == 1) { # read the entry, record it's position $entry = $line; # check if entry has space at the end: this will mess up entry look ups if(substr($entry, length($entry)-1, 1) eq " ") { print "Entry '$entry' has space at end.\n"; } if((substr($entry, 0, 3)) eq "{s}") { print "Opening sense but no entry at $lineno\n"; } elsif((substr($entry, 0, 4)) eq "{/s}") { print "Closing sense instead of entry at $lineno\n"; } else { # Got an entry, check for opening/closing senses $checkopenclose = 1; } $ecount++; $nextline = 0; } else { if($line eq "") { # next line should be an entry $nextline = 1; } } $lineno++; } $lineno++; # count line 0 # End of file : Finish up close(DICFILE); print "$sensecount definitions detected in $lineno lines.\n"; exit;